public static void main(String[] args) {
int length = 20;
int maxValue = 20;
int[] array = new int[length];
Random r = new Random();
for (int i = 0; i < array.length; i++) {
array[i] = r.nextInt(maxValue);
}
System.out.println(Arrays.toString(array));
quickSort(array, 0, length-1);
System.out.println(Arrays.toString(array));
}
public static void quickSort(int[] array, int leftPoint, int rightPoint) {
if (leftPoint > rightPoint) {
return;
}
int swapTmp = 0;
int baseNumber = array[leftPoint];
int i = leftPoint;
int j = rightPoint;
while (leftPoint != rightPoint) {
while (array[rightPoint] <= baseNumber && leftPoint < rightPoint) {
rightPoint--;
}
while (array[leftPoint] >= baseNumber && leftPoint < rightPoint) {
leftPoint++;
}
if (leftPoint < rightPoint) {
swapTmp = array[leftPoint];
array[leftPoint] = array[rightPoint];
array[rightPoint] = swapTmp;
}
}
array[i] = array[leftPoint];
array[leftPoint] = baseNumber;
quickSort(array, i, leftPoint-1);
quickSort(array, leftPoint+1, j);
}