Question: Using C++, do a partial quicksort using recursion and the function: void partialsort(Vector &a, int k) . . . as k represents the top of
Using C++, do a partial quicksort using recursion and the function:
void partialsort(Vector
as k represents the top of the vector that needs to be sorted. Example:
[3, 6, 2, 10, 5, 8, 8, 1] with k=4 will result in partialsort to give [3, 2, 5, 1, 6 ,8, 8, 10] (this is only an example not exact swap). We do not care about sorting the other end of the vector list, only the bottom half of whatever k is. The partition and quicksort is below:
void partition( vector
choosePivot( a, first, last );
Comparable pivot = a[first];
int lastS1 = first;
int firstUnknown = first + 1;
for ( ; firstUnknown <= last; ++firstUnknown ) {
if ( a[firstUnknown] < pivot ) {
++lastS1;
objectSwap( a[firstUnknown], a[lastS1] );
}
}
// decide a new pivot
objectSwap( a[first], a[lastS1] );
pivotIndex = lastS1;
}
void quicksort( vector
int pivotIndex;
if ( first < last ) {
partition( a, first, last , pivotIndex);
quicksort( a, first, pivotIndex - 1, k);
quicksort( a, pivotIndex + 1, last, k );
}
}
void partialsort( vector
quicksort( a, 0, a.size( ) - 1, k);
}
Only need to modify void quicksort( vector
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
