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); } }