您的位置:首页 > 编程语言

快速排序 C代码

2010-12-15 10:17 225 查看
#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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: