1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include <cstdlib>
#include <iostream>
using namespace std;
int kSmall(int k, int anArray[], int first, int last);
int main()
{
int anArray[8]={4,7,3,6,8,1,9,2};
int first = 0;
int last = 7;
int k = anArray[first];
int m = kSmall(k,anArray,first,last);
cout<<m;
system("PAUSE");
return 0;
}
int kSmall (int k, int anArray[], int first, int last)
{
int pivotIndex = 0;
if (k < anArray[(pivotIndex - first +1)])
return kSmall (k, anArray,first, pivotIndex-1);
else if (k == anArray[(pivotIndex -first+1)])
return k;
else
return kSmall (k-(pivotIndex-first+1), anArray, pivotIndex+1, last);
}
|