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
PLEASE ALSO ADD MEANINFUL COMMENT TO THE CODE. WILL UPVOTE
CODE TO MODIFY:
using System.Collections.Concurrent; using System.Diagnostics;
namespace CSC340._515_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");
}
static void PopulateRandomValues(int[] arr1, int[] arr2, int[] arr3, int[] arr4) { Random randGen = new Random();
for(int i=0; i //arr1[arr1.Length / 2] = arr2[arr1.Length / 2] = arr3[arr1.Length / 2] = arr4[arr1.Length / 2] = 2023;//almost sorted } //running time O(n) static void DisplayArray static void BubbleSort2(int[] arr)//O(n^2) best and worst case { for (int j = 0; j < arr.Length - 1; j++)//O(n^2) { //traverse the array for (int i = 0; i < arr.Length - 1- j; i++) //O(n) { if (arr[i] > arr[i + 1]) //O(1) { //swap int tmp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = tmp; } } } } static void BubbleSort3(int[] arr)//O(n^2) worst case, big omega(n) best case { for (int j = 0; j < arr.Length - 1; j++)//O(n^2) { bool performedSwapsFlag = false; //traverse the array for (int i = 0; i < arr.Length - 1 - j; i++) //O(n) { if (arr[i] > arr[i + 1]) //O(1) { //swap int tmp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = tmp; performedSwapsFlag = true;//set the flags } } if(performedSwapsFlag == false)//if no swaps { break; // return; - we are done early, the array is sorted } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started