Question
Program C++ I want to use the quicksort algorithm below to sort by using the first element to compare againt other and put in descending
Program C++
I want to use the quicksort algorithm below to sort by using the first element to compare againt other and put in descending order. So if the number is less than the first element move it to the right and greater than move to the left. All numbers should be compared against the same elment which is the first element.
int partition(int *a,int p,int r) { int q = p-1, i; //First Element int temp = 0; int pivot = a[0];
for (i = p; i < r; i++) { if (a[i] >= a[r]) //descending or acsending (change greater/less than sign) { ++q; temp = a[q]; a[q] = a[i]; a[i] = temp; } }
++q;
temp = a[q]; a[q] = a[r]; a[r] = temp;
return q;
}
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