• <li id="00i08"><input id="00i08"></input></li>
  • <sup id="00i08"><tbody id="00i08"></tbody></sup>
    <abbr id="00i08"></abbr>
  • 新聞中心

    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.czjhyjcfj.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);
    }
    };



    評論


    相關推薦

    技術專區

    關閉
    主站蜘蛛池模板: 襄汾县| 普宁市| 哈密市| 应城市| 武穴市| 永年县| 洱源县| 巨鹿县| 永和县| 邹城市| 富裕县| 巴彦淖尔市| 沈丘县| 宁强县| 中山市| 长治县| 千阳县| 黎川县| 海淀区| 栾川县| 元朗区| 大城县| 建水县| 垣曲县| 临澧县| 江山市| 海林市| 岱山县| 亳州市| 鞍山市| 吉木萨尔县| 翁牛特旗| 北京市| 锦屏县| 浦江县| 亚东县| 武平县| 驻马店市| 天水市| 平湖市| 龙门县|