Question: Modify the bubble sort algorithm in C# so it is generic AND will sort the array in reverse. Also add a local variable count (make
Modify the bubble sort algorithm in C# so it is generic AND will sort the array in reverse. Also add a local variable count (make sure to use the type long) that will count the number of elements comparisons that were performed for this sorting. Display the value of count before exiting this method. The method should have the following header: static void GenericReverseBubbleSort
Here is the code to modify:
using System.Collections.Concurrent; using System.Diagnostics;
namespace Sorting { internal class Program { static void Main(string[] args) //O(n^2) { /* int[] values = { 7, 3, 21, 9, 14, 15, 10};
//BubbleSort(values);//O(n^2) //DisplayArray(values);//O(n)
//BubbleSort2(values); MergeSort2(values); DisplayArray(values);//O(n)
string[] words = { "Saint", "Martin", "University"}; MergeSort2(words); DisplayArray(words);
int[] values2 = { 7, 3, 21, 9, 14, 15, 10 , 1}; QuickSort(values2); DisplayArray(values2);//O(n) */
int size = 500_000; int[] arr1 = new int[size]; int[] arr2 = new int[size]; int[] arr3 = new int[size]; int[] arr4 = new int[size];
PopulateRandomValues(arr1, arr2, arr3, arr4);
Stopwatch watch = new Stopwatch();
watch.Start(); QuickSort(arr4); watch.Stop(); Console.WriteLine($"Quicksort took {watch.ElapsedMilliseconds} ms to sort {size} elements");
watch.Restart(); SelectionSort(arr2); watch.Stop(); Console.WriteLine($"Selectionsort took {watch.ElapsedMilliseconds} ms to sort {size} elements");
watch.Restart(); BubbleSort3(arr1); watch.Stop(); Console.WriteLine($"Bubblesort took {watch.ElapsedMilliseconds} ms to sort {size} elements");
//watch.Restart(); //MergeSort2(arr3); //watch.Stop(); //Console.WriteLine($"Mergesort took {watch.ElapsedMilliseconds} ms to sort {size} elements");
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
