Question

Unit 4 Assignment 3: Coding Project (Quick Sort) Scenario Use the studentGrades array from task 1....

Unit 4 Assignment 3: Coding Project (Quick Sort)

Scenario

Use the studentGrades array from task 1. Implement a quicksort that will sort the grades highest to lowest and lowest to highest.

Create one method called sortArrayDes(). Implement a quicksort algorithm that will sort from highest to lowest.

Create a second method called sortArrayAsc(). Implement a quicksort algorithm that will sort from lowest to highest.

Create a third method called printArray, which will display the results of both sorts.

Expected Output

Quicksort Descending

98 97 95 90 88 78 75 65 56 55

Quicksort Ascending

55 56 65 75 78 88 90 95 97 98

This is what I have so far (I use C#):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp45
{
public static class QuickSort
{
public static void Sort<T>(T[] array) where T : IComparable
{
Sort(array, 0, array.Length - 1);
}
private static void Sort<T>(T[] array, int lower, int upper) where:
{
if (lower < upper)
{
int p = Partition(array, lower, upper);
Sort(array, lower, p);
Sort(array, p + 1, upper);
}
}
}private static int Partition<T>(T[] array, int lower, int upper) where:
{
int i = lower;
int j = upper;
T pivot = array[lower];

do
{
while (array[i].CompareTo(pivot) < 0) { i++; }
while (array[j].CompareTo(pivot) > 0) { j--; }
if (i >= j) { break; }
Swap(array, i, j);
}
while (i <= j);
return j;
}

}


0 0
Add a comment Improve this question Transcribed image text
Answer #1

using System;
          
public class Program
{
   static public int PartitionAsc(int[] a,int low,int high) {
int pivot;
pivot = a[low];
while (true) {
while (a[low]<pivot) {
low++;
}
while (a[high] >pivot) {
high--;
}
if (low<high) {
int temp = a[high];
a[high] = a[low];
a[low] = temp;
} else {
return high;
}
       }
   }
   static public int PartitionDesc(int[] a,int low,int high) {
int pivot;
pivot = a[low];
while (true) {
while (a[low]>pivot) {
low++;
}
while (a[high] <pivot) {
high--;
}
if (low<high) {
int temp = a[high];
a[high] = a[low];
a[low] = temp;
} else {
return high;
}
       }
   }
  
   static public void sortArrayAsc(int[] a, int low, int high) {
int pivot;
if (low<high) {
pivot = PartitionAsc(a,low,high);
sortArrayAsc(a,low,pivot - 1);
sortArrayAsc(a, pivot+1,high);
}
}
   static public void sortArrayDesc(int[] a, int low, int high) {
int pivot;
if (low<high) {
pivot = PartitionDesc(a,low,high);
sortArrayDesc(a,low,pivot - 1);
sortArrayDesc(a, pivot+1,high);
}
}
   static void printArray(int []a,int low,int high)
   {
       for(int i=low;i<high;i++)
           Console.Write(a[i] + " ");
   }
  
   public static void Main()
   {
       int []a={98,56,90,97,78,95,88,75,65,55};
       Console.Write("Given Array\n \n");
       printArray(a,0,10);
      
       sortArrayAsc(a,0,9);
       Console.Write("\n\nArray in Ascending Order\n \n");
       printArray(a,0,10);
      
       sortArrayDesc(a,0,9);
       Console.Write("\n\nArray in Descending Order \n\n");
       printArray(a,0,10);
      
  
   }  
      
}

Output:

Add a comment
Know the answer?
Add Answer to:
Unit 4 Assignment 3: Coding Project (Quick Sort) Scenario Use the studentGrades array from task 1....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT