For a homework problem, I need to write a Java method to find the kth smallest number in an array, using quicksort-style partitioning. I was given the partition() method, and I’m supposed to write the method to get the kth smallest number.
The problem asks that the pivot always be the rightmost element in the array range.
I was given the following:
public int partition(int left, int right, long pivot) {
int leftPtr = left - 1;
int rightPtr = right + 1;
while (true) {
while (leftPtr < right && array[++leftPtr] < pivot);
while (rightPtr > left && array[--rightPtr] > pivot);
if (leftPtr >= rightPtr)
break;
else
swap(leftPtr, rightPtr);
}
return leftPtr;
}
And I’ve written this method based on Wikipedia’s pseudocode:
public int kthSmallest(int left, int right, int k){
if(left >= right){
return array[left];
}
int pivotNewIndex = partition(left, right, array[right]);
int pivotDist = pivotNewIndex - left - 1;
if(pivotDist == k){
return array[pivotNewIndex];
} else if(k < pivotNewIndex){
return kthSmallest(k, left, pivotNewIndex - 1);
} else{
return kthSmallest(k - pivotDist, pivotNewIndex + 1, right);
}
}
But when I call kthSmallest() with randomly generated arrays of integers, about half the time it returns the wrong value. For example:
array: [45, 60, 24, 82, 87, 79, 16, 32, 59, 83, 20, 2, 1,
50, 11, 79, 72, 32, 0, 48, 69, 74, 22, 6, 96]
expected result when k=4: 11
actual result when k=4: 87
What am I doing wrong?
Your recursive calls have the argument list in the wrong order. The position in the subarray that you’re looking at should be the third argument, not the first.