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

сд2

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

3Заключение

Входе выполнения практической работы были реализованы быстрая сортировка, сортировка Шелла и сортировка расчёской. Было произведено сравнение алгоритмических сложностей данных сортировок и сгенерированы лучшие и худшие варианты. Результаты совпали с теоретическими.

11

ПРИЛОЖЕНИЕ А

using System;

using System.Diagnostics; Random rnd = new Random();

for (int n = 1; n <= 1000; n++)

{

int LongArray = n;

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()); // Сортировка расчёской

12

/* Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Green;

Console.WriteLine("\nСортировка расчёской\n"); Console.ResetColor(); */

int count_sr = 0; int count_sw = 0;

Stopwatch stopwatch = new Stopwatch(); double factor = 1.2473309;

double step = n - 1;

stopwatch.Start(); while (step >= 1)

{

for (int i = 0; i + step < n; i++)

{

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

{

double temp = arrayComb[i]; arrayComb[i] = arrayComb[i + (int)step]; arrayComb[i + (int)step] = temp; count_sw++;

}

count_sr++;

}

step = step / factor;

13

}

stopwatch.Stop();

Console.WriteLine($"\n {n}, {count_sr + count_sw}, {stopwatch.Elapsed.TotalMilliseconds}");

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

{

Console.WriteLine(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) 14

{

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++) 15

{

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;

16

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

{

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

}

} */

17

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