快速排序

 1 void swap(int &i, int &j)
 2 {
 3     int temp = i;
 4     i = j;
 5     j = temp;
 6 }
 7 
 8 int partition(int a[], int p, int r)
 9 {
10     int x = a[r];
11     int i = p - 1;
12     for (int j = p; j < r; j++) {
13         if (a[j] <= x) {
14             i ++;
15             swap(a[i], a[j]);
16         }
17     }
18     i ++;
19     swap(a[i], a[r]);
20     return i;
21 }
22 
23 void quick_sort(int a[], int p, int r)
24 {
25     if (p < r) {
26         int q = partition(a, p, r);
27         quick_sort(a, p, q - 1);
28         quick_sort(a, q + 1, r);
29     }
30 }

 

posted @ 2019-11-15 21:19  Ren.Yu  阅读(105)  评论(0编辑  收藏  举报