1 int Partition (Type a[], int p, int r)
2 {
3 int i = p, j = r + 1;
4 Type x=a[p];
5 // 將< x的元素交換到左邊區域
6 // 將> x的元素交換到右邊區域
7 while (true) {
8 while (a[++i] <x);
9 while (a[- -j] >x);
10 if (i >= j) break;
11 Swap(a[i], a[j]);
12 }
13 a[p] = a[j];
14 a[j] = x;
15 return j;
16 }
17 void QuickSort (Type a[], int p, int r)
18 {
19 if (p<r) {
20 int q=Partition(a,p,r);
21 QuickSort (a,p,q-1); //對左半段排序
22 QuickSort (a,q+1,r); //對右半段排序
23 }
24 }
posted on 2009-02-06 22:44
混沌的云 閱讀(358)
評論(0) 編輯 收藏 引用