快速排序 C代码

来源:互联网 发布:身份证被别人淘宝开店 编辑:程序博客网 时间:2024/04/29 21:56

#include<iostream.h>

#define MIXSIZE 20
typedef struct
{
 int key;
 int otherinfo;
}Redtype;
typedef struct
{
 Redtype r[MIXSIZE+1];
 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)
{
 if(low<high)
 {
     int pivotloc;
  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 Output(Sqlist L)
{
 int i;
 for(i=1;i<=L.length;i++)
  cout<<L.r[i].key<<" ";
 cout<<endl;
}

void main()

 Sqlist s;
 int t;
 cout<<"请输入线形表的长度:";
    cin>>s.length;
    for(int i=1;i<=s.length;i++)
 {cin>>t;
 s.r[i].key=t;}
 Quicksort(s);
    Output(s);
}