Question
C# add a local variable count (use long count) of the number of comparisons that were performed. Display it before exiting this method. Call it:
C#
add a local variable count (use long count) of the number of comparisons that were performed. Display it before exiting this method. Call it: static void MergeReverseSort(string[] arr)
static void MergeSort(string[] arr) { string[] tmp = new string[arr.Length]; MergeSort(arr, 0, arr.Length - 1, tmp); }
static void MergeSort(string[] arr, int startPosition, int endPosition, string[] tmp) { long count = 0; if (startPosition < endPosition) {
int middlePosition = (startPosition + endPosition) / 2; MergeSort(arr, startPosition, middlePosition, tmp); MergeSort(arr, middlePosition + 1, endPosition, tmp); MergeSubArrays(arr, startPosition, middlePosition, endPosition, tmp); } }
static void MergeSubArrays(string[] arr, int startPosition, int middlePosition, int endPosition, string[] tmp) {
int i = startPosition; int j = middlePosition + 1; int k = startPosition;
while (i <= middlePosition && j <= endPosition) { if (arr[i].CompareTo(arr[j]) > 0) { tmp[k] = arr[i]; i++; k++; } else { tmp[k] = arr[j]; j++; k++; } }
while (i <= middlePosition) { tmp[k] = arr[i]; i++; k++; }
while (j <= endPosition) { tmp[k] = arr[j]; j++; k++; }
for (int p = startPosition; p <= endPosition; p++) { arr[p] = tmp[p]; } }
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