首页 > 代码库 > 快速排序

快速排序

技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 int a[100];
 5 void qsort(int *s,int *e)
 6 {
 7     if (s>=e) return;
 8     int mid = *(s+(e-s)/2);
 9     int * l = s;
10     int * r = e;
11     int i = 0;
12     while (l<r)
13     {
14         while ((*l)<mid&&l<=r) l++;
15         while ((*r)>mid&&l<=r) r--;
16         if (l<=r)
17         {
18         int t = *(l);
19         *l = *r;
20         *r = t;
21         l++;r--;
22         }
23     }
24     qsort(l,e);
25     qsort(s,r);
26     return;
27 }
28 int main()
29 {
30     int n = 0 ;
31     cin >> n;
32     for (int i = 0; i< n ;i++) cin >> a[i];
33 
34     qsort(a,a+n-1);
35 
36     for (int i = 0; i<n ;i++)
37         cout <<a[i]<<" ";
38     cout <<endl;
39     return 0;
40 }
Quick_Sort

 

快速排序