Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

сд1

.pdf
Скачиваний:
2
Добавлен:
01.12.2024
Размер:
259 Кб
Скачать
☆

Приложение А

(обязательное)

Сортировка расческой

using System;

Random rnd = new Random();

int LongArray = rnd.Next(20, 101); double[] array1 = new double[LongArray];

Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("\nИсходный массив\n"); Console.ResetColor();

for (int i = 0; i < LongArray; i++)

{

array1[i] = rnd.NextDouble() * Math.Pow(10, 6);

}

for (int i = 0; i < LongArray; i++)

{

array1[i] = Math.Round(array1[i], 3); Console.Write(array1[i]+"; ");

}

double[] arrayComb = (double[])(array1.Clone()); double[] arrayQuick = (double[])(array1.Clone()); double[] arrayShell = (double[])(array1.Clone()); // Сортировка расчёской

Console.WriteLine();

Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("\nСортировка расчёской\n"); Console.ResetColor();

double factor = 1.247; // фактор уменьшения

double ras_step = LongArray - 1; while (ras_step >= 1)

{

for (int i = 0; i + ras_step < LongArray; i++)

{

if (arrayComb[i] > arrayComb[i + (int)ras_step])

{

double temp = arrayComb[i];

arrayComb[i] = arrayComb[i + (int)ras_step]; arrayComb[i + (int)ras_step] = temp;

}

}

ras_step = ras_step / factor;

}

for (int i = 0; i < LongArray; i++)

{

Console.Write(arrayComb[i] + "; ");

}

Приложение Б

(обязательное)

Быстрая сортировка

Console.WriteLine();

Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("\nБыстрая сортировка\n"); Console.ResetColor();

void QuickSort(double[] array, int left, int right)

{

if (left < right)

{

int pivotIndex = Partition(array, left, right); QuickSort(array, left, pivotIndex - 1); QuickSort(array, pivotIndex + 1, right);

}

}

int Partition(double[] array, int left, int right)

{

double pivot = array[right]; int i = left - 1;

for (int j = left; j < right; j++)

{

if (array[j] < pivot)

{

i++; Swap(array, i, j);

}

}

Swap(array, i + 1, right); return i + 1;

}

void Swap(double[] array, int i, int j)

{

double temp = array[i]; array[i] = array[j]; array[j] = temp;

}

QuickSort(arrayQuick, 0, arrayQuick.Length - 1); for (int i = 0; i < arrayQuick.Length; i++)

{

Console.WriteLine(arrayQuick[i]);

}

Приложение В

(обязательное)

Сортировка Шелла

Console.WriteLine();

Console.ForegroundColor = ConsoleColor.Green;

Console.WriteLine("\nСортировка Шелла\n");

Console.ResetColor();

double Shell_step = LongArray / 2; while (Shell_step > 0)

{

for (int i = 0; i < (LongArray - Shell_step); i++)

{

int j = i;

while (j >= 0 && arrayShell[j] > arrayShell[j + (int)Shell_step])

{

double temp = arrayShell[j];

arrayShell[j] = arrayShell[j + (int)Shell_step]; arrayShell[j + (int)Shell_step] = temp;

j--;

}

}

Shell_step = Shell_step / 2;

}

for (int i = 0; i < LongArray; i++)

{

Console.WriteLine(arrayShell[i] + "; ");

}

Соседние файлы в предмете Структуры данных