快速排序

Posted ren-yu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了快速排序相关的知识,希望对你有一定的参考价值。

 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 }

 

以上是关于快速排序的主要内容,如果未能解决你的问题,请参考以下文章