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 &a, int k) . . .

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 &a, int first, int last, int &pivotIndex ){

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 &a, int first, int last, int k) {

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 &a, int k ) {

quicksort( a, 0, a.size( ) - 1, k);

}

Only need to modify void quicksort( vector &a, int first, int last, int k) and or partialsort.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!