The task is as follows:

Develop a program for comparing the effectiveness of two sorting algorithms by simultaneously running them on a random array of 500,000 integers (generate random numbers in the range from 0 to 1000,000). To improve the accuracy of the comparison, sort each of the algorithms at least 10 times. Results averaged.

The problem is that the program sorts the array at the first iteration, and then again it passes by the sorted one. How do I need to fix the code so that after each iteration I have the sorted array replaced by the source one for re-sorting? Here is the code:

class Program { static void Main(string[] args) { int length = Convert.ToInt32(Console.ReadLine()); int[] arr = new int[length]; ArrayRandomizer(arr); int[] copyarr = new int[length]; for (int i = 0; i < arr.Length; i++) { copyarr[i] = arr[i]; } Thread t1 = new Thread(new ParameterizedThreadStart(shellSort)); t1.Start(arr); Thread t2 = new Thread(new ParameterizedThreadStart(BubbleSort)); t2.Start(copyarr); Console.Read(); } static int[] ArrayRandomizer(int[] Arr) { Random rand = new Random(); for (int i = 0; i < Arr.Length; i++) { Arr[i] = rand.Next(0, 1000000); } return Arr; } static void shellSort(object ara) { int[] arr = (int[])ara; Stopwatch sw = new Stopwatch(); sw.Start(); double[] exectime = new double[10]; double result=0; int index = -1; for (int k = 0; k < 10; k++) { int j; int step = arr.Length / 2; while (step > 0) { for (int i = 0; i < (arr.Length - step); i++) { j = i; while ((j >= 0) && (arr[j] > arr[j + step])) { int tmp = arr[j]; arr[j] = arr[j + step]; arr[j + step] = tmp; j -= step; } } step = step / 2; } sw.Stop(); Console.WriteLine("ΠΏΠΎΡ‚ΠΎΠΊ с сортировкой шСлла: " + (sw.ElapsedMilliseconds) + " миллисСкунд"); index++; exectime[index] = sw.ElapsedMilliseconds; sw.Reset(); } for (int n = 0; n < exectime.Length; n++) { result += exectime[n]; } Console.WriteLine("Π‘Ρ€Π΅Π΄Π½Π΅Π΅ врСмя выполнСния шСлла: " + result / exectime.Length); Thread.Sleep(0); } static void BubbleSort(object copyara) { int[] arr = (int[])copyara; Stopwatch sw = new Stopwatch(); sw.Start(); double[] exectime = new double[10]; double result = 0; int index = -1; for (int m = 0; m < 10; m++) { for (int i = 0; i < arr.Length; i++) { for (int j = 0; j < arr.Length - i - 1; j++) { if ((arr[j] > arr[j + 1])) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } sw.Stop(); Console.WriteLine("ΠΏΠΎΡ‚ΠΎΠΊ с сортировкой ΠΏΡƒΠ·Ρ‹Ρ€ΡŒΠΊΠΎΠΌ: " + (sw.ElapsedMilliseconds) + " миллисСкунд"); index++; exectime[index] = sw.ElapsedMilliseconds; sw.Reset(); } for (int n = 0; n < exectime.Length; n++) { result += exectime[n]; } Console.WriteLine("Π‘Ρ€Π΅Π΄Π½Π΅Π΅ врСмя выполнСния ΠΏΡƒΠ·Ρ‹Ρ€ΡŒΠΊΠ°: " + result / exectime.Length); Thread.Sleep(0); } } 
  • one
    In my opinion, doing a test of two different algorithms at the same time is wrong: arrays of 500,000 elements each will not fit into the processor's cache memory, which means that the data will be pulled from the main memory during operation. And two simultaneously running streams will interfere with each other. That is, the memory bandwidth may be decisive. So test your algorithms consistently. - Alexander Petrov
  • one
    I would change the title of the question to "Comparing sorting by time" or something like that, to match the question - rdorn

2 answers 2

So that the use of arrays does not affect the measurements at all, then not one array can be transferred to the input, but an array of 10 identical arrays, which, incidentally, may require extra memory.

Accordingly, you can copy an array before each run, for example, Array.Copy () - it is better to use it at the beginning instead of a cycle. The study https://stackoverflow.com/questions/5099604/any-faster-way-of-copying-arrays-in-c says that this method works with the speed of copying memory. At the time of copying the array Stopwatch can be turned off.

    As an alternative to the proposed option, if you are limited in memory, you can remember that Random not a real RNG and depends on the initial value (seed). If you specify in the constructor new Random(seed) before generating the sequence, the sequence will always be generated the same. This works more slowly than copying, but reduces the memory consumption of up to one array to all the investigated sorting methods.

    StopWatch turned on strictly before calling the method under study and turned off after it has been completed.

    Read about time measurement features here, if you haven’t read it of course https://ru.stackoverflow.com/a/547742/198316

    And forget about streams if you need normal measurements, they only work in theory in parallel, in practice, they can and will interfere with each other, especially on long operations with big data. Despite the number of cores in modern processors, they at least have a shared level 3 cache, which will have to be constantly re-read between threads, and there is no guarantee that threads will work on different cores, they may well be running on one core. OS discretion.

    • By the way, a good idea with a seed . - Alexander Petrov