Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please edit this code in C# Please generate the method GenericMergeSort and GenericReverseSort so that it can be used in the current context. I will

Please edit this code in C#

Please generate the method GenericMergeSort and GenericReverseSort so that it can be used in the current context.

I will be sure to upvote!

CODE TO MODIFY:

using System;

class Program { static void Main(string[] args) { // create some random arrays of size at least 20 string[] arr1 = new string[] { "apple", "banana", "cherry", "date", "elderberry", "fig", "grape", "honeydew", "imbe", "jackfruit", "kiwi", "lemon", "mango", "nectarine", "orange", "pear", "quince", "raspberry", "strawberry", "tangerine" }; string[] arr2 = new string[] { "Alice", "Bob", "Charlie", "David", "Emily", "Frank", "Gina", "Haley", "Isaac", "Jack", "Katie", "Liam", "Mia", "Nate", "Olivia", "Parker", "Quinn", "Ryan", "Sarah", "Tyler" }; string[] arr3 = new string[] { "bird", "cat", "dog", "elephant", "fox", "giraffe", "horse", "iguana", "jaguar", "kangaroo", "lemur", "monkey", "newt", "otter", "panda", "quail", "rhino", "snake", "toucan", "unicorn" };

// call the four sorting methods and display the number of comparisons performed int numComparisons1 = GenericReverseBubbleSort(arr1); Console.WriteLine($"Bubble sort comparisons: {numComparisons1}");

int numComparisons2 = GenericReverseSelectionSort(arr2); Console.WriteLine($"Selection sort comparisons: {numComparisons2}");

int numComparisons3 = GenericReverseMergeSort(arr3); Console.WriteLine($"Merge sort comparisons: {numComparisons3}");

int numComparisons4 = GenericReverseQuickSort(arr1); Console.WriteLine($"Quick sort comparisons: {numComparisons4}"); }

// implementation of generic reverse bubble sort static int GenericReverseBubbleSort(T[] arr) where T : IComparable { int numComparisons = 0; int n = arr.Length;

for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j].CompareTo(arr[j + 1]) < 0) { // swap arr[j] and arr[j+1] T temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp;

numComparisons++; } } }

return numComparisons; }

// implementation of generic reverse selection sort static int GenericReverseSelectionSort(T[] arr) where T : IComparable { int numComparisons = 0; int n = arr.Length;

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

for (int j = i + 1; j < n; j++) { if (arr[j].CompareTo(arr[maxIndex]) > 0) { maxIndex = j; }

numComparisons++; }

// swap arr[i] and arr[maxIndex] T temp = arr[i]; arr[i] = arr[maxIndex]; arr[maxIndex] = temp; }

return numComparisons; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Temporal Databases Research And Practice Lncs 1399

Authors: Opher Etzion ,Sushil Jajodia ,Suryanarayana Sripada

1st Edition

3540645195, 978-3540645191

More Books

Students also viewed these Databases questions