-
Notifications
You must be signed in to change notification settings - Fork 0
/
lanqiao quick sorting
67 lines (45 loc) · 1.07 KB
/
lanqiao quick sorting
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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n=0;
n = in.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++)
a[i] = in.nextInt();
quickSort(a,0,a.length-1);
for(int i=0;i<n;i++) {
System.out.print(a[i]);
System.out.print(" ");
}
}
public static void quickSort(int a[],int start,int end) {
if(start>end)
return;//解决溢出问题
int standard = a[start];
int i=start;
int j = end;
//原来写的是i=start+1; 其实是一样的
//原先写的是i=0; 完全错误
// if(start>end)
// return;
while(i!=j) {
while(a[j]>=standard&&j>i) {
j--;
}
//i = start; 如果该句在这写,不在前面写,那就会陷入死循环,重点。
while(a[i]<=standard&&i<j) {
i++;
}
if(i<j) {
int tempt = a[i];
a[i] = a[j];
a[j] = tempt;
}
}
a[start] = a[i];
a[i] = standard;//很重要
quickSort(a,start,i-1);
quickSort(a,i+1,end);//数组下标溢出
}
}