1. 程式人生 > >快速排序的穩定化實現

快速排序的穩定化實現

穩定排序的概念:
保證排序前2個相等的數其在序列的前後位置順序和排序後它們兩個的前後位置順序相同。
例如:a[i]==a[j]&&i

通常的快速排序為什麼不穩定?

快速排序初始的版本:

int partition(vector<int>&a, int s, int e) {
    if (s < e) {
        int low = s, high = e, key = a[s];
        while (low < high) {

            while (low < high && a[high] >= key)
                high--;
            if
(low < high) { a[low++] = a[high];//將比key小的元素移到低端 } while (low < high && a[low] < key) low++; if (low < high) { a[high--] = a[low];//將比key大的元素移到高階 } } a[low] = key; return
low; } return -1; } void quick_sort(vector<int> & a,int s, int e) { if (s < e) { int k = partition(a, s, e); quick_sort(a, s, k); quick_sort(a, k + 1,e); } }

看看交換導致的不穩定:
以下是初始序列(第一行是在原始陣列的下標,第二行是元素值,若有第三行是當前陣列的下標):

0 1 2 3 4 5 6 7 8 9
34 21 53 8 78 123 21 53 34 111
0 1 2 3 4 5 6 7 8 9

說明:low,high指的下標都是第三行當前陣列內的下標。

1)以34為key進行劃分,low=0,high=9:

0 1 2 3 4 5 6 7 8 9
34 21 53 8 78 123 21 53 34 111
0 1 2 3 4 5 6 7 8 9

找到第一個要交換到低端的元素21,low=0,high=6,將它換到0位置後,low=1,high=6;

6 1 2 3 4 5 6 7 8 9
21 21 53 8 78 123 21 53 34 111
0 1 2 3 4 5 6 7 8 9

將位於6位置的21換到首位時,這時已經不是穩定的了,因為它位於a[1]=21的前面;

從low=1開始向後找,下面在找需要移到高階的元素位於low=2的53,low=2,將它換到6位置:

6 1 2 3 4 5 6 7 8 9
21 21 53 8 78 123 21 53 34 111
0 1 2 3 4 5 6 7 8 9

交換後:

6 1 2 3 4 5 2 7 8 9
21 21 53 8 78 123 53 53 34 111
0 1 2 3 4 5 6 7 8 9

此時,high=5,low=2;

從high=5開始往前找,遇到a[3]=8停止,將a[3]換到low=2的位置。

6 1 3 3 4 5 2 7 8 9
21 21 8 8 78 123 53 53 34 111
0 1 2 3 4 5 6 7 8 9

此時,low=high=3結束;
最後,將key放到a[low],完成了一次劃分。

6 1 3 0 4 5 2 7 8 9
21 21 8 34 78 123 53 53 34 111
0 1 2 3 4 5 6 7 8 9

因此,上面劃分時的交換不能保證穩定性。

改進:穩定版

在交換是用額外的空間儲存要交換的資料,具體:

int stable_partition(vector<int>& a, int s, int e) {
    if (s >= e) return s;
    vector<int> b(a.size());
    int pa1, pa2, pb1, pb2;
    pa1 = pb1 = s; pa2 = pb2 = e;
    int start = s, end = e;
    int key = a[s];
    while (pa1 < pa2) {
        while (pa1 < pa2 && a[pa1] < key)
        {
             a[s++]= a[pa1++];
        }

        while (pa1 < pa2 && a[pa2] >= key) {
             a[e--]= a[pa2--] ;
        }
        if (pa1 < pa2) {
            b[pb1++]= a[pa1++];
            b[pb2--]= a[pa2--] ;
        }
    }
    int i;
    for (i = pb2 + 1; i <= end; i++)
        a[s++] = b[i];
    int idx = pa1;
    for (i = start; i < pb1; i++)
        a[s++] = b[i];

    return idx;
}

仍然以上面的序列為例:
這裡用原陣列a儲存不需要交換位置的元素,如果要發生交換,放到b中。

它的一次劃分過程如下,以key=a[1]=34為主元:
a陣列:

0 1 2 3 4 5 6 7 8 9
34 21 53 8 78 123 21 53 34 111
0 1 2 3 4 5 6 7 8 9

b陣列:

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9

pa1=0開始,pa2=9開始,發現當pa1=0,pa2=6時需要交換;
此時,不進行交換,而是將pa1的資料放到b的首端,pa2的資料放到末端:
a陣列:

0 1 2 3 4 5 6 7 8 9
34 21 53 8 78 123 21 53 34 111
0 1 2 3 4 5 6 7 8 9

b陣列:

0 0 0 0 0 0 0 0 0 6
34 0 0 0 0 0 0 0 0 21
0 1 2 3 4 5 6 7 8 9

從pa1=1,pa2=5開始,繼續向中間遍歷,當不需要交換時,資料向兩端補齊,要交換時,放入b中,下次要交換髮生在pa1=2,pa2=3,此時
a陣列:

1 1 2 3 4 4 5 7 8 9
21 21 53 8 78 78 123 53 34 111
0 1 2 3 4 5 6 7 8 9

b陣列:

0 2 0 0 0 0 0 0 3 6
34 53 0 0 0 0 0 0 8 21
0 1 2 3 4 5 6 7 8 9

下一次,pa1=3,pa2=4,結束。
下面就是整合兩個陣列,結束時a的指標,s=2,
現將b後面的陣列接上去,再把b前面的部分接上去,最後得到:
a陣列:

1 3 6 0 2 4 5 7 8 9
21 8 21 34 53 78 123 53 34 111
0 1 2 3 4 5 6 7 8 9

可以看出這種劃分結束時,以a[3]=34劃分的結果保證了相等元素在劃分後,下標的有序,所以這種快排是穩定的。

參考:
邵順增. 穩定快速排序演算法研究[J]. 計算機應用與軟體, 2014, 31(7):263-266.