Question: In C# sing the below code, in Main read the entries given in a input.txt (one line per entry) and store them into an array.

In C# sing the below code, in Main read the entries given in a input.txt (one line per entry) and store them into an array. Make four copies of it. Then, on each copy call selectionReverseSort, mergeReverseSort. For each of these measure the execution time (how long it took to run the reverse sorting) and display this time. 

using System;

namespace SimpleSort { class Sort

{

static void Main(string[] args)

{ string[] ary = {

}; Console.WriteLine("Selection Sort in reverse order"); selectionReverseSort(ary);

Console.WriteLine(" Merge Sort in reverse order"); mergeReverseSort(ary);

Console.ReadKey();

}

static void selectionReverseSort(string[] arr) { int n = arr.Length; long count = 0;

for (int i = 0; i < n - 1; i++) { int mIndex = i;

for (int j = i + 1; j < n; j++) { // reverse order if (String.Compare(arr[j], arr[mIndex]) > 0) { mIndex = j; count++; } }

string tmp = arr[mIndex]; arr[mIndex] = arr[i]; arr[i] = tmp; }

Console.WriteLine("Number of Comparision is " + count); }

static void mergeReverseSort(string[] arr) { int count = mergereversesort(arr, 0, arr.Length - 1);

Console.WriteLine("Number of Comparision is " + count); }

static int mergereversesort(string[] A, int l, int r) { int c = 0; if (l < r) { int midpoint = (l + r) / 2; //keep in mind this is integer division c += mergereversesort(A, l, midpoint); c += mergereversesort(A, midpoint + 1, r); c += mergereverse(A, l, midpoint, r);

} return c; }

static int mergereverse(string[] A, int l, int midpoint, int r) { int c = 0; int i, j, k; int n1 = midpoint - l + 1; int n2 = r - midpoint;

string[] L = new string[n1]; string[] R = new string[n2];

for (i = 0; i < n1; i++) { L[i] = A[l + i]; }

for (j = 0; j < n2; j++) { R[j] = A[midpoint + 1 + j]; }

i = 0; // Initial index of first subarray j = 0; // Initial index of second subarray k = l; // Initial index of merged subarray

while (i < n1 & j < n2) { c++;

// reverse order if (String.Compare(L[i], R[j]) > 0) { A[k] = L[i]; i++; } else { A[k] = R[j]; j++; } k++; }

while (i < n1) { A[k] = L[i]; i++; k++; }

return (c); } } }

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!