代码改变世界

快速排序

2012-09-12 21:49  neu_zjujunge  阅读(154)  评论(0编辑  收藏  举报

部分源代码

typedef int KeyType;
#include "SqTable.h"
template<typename D>class QkSort:public SqTable<D>
{
private:
    int Partition(int low, int high)
    {
     
        int i=low, j=high;
        elem[0]=elem[low];
        while(low<high)
        {
            while(low<high && elem[high].key>=elem[0].key)
                --high;
            elem[low]=elem[high];
            while(low<high&&elem[low].key<=elem[0].key)
                ++low;
            elem[high]=elem[low];
        }
        elem[low]=elem[0];
        cout<<endl<<"pivotloc="<<low<<" Partition("<<i<<", "<<j<<")="<<endl;
        Traverse(Visit);
        return low;
    }
    void QSort(int low, int high)
    {
        if(low<high)
        {
            int pivotloc=Partition(low, high);
            
            QSort(low, pivotloc-1);
            QSort(pivotloc+1, high);
        }
    }
public:
    void QuickSort()
    {
        QSort(1, length);
    }
};

示意图