1
2 public class QuickSort {
3 public static int partition(int[] arr, int low, int high){
4 int pivot = arr[low];
5 while(low<high){
6 while(low<high && arr[high]>=pivot){
7 high--;
8 }
9 if(low<high){
10 arr[low] = arr[high];
11 low++;
12 }
13
14 while(low<high && arr[low]<=pivot){
15 low++;
16 }
17 if(low<high){
18 arr[high] = arr[low];
19 high--;
20 }
21 }
22 arr[low] = pivot;
23 return low;
24 }
25
26 public static void sort(int[] arr, int low, int high){
27 int pivot;
28 if(low<high){
29 pivot = partition(arr,low,high);
30 sort(arr,low,pivot-1);
31 sort(arr,pivot+1,high);
32 }
33 }
34
35 public static void quickSort(int[] arr){
36 sort(arr,0,arr.length-1);
37 }
38
39 /*public static void main(String[] args) {
40 int[] arr = {46,34,2,99,6,23,20,8};
41 quickSort(arr);
42 StringBuilder sb = new StringBuilder();
43 for(int i=0;i<arr.length;i++){
44 sb.append(arr[i]+",");
45 }
46 sb.deleteCharAt(sb.length()-1);
47 System.out.println(sb);
48 }*/
49 }
50