-
Notifications
You must be signed in to change notification settings - Fork 7
/
kuaiPai.txt
72 lines (66 loc) · 1.37 KB
/
kuaiPai.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package paixu;
public class KuaiPai {
/**
* @param args
*/
public static void main(String[] args) {
int[] arr = {13,4,1,8,5,8,44,11};
quickSort(arr);
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
int[] arr1 = {77,4,7,11,777,444,99,0,22};
quickSort(arr1);
for(int i=0;i<arr1.length;i++){
System.out.print(arr1[i]+" ");
}
}
private static void quickSort(int[] arr){
if(arr==null ||arr.length==0){
return;
}
sort(arr,0,arr.length-1);
}
private static void sort(int[] arr,int low,int high){
int i = low, j = high;
int temp = arr[low]; //枢轴
while(i<j){
while(i<j&&arr[j]>=temp){ //i<j不可以少,不然会报错的
j--;
}
arr[i] = arr[j];
while(i<j&&arr[i]<=temp){
i++;
}
arr[j] = arr[i];
arr[i] = temp;
sort(arr,low,i-1);
sort(arr,i+1,high);
}
}
// public static void quickSort(int[] arr){
// if(arr==null || arr.length==0){
// return;
// }
// sort(arr,0,arr.length-1);
// }
//
// public static void sort(int[] arr,int low,int high){
// int i = low, j = high;
// int temp = arr[low]; //枢轴
// while(i<j){
// while(i<j && arr[j]>=temp){
// j--;
// }
// arr[i] = arr[j];
// while(i<j && arr[i]<=temp){
// i++;
// }
// arr[j] = arr[i];
// arr[i] = temp;
// sort(arr,low,i-1);
// sort(arr,i+1,high);
// }
// }
}