Question: Modify the quick sort algorithm in C# so it is generic (see our class example!) AND will sort the array in reverse. Also add a

Modify the quick sort algorithm in C# so it is generic (see our class example!) 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 GenericReverseQuickSort(T[] arr) where T : IComparable

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:

static void QuickSort(T[] arr) where T : IComparable { QuickSortHelper(arr, 0, arr.Length - 1); }

static void QuickSortHelper(T[] arr, int startIdx, int endIdx) where T : IComparable { if(startIdx < endIdx) { int q = Partition(arr, startIdx, endIdx); QuickSortHelper(arr, startIdx, q - 1); QuickSortHelper(arr, q + 1, endIdx);

} }

static int Partition(T[] arr, int startIdx, int endIdx) where T : IComparable { T pivot = arr[endIdx]; int i = startIdx - 1;

for(int j = startIdx; j

i++; //swap elem i and last T tmp2 = arr[i]; arr[i] = arr[endIdx]; arr[endIdx] = tmp2;

return i; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!