
Приложение б (Быстрая)
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
int[] lengths = { 1, 2, 3, 4, 5, 10, 15, 20, 25, 30, 40, 50, 75, 100, 150, 200, 250, 300, 400, 500, 600, 800, 1000 };
foreach (int length in lengths)
{
Console.WriteLine($"Сортировка быстрой сортировкой для массива длиной {length}");
int[] array = new int[length]; Random random = new Random();
for (int i = 0; i < length; i++)
{
array[i] = random.Next(1000); // Генерация случайных чисел от 0 до 999
}
Console.WriteLine($"До сортировки: {string.Join(", ", array)}");
int comparisons = 0; int swaps = 0;
Stopwatch stopwatch = new Stopwatch(); stopwatch.Start();
QuickSort(array, ref comparisons, ref swaps);
stopwatch.Stop();
Console.WriteLine($"После сортировки: {string.Join(", ", array)}"); Console.WriteLine($"Количество сравнений: {comparisons}");
Console.WriteLine($"Количество перестановок: {swaps}"); Console.WriteLine($"Время выполнения: {stopwatch.Elapsed.TotalMilliseconds} мс");
Console.WriteLine();
}
Console.ReadLine();
}
static void Swap(ref int x, ref int y)
{
var t = x; x = y;
y = t;
}
static int Partition(int[] array, int minIndex, int maxIndex, ref int comparisons, ref int swaps)
{
var pivot = minIndex - 1;
for (var i = minIndex; i < maxIndex; i++)
{
comparisons++;
if (array[i] < array[maxIndex])
{
pivot++;
swaps++; Swap(ref array[pivot], ref array[i]);
}
}
pivot++;
swaps++; Swap(ref array[pivot], ref array[maxIndex]);
return pivot;
}
static int[] QuickSort(int[] array, int minIndex, int maxIndex, ref int comparisons, ref int swaps)
{
if (minIndex >= maxIndex)
{
return array;
}
var pivotIndex = Partition(array, minIndex, maxIndex, ref comparisons, ref swaps);
QuickSort(array, minIndex, pivotIndex - 1, ref comparisons, ref swaps); QuickSort(array, pivotIndex + 1, maxIndex, ref comparisons, ref swaps);
return array;
}
static int[] QuickSort(int[] array, ref int comparisons, ref int swaps)
{
return QuickSort(array, 0, array.Length - 1, ref comparisons, ref swaps);
}
}
}
Приложение в (Вставками)
using System.Diagnostics;
static void Main(string[] args)
{
int[] lengths = { 1, 2, 3, 4, 5, 10, 15, 20, 25, 30, 40, 50, 75, 100, 150, 200, 250, 300, 400, 500, 600, 800, 1000 };
foreach (int length in lengths)
{
Console.WriteLine($"Сортировка вставками для массива длиной {length}");
int[] arr = new int[length];
Random a = new Random();
Random f = new Random();
for (int i = 0; i < length; i++)
{
Console.Write($"Элемент массива с индексом {i + 1}: ");
int minus = a.Next(-100, -1);
int plus = f.Next(1, 100);
if (i == 0)
{
arr[i] = minus;
}
else if (i % 2 == 0)
{
while (minus < 0)
{
if (minus % 2 != 0)
{
arr[i] = minus;
break;
}
minus++;
}
}
else
{
while (plus > 0)
{
if (plus % 2 != 0)
{
arr[i] = plus;
break;
}
plus--;
}
}
Console.WriteLine($" {arr[i]}");
}
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
int comparisons = 0;
int swaps = 0;
for (int t = 0; t < length; t++)
{
int x = arr[t];
int j = t;
while (j > 0 && (x < arr[j - 1]))
{
arr[j] = arr[j - 1];
j--;
comparisons++;
swaps++;
}
arr[j] = x;
swaps++;
}
stopwatch.Stop();
Console.WriteLine("Отсортированный массив:");
for (int t = 0; t < length; t++)
{
Console.WriteLine($" {arr[t]}");
}
int totalOperations = comparisons + swaps;
Console.WriteLine($"Количество операций: {totalOperations}");
Console.WriteLine($"Время выполнения: {stopwatch.Elapsed.TotalMilliseconds} мс");
Console.WriteLine();
}
Console.ReadLine();
}
}
}