I am trying to understand how QuickSelect partitioning works, and there are a few things I don't get, I will try to explain how I see it (please notice that I have renamed the variables and made my comments to try to understand it, so maybe some renaming or commenting is wrong):
- First, the value of the pivot is the value of the index the pivot is at, that makes sense.
- We now swap the pivot to the end of the Array, why?
- We have a first pointer which starts at left, because the first pointer should of course start at the start.
- In the for loop we have a second pointer, which also starts at left, why?. Shouldn't it start at the other end?
- If where we are at is less than the pivot value, swap them, so we get the lesser elements to the left.
- At the end swap the pivot back (this leads to my fist "why").
- At the end we return the first pointer, which I assume is because that is the only element left in the Array?
I have seen different kinds of implementations, and I have found that most if not all do this.
// Partitioning.
private static int quickSelectPartition(int[] array, int left, int right, int pivotIndex) {
// The value of the pivot depends on the value at the random index that we got.
int pivotValue = array[pivotIndex];
// Move the pivot to the end.
swapLeftWithRight(array, pivotIndex, right);
// First pointer starts from left.
int firstPointer = left;
// Second pointer starts from left.
for(int secondPointer = left; secondPointer < right; secondPointer++) {
// If the value at the second pointer is less than pivot value, swap it to where the first pointer is.
if(array[secondPointer] < pivotValue) {
// Swap.
swapLeftWithRight(array, firstPointer, secondPointer);
// Move the first pointer forward.
firstPointer++;
}
}
// When done with this partitioning, swap the pivot back to its original position.
swapLeftWithRight(array, right, firstPointer);
return firstPointer;
}