-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksort.cpp
More file actions
62 lines (50 loc) · 773 Bytes
/
Copy pathquicksort.cpp
File metadata and controls
62 lines (50 loc) · 773 Bytes
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
#include <stdio.h>
int a[101],n;
void quicksort(int left,int right){
int i,j,t,temp;
if(left>right)
return;
temp=a[left];
i=left;
j=right;
while (i!=j) {
while (a[j]>=temp && i<j) {
j--;
}
while (a[i]<=temp && i<j) {
i++;
}
if (i<j) {
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
a[left]=a[i];
a[i]=temp;
// for (i = 1; i <=n; ++i) {
// printf("%d ",a[i]);
// }
//跟从做还是从右没有关系
quicksort(left,i-1);
// for (i = 1; i <=n; ++i) {
// printf("%d ",a[i]);
// }
quicksort(i+1,right);
// for (i = 1; i <=n; ++i) {
// printf("%d ",a[i]);
// }
}
int main(void)
{
int i;
scanf("%d",&n);
for (i = 1; i <=n; ++i) {
scanf("%d",&a[i]);
}
quicksort(1,n);
for (i = 1; i <=n; ++i) {
printf("%d ",a[i]);
}
return 0;
}