码迷,mamicode.com
首页 > 编程语言 > 详细

排序算法之快速排序

时间:2015-03-20 21:57:05      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!