快速排序算法

  1. #include<iostream> 
  2. #define INITSIZE 20 
  3. #define INCREAMENT 5 
  4. #define OK 1 
  5. #define ERROR 0 
  6. #define OVERFLOW -1  
  7. using namespace std; 
  8. typedef struct Lnode 
  9.     int key; 
  10.     char info; 
  11. }LNode; 
  12. typedef struct 
  13.     LNode *r; 
  14.     int length; 
  15.     int listsize; 
  16. }SqList; 
  17. int initList(SqList &L) 
  18.     L.r=(LNode *)malloc(sizeof(LNode) * INITSIZE); 
  19.     if(!L.r) exit(OVERFLOW); 
  20.     L.length=1; 
  21.     L.r[0].key = 0; 
  22.     L.listsize=INITSIZE; 
  23.     return OK; 
  24. int Listinsert(SqList &L,int i,LNode *elem) 
  25.     if(i<1||i>L.length+1)  return ERROR; 
  26.     if(L.length>=L.listsize) 
  27.     { 
  28.         LNode *newbase=(LNode *)realloc(L.r,(L.length+INCREAMENT)*sizeof(LNode)); 
  29.         if(!newbase) exit(OVERFLOW); 
  30.         L.r=newbase; 
  31.         L.listsize+=INCREAMENT; 
  32.     } 
  33. //  L.r[i].key = elem->key; 
  34.  
  35.     LNode *q=&L.r[i]; 
  36.     for(LNode *p=&(L.r[L.length-1]);p>=q;--p) 
  37.     { 
  38.         *(p+1)=*p; 
  39.     } 
  40.     L.r[i].key=elem->key;      
  41.     ++L.length; 
  42.     return OK; 
  43. int Partition(SqList &L,int low,int high) 
  44.     L.r[0].key=L.r[low].key; 
  45.     int pivotkey=L.r[low].key; 
  46.     while(high>low) 
  47.     { 
  48.         while(low<high  &&  L.r[high].key>=pivotkey)  --high; 
  49.  
  50.         if(low<high)    L.r[low].key=L.r[high].key; 
  51.  
  52.         while(low<high  &&  L.r[low].key<=pivotkey)    ++low; 
  53.  
  54.         if(low<high) L.r[high].key=L.r[low].key; 
  55.     }    
  56.     L.r[low].key=pivotkey; 
  57.     return high; 
  58.  
  59. int Qsort(SqList &L,int low,int high) 
  60.     if(low<high) 
  61.     { 
  62.         int pivotkey; 
  63.         pivotkey=Partition(L,low,high); cout<<"pivotkey  "<<pivotkey<<"  low "<<low<<"  high "<<high<< endl;  
  64.         for(int i=1;i<L.length;++i) 
  65.         cout<<L.r[i].key<<"  "
  66.         cout<<endl;   
  67.         Qsort(L,low,pivotkey-1); 
  68.         Qsort(L,pivotkey+1,high); 
  69.     } 
  70.     return OK; 
  71. int Quicksort(SqList &L) 
  72.     Qsort(L,1,L.length - 1); 
  73.     return OK; 
  74. int main(void
  75.     SqList L; 
  76.     initList(L); 
  77.     int i,num; 
  78.     cout<<"請輸入排序的個數"<<endl; 
  79.     cin>>num; 
  80.     for(i=1;i<=num;i++) 
  81.     { 
  82.         LNode *p=(LNode *)malloc(sizeof(LNode)); 
  83.         cout<<"輸入關鍵字\n"
  84.         cin>>p->key; 
  85. //      cout<<"輸入信息"<<endl; 
  86. //      cin>>p->info; 
  87.         Listinsert(L,i,p); 
  88.     } 
  89.     cout<<"排序前:\n"
  90.     for(i=1;i<L.length;++i) 
  91.         cout<<L.r[i].key<<"  "
  92.     //  cout<<"TEST HERE"<<endl; 
  93.     cout<<endl; 
  94. //  cout<<L.length<<endl; 
  95.     Quicksort(L); 
  96.      
  97.     cout<<"排序後:"<<endl; 
  98.     for(i=1;i<L.length;++i) 
  99.         cout<<L.r[i].key<<"  "
  100.     return OK; 
  101.      

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章