新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 用VC++類實現快速排序(并輸出過程)

        用VC++類實現快速排序(并輸出過程)

        作者: 時間:2016-12-01 來源:網絡 收藏


        &&&&&&&&&&&&&&&&&&&&&&&&&&&&主函數&&&&&&&&&&&&&&&&&&&&&&
        #include
        #include
        #include
        #include "WangQi.h"
        using namespace std;
        #define MAX 100
        void main(){
        SeqList L;
        int num;
        cout<<"請輸入要排序的元素個數:"<cin>>num;
        cout<<"請輸入要排序的元素:"<for(int i=1;i<=num;i++)
        cin>>L.r[i];
        L.length=num;
        //輸出排序前的順序表
        L.output(&L,1,L.length,-1);
        L.quicksort(&L,1,L.length);
        L.output(&L,1,L.length,-2);
        }
        &&&&&&&&&&&&&&&&&&&含有類定義的頭文件&&&&&&&&&&&&&&&&&&&&&&&&&
        #include
        using namespacestd;
        #define MAX 100
        class SeqList{
        public:
        int r[MAX+1];
        int length;

        本文引用地址:http://www.104case.com/article/201612/324137.htm

        void output(SeqList *L,int low, int high,int pivotloc){
        int i;

        if(pivotloc==-1||pivotloc==-2){
        if(pivotloc==-1)
        cout<<"初始狀態:{"<< ;
        else cout<<"排序結果:{"<< ;
        for(i=low;i<=high;i++)
        cout<r[i]<< ;
        cout<<"}";
        }else {
        cout<<"劃分結果:{"<< ;
        for(i=low;i cout<r[i]<< ;
        cout<<"}"<r[pivotloc]<<"{";
        for(i=pivotloc+1;i<=high;i++)
        cout<r[i]<< ;
        cout<<"}";
        }
        cout<<<}


        int partition(SeqList *L,int low,int high){
        int pivotkey;
        int temp1=low,temp2=high;
        L->r[0]=L->r[low];
        pivotkey=L->r[low];
        while (low while (lowr[high]>=pivotkey)
        --high;
        L->r[low]=L->r[high];
        while(lowr[low]<=pivotkey)
        ++low;
        L->r[high]=L->r[low];
        }
        L->r[low]=L->r[0];
        output(L,temp1,temp2,low);
        return low;
        }


        void quicksort(SeqList *L,int low,int high){
        int pivotloc;
        if(low pivotloc=partition(L,low,high);
        if(low quicksort(L,low,pivotloc-1);
        if(high>pivotloc+1)
        quicksort(L,pivotloc+1,high);
        }
        };



        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 安顺市| 延川县| 德兴市| 三台县| 纳雍县| 高邮市| 宜州市| 绥中县| 镇平县| 防城港市| 油尖旺区| 沛县| 南靖县| 年辖:市辖区| 桦川县| 娱乐| 阳春市| 平塘县| 五莲县| 唐海县| 临洮县| 沾化县| 岱山县| 长治市| 澄城县| 宾阳县| 深州市| 福泉市| 海兴县| 普格县| 舒兰市| 恩施市| 左云县| 亳州市| 陆川县| 潮州市| 叶城县| 任丘市| 射阳县| 肇东市| 镇赉县|