Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can anyone please fix this program? #include using namespace std; const int SIZE = 5; int kSmall(int k, const int anArray[], int first, int last);

Can anyone please fix this program?

#include

using namespace std;

const int SIZE = 5;

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

void partition(int anArray[], int size, int pivotValue, int& pivotIndex);

int main()

{

return 0;

}

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

{

int pivotIndex = first;

int p = anArray[first];

partition(anArray, SIZE, int pivotValue, pivotIndex);

if(k

{

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);

}

}

void partition(int anArray[], int first, int last, int& pivotIndex)

{

int pivot = anArray[first];

int lastS1 = first;

int firstUnknown = first + 1;

for( firstUnknown

{

if(anArray[firstUnknown]

{

++lastS1;

swap(anArray[firstUnknown], anArray[lastS1]);

}

}

swap(anArray[first], anArray[lastS1]);

pivotIndex = lastS1;

}image text in transcribed

/Returns the kth smallest value in anArray[first..last] kSmall(k: integer, anArray: ArrayType, irst: integer, last: integer): ValueType Choose a pivot value p from anArray[first..last] Partition the values of anArray[firstlast] about p if (k

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_2

Step: 3

blur-text-image_3

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