关于数据结构:快速排序递归

35次阅读

共计 879 个字符,预计需要花费 3 分钟才能阅读完成。

#include <iostream>
using namespace std;
#define  MAXSIZE  20        
typedef struct{
    int key;
    char *otherinfo;
}ElemType;                        
typedef struct{
    ElemType *r;                                 
    int  length;                                
}SqList;                                        
int Partition(SqList &L,int low,int high){ 
    int pivotkey;
    L.r[0]=L.r[low];                    
    pivotkey=L.r[low].key;                       
    while(low<high){while(low<high&&L.r[high].key>=pivotkey) --high;
        L.r[low]=L.r[high];                
        while(low<high&&L.r[low].key<=pivotkey) ++low;
        L.r[high]=L.r[low];                
    }
    L.r[low]=L.r[0];                    
    return  low;                        
}
void QSort(SqList &L,int low,int high){
    int pivotloc;
    if(low<high){pivotloc=Partition(L,low,high);     
       QSort(L,low,pivotloc-1);            
       QSort(L,pivotloc+1,high);        
    }
}                                        
void QuickSort(SqList &L){QSort(L,1,L.length);
}                                                                
void Create_Sq(SqList &L){
    int i,n;
    cout<<"数据个数:";
    cin>>n;                                        
    cout<<"待排序的数据:"; 
    for(i=1;i<=n;i++){cin>>L.r[i].key;
        L.length++;
    }
}
void show(SqList L){
    int i;
    for(i=1;i<=L.length;i++)
        cout<<L.r[i].key<<" ";
}
int main(){
    SqList L;
    L.r=new ElemType[MAXSIZE+1];
    L.length=0;
    Create_Sq(L);
    QuickSort(L);
    cout<<"疾速排序:";
    show(L);
}

正文完
 0