快速排序

上次讲了基于分治法的归并排序,可是归并排序有许多缺点,比如它需要占用额外的内存来存储所需排序的数组,并且整个排序最重要的就是用来合并数组的函数。我写了几次发现,这个合并数组的函数写起来感觉有点麻烦啊!

早就听说了快速排序的鼎鼎大名,今天终于见识了。

快速排序是一种基于分治法的排序算法,平均复杂度是O(NlogN),是一般情况下最高效的排序算法。

它的主要流程是下面这样的

·1、分割对象局部数组,变成两个局部数组。

·2、分别对前后两个部分的局部数组执行quicksort

这个过程可能有点绕,看看下面这个图,我觉得就比较清晰了。

从图中可以看到,每次分割数组,都会把数组分成小于基准元素的部分和大于基准元素的部分。每一步的操作都是一样的,那么当分割到每个数组只剩下一个元素的时候,就达到了递归终点。

在这个图里,可以隐喻意识到一个问题:基准元素的选择十分重要。如果基准元素选的不好,那么算法的效率就会很低,最坏的时候时间复杂度高达O(N²)。而且,快速排序很容易由于递归深度过深,造成堆栈溢出。(一个简单的办法是,先解决分割后序列长度较短的部分,这样可以降低最大深度)

如果快排在分割的时候能恰好选择到中间值,那么整个过程大致能分为log2(N)层。在基准元素的选择上,我们可以采取随机选择,或者任取几个值然后取中间值的方式来进行选择。

不稳定性

由于快速排序交换了不相邻的元素,所以它是不稳定的排序算法。

题目

这里的题目是

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_6_C

Let’s arrange a deck of cards. Your task is to sort totally n cards. A card consists of a part of a suit (S, H, C or D) and an number. Write a program which sorts such cards based on the following pseudocode:

Partition(A, p, r)
1 x = A[r]
2 i = p-1
3 for j = p to r-1
4     do if A[j] <= x
5        then i = i+1
6            exchange A[i] and A[j]
7 exchange A[i+1] and A[r]
8 return i+1


Quicksort(A, p, r)
1 if p < r
2    then q = Partition(A, p, r)
3        run Quicksort(A, p, q-1)
4        run Quicksort(A, q+1, r)

Here, A is an array which represents a deck of cards and comparison operations are performed based on the numbers.

Your program should also report the stability of the output for the given input (instance). Here, ‘stability of the output’ means that: cards with the same value appear in the output in the same order as they do in the input (instance).

Input

The first line contains an integer n, the number of cards.

n cards are given in the following lines. Each card is given in a line and represented by a pair of a character and an integer separated by a single space.

Output

In the first line, print the stability (“Stable” or “Not stable”) of this output.

In the following lines, print the arranged cards in the same manner of that of the input.

Constraints

1 ≤ n ≤ 100,000

1 ≤ the number of a card ≤ 109

There are no identical card in the input

代码实现

按照题目给的伪代码,我们就可以写出下面的代码。(不要问我为什么这么喜欢用vector,这个坏习惯我一定要改过来,下次尽量不用vector,因为vector的效率比传统数组低)

由于归并排序是稳定排序,所以判断快排是否为稳定排序,可以通过与归并排序的结果进行比较而得出。

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;

struct card{
    char zimu;
    unsigned int num;
};

vector<card> cs;
vector<card> va;

void _Merge(int p,int mid, int q)
{

    vector<card>left,right;
    const unsigned int m = 1000000005;
    card tmp;
    tmp.num = m;
    tmp.zimu = 'x';
    for(int i=p;i<mid;i++)
        left.push_back(va[i]);

    for(int i=mid;i<q;i++)
        right.push_back(va[i]);

    left.push_back(tmp);
    right.push_back(tmp);

    int js1=0,js2=0;
    for(int i=p;i<q;i++)
    {
        if(left[js1].num<=right[js2].num)
        {
            va[i]=left[js1];
            js1++;
        }
        else{
            va[i]=right[js2];
            js2++;
        }
    }
}

void mergeSort(int p, int q)
{
    if(p+1<q)
    {
        int mid=(p+q)/2;
        mergeSort(p,mid);
        mergeSort(mid,q);
        _Merge(p,mid,q);
    }

}

int part(int p, int r)
{
    unsigned int x = cs[r].num;
    int i = p-1;
    for(int j=p;j<=r-1;j++)
    {
        if(cs[j].num<=x)
        {
            i++;
            swap(cs[j],cs[i]);
        }
    }
    swap(cs[i+1],cs[r]);
    return i+1;
}


int quickSort(int p, int r)
{
    if(p<r)
    {
        int q = part(p, r);
        quickSort(p,q-1);
        quickSort(q+1,r);
    }
}



int main()
{

    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        card tmp;
        cin>>tmp.zimu>>tmp.num;
        cs.push_back(tmp);
    }

    va=cs;//对于vector而言,这个操作是深拷贝
    quickSort(0,n-1);
    mergeSort(0,n);
    bool is_stable = true;
    for(int i=0;i<n;i++)
    {
        if(va[i].zimu!=cs[i].zimu)
        {
            cout<<"Not stable"<<endl;
            is_stable = false;
            break;
        }
    }

    if(is_stable) cout<<"Stable"<<endl;
    /*
    cout<<"=================\n";
    for(int i=0;i<n;i++)
    {
        cout<<va[i].zimu<<" "<<va[i].num<<endl;
    }
    cout<<"=================\n";
    */
    for(int i=0;i<n;i++)
    {
        cout<<cs[i].zimu<<" "<<cs[i].num<<endl;
    }
}

你也可能喜欢

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注