partition:
private static int partition(String[] list, int start, int end) {
String pivot = list[end];
int leftCounter = start;
int rightCounter = end;
while (leftCounter < rightCounter) {
while (list[leftCounter].compareTo(pivot) <= 0 && leftCounter < end && rightCounter > leftCounter) {
leftCounter++;
}
while (list[rightCounter].compareTo(pivot) >= 0 && rightCounter > start && rightCounter >= leftCounter) {
rightCounter--;
}
if (leftCounter < rightCounter) {
swap(list, leftCounter, rightCounter);
}
}
Hoare. [l1, l2, ..., lx, g1, g2, ..., gy, pivot], l1, l2, ..., lx g1, g2, ..., gy . leftCounter g1 rightCounter lx. , :
swap(list, start, end);
return end;
}
. swap(list, leftCounter, end) . [l1, l2, ..., lx, pivot, g2, g3, ..., gy, g1]. , leftCounter.
, : . , .
String[] list = { "a", "c", "a", "b","c","f","g","a","b" }
[a, g, b, a, f, c, c, b, a]