private void InsertionSortButton_Click(object sender, EventArgs e) { var insertion = new InsertSort <SortedItem>(items); Button_Click(insertion); }
private void InsertSortBut_Click(object sender, EventArgs e) { var insert = new InsertSort <SortedItem>(items); Btn_Click(insert); }
static void Main(string[] args) { Random random = new Random(); Console.WriteLine("Enter the Number"); int c = Console.Read(); int[] arr = new int[c]; for (int i = 0; i < c; i++) { arr[i] = random.Next(); } Console.WriteLine("Choose The Algorhitm "); Console.WriteLine("1: InsertSort"); Console.WriteLine("2: BubbleSort"); Console.WriteLine("3: Quick Sort"); Console.WriteLine("4: Heap Sort"); Console.WriteLine("5: Merge Sort"); Console.WriteLine("6: All"); string s = Console.ReadLine(); if (s.Length > 1) { for (int i = Convert.ToInt32(s.Substring(0, 1)); i <= Convert.ToInt32(s.Substring(2, 3)); i++) { switch (Convert.ToString(i)) { case "1": InsertSort.Sort(arr); break; case "2": BubbleSort.Sort(arr); break; case "3": QuickSort.Sort(arr, 0, arr.Length - 1); break; case "4": HeapSort.Sort(ref arr); break; case "5": MergeSort.Sort(ref arr, 0, arr.Length - 1); break; } } } else { switch (s) { case "1": InsertSort.Sort(arr); break; case "2": BubbleSort.Sort(arr); break; case "3": QuickSort.Sort(arr, 0, arr.Length - 1); break; case "4": HeapSort.Sort(ref arr); break; case "5": MergeSort.Sort(ref arr, 0, arr.Length - 1); break; case "6": InsertSort.Sort(arr); BubbleSort.Sort(arr); QuickSort.Sort(arr, 0, arr.Length - 1); HeapSort.Sort(ref arr); MergeSort.Sort(ref arr, 0, arr.Length - 1); break; } } Console.ReadLine(); }
/// <summary> /// Sort the specified array with the algorithm of index i. /// </summary> /// <returns>The sort.</returns> /// <param name="a">The array of integers.</param> /// <param name="i">The index of the algorithm.</param> static void sort(int[] a, int i) { switch (i) { // Bubble Sort. case 1: // Stores the memory allocated at the moment. var mem1 = GC.GetTotalMemory(false); // Starts a new stopwatch. var watch = System.Diagnostics.Stopwatch.StartNew(); // Calls the Bubble Sort algorithm. BubbleSort.sort(a); // Stops the stopwatch. watch.Stop(); // Gets the memory allocated at the moment and sumtracts the // older one to get the memory used in the intermediate process. var mem2 = GC.GetTotalMemory(false) - mem1; // Writes the memory usage in the index of its algorithm in the memory array. MainClass.mem[i] = mem2; // Gets the value of the stopwatch and stores itin the running time array. double time = watch.ElapsedTicks; MainClass.eff[i] = time; break; // Similarly the other cases. case 2: mem1 = GC.GetTotalMemory(false); watch = System.Diagnostics.Stopwatch.StartNew(); InsertSort.sort(a); watch.Stop(); mem2 = GC.GetTotalMemory(false) - mem1; MainClass.mem[i] = mem2; time = watch.ElapsedTicks; MainClass.eff[i] = time; break; case 3: mem1 = GC.GetTotalMemory(false); watch = System.Diagnostics.Stopwatch.StartNew(); QuickSort.sort(a); watch.Stop(); mem2 = GC.GetTotalMemory(false) - mem1; MainClass.mem[i] = mem2; time = watch.ElapsedTicks; MainClass.eff[i] = time; break; case 4: mem1 = GC.GetTotalMemory(false); watch = System.Diagnostics.Stopwatch.StartNew(); HeapSort.sort(a); watch.Stop(); mem2 = GC.GetTotalMemory(false) - mem1; MainClass.mem[i] = mem2; time = watch.ElapsedTicks; MainClass.eff[i] = time; break; case 5: mem1 = GC.GetTotalMemory(false); watch = System.Diagnostics.Stopwatch.StartNew(); MergeSort.sort(a); watch.Stop(); mem2 = GC.GetTotalMemory(false) - mem1; MainClass.mem[i] = mem2; time = watch.ElapsedTicks; MainClass.eff[i] = time; break; } }