Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a partition function that is a part of the quicksort method. Correct answer gets upvote immediately The function to make must use the previous

Create a partition function that is a part of the quicksort method. Correct answer gets upvote immediately

The function to make must use the previous function findAndSwapPivot or/and swapListValues when swapping values in place in the list of integers.

Assume that the input array contains k values less than the pivot. The records are then rearranged in such a way that the k values less than the pivot are placed in the first, or leftmost, k positions in the array, and the values greater than or equal to the pivot are placed in the last, or rightmost, nk positions.

The values placed in a given partition need not (and typically will not) be sorted with respect to each other. All that is required is that all values end up in the correct partition.

The pivot value itself is placed in position k.

Function needs to correctly return the index where described as k where pivot value should actually go.

#include #include #include #include using namespace std;

class quicksort { private: int left; int right; int* array; public: void swapListValues(int* array, int left, int right ); int findAndSwapPivot(int* array, int left, int right ); int partitionList(int* array, int left, int right, ); //1 more parameter must be in this function. The pivot value };

void quicksort::swapListValues(int* array, int left, int right ) { int temp = array[left]; array[left] = array[right]; array[right] = temp; }

int quicksort::findAndSwapPivot(int* array, int left, int right ) { int temp = (right + left) / 2; int pivot = array[temp];

swapListValues(array, temp, right );

return pivot; }

Partition function to code.

int partitionList(int* array, int left, int right,??? ); { }

int main(int argc, char** argv) {

const int MAX_TEST_SIZE = 100; int testvals[MAX_TEST_SIZE]; int expected[MAX_TEST_SIZE]; int length;

quicksort sortAndSearch;

cout << "Test partitionList() -------------------------------------------" << endl; int expectedPivotIndex; int actualPivotIndex;

length = 10; memcpy(testvals, (const int[]){8, 3, 7, 6, 9, 2, 4, 8, 1, 5}, sizeof(int) * length); memcpy(expected, (const int[]){1, 3, 4, 2, 9, 6, 7, 8, 8, 5}, sizeof(int) * length); expectedPivotIndex = 4; // actualPivotIndex = sortAndSearch.partitionList(testvals, 0, length-2, testvals[length-1]); // cout << "partitionList() test 1, basic test" << endl // << " expected: " << tostring(expected, length) << endl // << " expected pivot: " << expectedPivotIndex <

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions