Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Choose a pivot value p from anArray[first..last]
- Partition the values of anArray[first..last] about p
- Implement the algorithm kSmall, discussed in the class (Chapter 2, Section 2.4.4), as a C++ function. Use the first value of the array as the pivot.
- Requirements:
- For the function, the array parameter MUST BE passed as a pointer.
- Write a main program to test your function.The array that is to be passed to the function is required to be generated as a dynamic array using new operator
- #include <iostream>
- using namespace std;
- int kSmall(int k, int *arrayPtr, int first, int last)
- {
- int pivotIndex = 0;
- int p = arrayPtr[pivotIndex];
- if (k < pivotIndex - first + 1) {
- return kSmall(k, arrayPtr, first, pivotIndex - 1);
- }
- else if (k == pivotIndex - first + 1) {
- return p;
- }
- else {
- return kSmall(k - (pivotIndex - first + 1), arrayPtr,
- pivotIndex + 1, last);
- }
- }
- int main()
- {
- const int SIZE = 5;
- int array[SIZE] = {8,5,3,1,7};
- int* arrayPtr = new int[SIZE];
- arrayPtr = array;
- cout << kSmall(3, arrayPtr, 8, 7) << endl;
- delete arrayPtr;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement