Question: Modify the merge sort algorithm in C# so it is generic AND will sort the array in reverse. Add a local variable count (make sure
Modify the merge sort algorithm in C# so it is generic AND will sort the array in reverse.
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 GenericReverseMergeSort
Please be sure to add meaningful comment to the code and to follow all the steps! I will be sure to upvote! :)
CODE TO MODIFY:
public static void Merge(int[] arr, int startIdx, int midIdx, int endIdx, int[] tmp) {
int i = startIdx; int j = midIdx + 1; int k = startIdx;
while(i<=midIdx && j<=endIdx) //there are still values in both "halves" { if (arr[i] < arr[j]) { tmp[k] = arr[i]; k++; i++; } else { tmp[k] = arr[j]; k++; j++; } }
while(i <= midIdx) //copy left overs { tmp[k] = arr[i]; k++; i++; }
while (j <= endIdx) { tmp[k] = arr[j]; k++; j++; }
//move values from tmp back onto arr for (i = startIdx; i <= endIdx; i++) arr[i] = tmp[i]; }
static void MergeSort(int[] arr) { int[] tmp = new int[arr.Length];//only created once MergeSortHelper(arr, 0, arr.Length-1, tmp); }
static void MergeSortHelper(int[] arr, int startIdx, int endIdx, int[] tmp) { if(startIdx } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
