标签:
1 /*
2 用zstu3539题目来验证算法的正确性
3 */
4 #include <cstdio>
5 #include <iostream>
6 #include <algorithm>
7 #include <ctime>
8 #include <cstdlib>
9 using namespace std;
10
11 const int maxn = 1000000 + 10;
12 const int INF = 0x3f3f3f3f;
13 int a[maxn];
14
15 int Partition(int *a, int p, int r)
16 {
17 int x = a[r];
18 int i = p;
19 for (int j=p; j<r; ++j)
20 {
21 if (a[j] <= x)
22 {
23 swap (a[i++], a[j]);
24 }
25 }
26 swap (a[i], a[r]);
27
28 return i;
29 }
30
31 void QuickSort(int *a, int p, int r)
32 {
33 if (p < r)
34 {
35 int q = Partition (a, p, r);
36 QuickSort (a, p, q-1);
37 QuickSort (a, q+1, r);
38 }
39 }
40
41 int main(void)
42 {
43 freopen ("sort.in", "r", stdin);
44 int n;
45
46 while (scanf ("%d", &n) != EOF)
47 {
48 for (int i=1; i<=n; ++i)
49 {
50 scanf ("%d", &a[i]);
51 }
52
53 QuickSort (a, 1, n);
54
55 for (int i=1; i<=n; ++i)
56 {
57 if (i == n) printf ("%d\n", a[i]);
58 else printf ("%d ", a[i]);
59 }
60 }
61
62 return 0;
63 }
标签:
原文地址:http://www.cnblogs.com/Running-Time/p/4354617.html