Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can anyone finsih code here with explain? using namespace std; int kSmall(int k, int anArray[], int first, int last) { return 0; } /* //

Can anyone finsih code here with explain?

using namespace std;

int kSmall(int k, int anArray[], int first, int last)

{

return 0;

}

/*

// Returns the kth smallest value in anArray[first..last].

kSmall(k: integer, anArray: ArrayType,first: integer, last: integer): ValueType

{

//Choose a pivot value p from anArray[first..last] p = 0

//Partition the values of anArray[first..last] about p

if (k < pivotIndex - first + 1)

return kSmall(k, anArray, first, pivotIndex - 1)

else if (k == pivotIndex - first + 1)

return p

else

return kSmall(k - (pivotIndex - first + 1), anArray, pivotIndex + 1, last)

}

//This pseudocode is not far from a C++ function. The only questions that remain

are how to choose the pivot value p and how to partition the array about the chosen

p. The choice of p is arbitrary. Any p in the array will work, although the

sequence of choices will affect how soon you reach the base case. Chapter 11 gives

an algorithm for partitioning the values about p. There you will see how to turn

the function kSmall into a sorting algorithm.

*/

#include

#include

#include

#include "pp04.cpp"

using namespace std;

int const ARRAY_SIZE = 6;

int main()

{

int testArray1[] = {6,3,1,2,4,5};

int position = 4;

cout << "answer is: 4" << endl << "kSmall answer is: " << kSmall(position,

testArray1, 0, ARRAY_SIZE -1) << endl;

int testArray2[] = {20,11,12,-45,7,18};

position = 3;

cout << "answer is: 11" << endl << "kSmall answer is: " << kSmall(position,

testArray2, 0, ARRAY_SIZE -1) << endl;

}

Output:

answer is: 4 kSmall answer is: 4 answer is: 11 kSmall answer is: 11

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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