public class QuickSort3Way {
public static void main(String[] args) {
int[] nums = new int[]{10, 8, 9, 4, 2, 7, 6};
sort(nums, 0, nums.length - 1);
System.out.println();
}
public static void sort(int[] input, int lowIndex, int highIndex) {
if (highIndex <= lowIndex) {
return;
}
int lt = lowIndex;
int gt = highIndex;
int index = lowIndex + 1;
int pivotValue = input[lowIndex];
while (index <= gt) {
if (input[index] < pivotValue) {
swap(input, index++, lt++);
} else if (lowIndex > pivotValue) {
swap(input, index, gt--);
} else {
index++;
}
}
sort(input, lowIndex, lt - 1);
sort(input, gt + 1, highIndex);
}
private static void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}