C++ language question Hello, I have a problem changing this quicksort algorithm
ID: 3711331 • Letter: C
Question
C++ language question
Hello, I have a problem changing this quicksort algorithm from the current (mostly sorted ascending) order to ( mostly sorted descending) order of array values. The algorithm is supposed to take an array and sorted them in mostly descending order and measure the time taken to do that.
This algorithm below works well for (mostly ascending order). But I want to reverse this algorithm for (Mostly descending order) so that it sorts the array in the most descending order.
2 I/ swaps two numbers in an array 3 void swap(float* a, float* b) float ta *a = *b; *b = t; 45 47 49 0 int partition (float arrll, int low, int high) 51 52 int pivot = arr[high); // pivot int ? (low-1); // Index of smaller element 54 55 for (int j = low; jExplanation / Answer
void swap(float *a, float *b)
{
float t = *a;
*a = *b;
*b = t;
}
int partition(float arr[], int low, int high)
{
int pivot = arr[high];
int i = (low -1);
for(int j = low; j <= high -1; j++)
{
if (arr[j] > pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i+1], &arr[high]);
return (i+1);
}
void quickSort(float arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi-1);
quickSort(arr, pi+1, high);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.