快速排序

来源:互联网 发布:淘宝互刷微信群 编辑:程序博客网 时间:2024/06/05 20:21
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>#include <iostream>using namespace std;typedef struct{    int key;}Data;typedef struct  {    Data *data;    int length;}Sqlist;void input(Sqlist &L){    int n;    cout<<"please enter a number"<<endl;    cin>>n;    L.length=n;    L.data=(Data *)malloc(sizeof(int)*(L.length+1));    for(int i=1;i<=n;i++){        cout<<i<<" number :  ";        cin>>L.data[i].key;    }}int Pation(Sqlist &L,int low,int high){    L.data[0]=L.data[low];    int pivotkey=L.data[low].key;    while(low<high){        while(low<high&&L.data[high].key>=pivotkey) --high;        L.data[low]=L.data[high];        while(low<high&&L.data[low].key<=pivotkey) ++low;        L.data[high]=L.data[low];    }    L.data[low]=L.data[0];    return low;}void Qsort(Sqlist &L,int low,int high){    if(low>high) return ;    if(low<high){        int pivoloc=Pation(L,low,high);        cout<<"pivoloc "<<pivoloc<<endl;        Qsort(L,low,pivoloc-1);        Qsort(L,pivoloc+1,high);    }}//Qsortvoid print(Sqlist &L){     for(int i=1;i<=L.length;i++){         cout<<i<<" "<<L.data[i].key<<endl;     } }int main(){    Sqlist L;    input(L);    print(L);    Qsort(L,1,L.length);    print(L);}

0 0