1 public class QuickSort {
2 public static void quickSort(int[] list) {
3 quickSort(list, 0, list.length - 1);
4 }
5
6 private static void quickSort(int[] list, int first, int last) {
7 if (last > first) {
8 int pivotIndex = partition(list, first, last);
9 quickSort(list, first, pivotIndex - 1);
10 quickSort(list, pivotIndex + 1, last);
11 }
12 }
13
14
15 private static int partition(int[] list, int first, int last) {
16 int pivot = list[first];
17 int low = first + 1;
18 int high = last;
19
20 while (high > low) {
21
22 while (low <= high && list[low] <= pivot)
23 low++;
24
25
26 while (low <= high && list[high] > pivot)
27 high--;
28
29
30 if (high > low) {
31 int temp = list[high];
32 list[high] = list[low];
33 list[low] = temp;
34 }
35 }
36
37 while (high > first && list[high] >= pivot)
38 high--;
39
40
41 if (pivot > list[high]) {
42 list[first] = list[high];
43 list[high] = pivot;
44 return high;
45 }
46 else {
47 return first;
48 }
49 }
50
51
52 public static void main(String[] args) {
53 int[] list = {2, 3, 2, 5, 6, 1, -2, 3, 14, 12};
54 quickSort(list);
55 for (int i = 0; i < list.length; i++)
56 System.out.print(list[i] + " ");
57 }
58 }