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
I will be sure to upvote!
CODE TO MODIFY(please also add meaningful comment in code):
static void BubbleSort(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; 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; } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
