Материал: Практика 2 - Алгоритмичекая сложность алгоритмов - СФ

Внимание! Если размещение файла нарушает Ваши авторские права, то обязательно сообщите нам

for (int i = 0; i <= arr.Length - 1; i++)

{

arr[i] = rand.NextDouble(); arr[i] *= 100;

while (arr[i] / 10 < 1)

{

arr[i] = rand.Next(); arr[i] *= 100;

}

}

int sum = 0; stopWatch.Start();

sorting(arr, 0, arr.Length - 1, ref sum); stopWatch.Stop();

TimeSpan ts = stopWatch.Elapsed; Console.WriteLine();Console.WriteLine($"Количество операций = {sum}"); Console.WriteLine("RunTime " + ts);

Main(args);

}

}

}

16

Приложение В

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

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

using System;

using System.Threading; using System.Diagnostics;

namespace StructureOfData22

{

class Program

{

static void Main(string[] args)

{

try

{

Console.WriteLine("========================================"); Console.Write("Введите количество чисел в массиве (Сортировка Шелла):

");

int n = int.Parse(Console.ReadLine()); Random rand = new Random(); int Sum = 0; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start();

double[] arr = new double[n]; for (int i = 0; i <= n - 1; i++)

{

arr[i] = rand.NextDouble(); arr[i] *= 100;

while (arr[i] / 10 < 1)

{

arr[i] = rand.NextDouble(); arr[i] *= 100;

}

}

int step = arr.Length / 2; while (step > 0)

{

for (int i = 0; i < arr.Length - step; i++)

{

Sum++;

int index = i;

while (index >= 0 && arr[index] > arr[index + step])

{

Sum++;

double tmp = arr[index]; arr[index] = arr[index + step]; arr[index + step] = tmp; index--;

}

}

step /= 2;

}

stopWatch.Stop();

TimeSpan ts = stopWatch.Elapsed; Console.WriteLine(); Console.WriteLine("RunTime " + ts);

Console.WriteLine($"Количество операций = {Sum}"); //Console.Readline();

Main(args);

}

catch (Exception)

{

17

Console.WriteLine("Неверно введены данные!!!"); Main(args);

}

}

}

}

18

Источник: https://studfile.net/preview/16706710/