您的位置:首页 > 其它

快速排序

2015-12-03 09:16 274 查看
#include<iostream>

using namespace std;

#define MAXSIZE 1000

typedef int keytype;

typedef struct

{

keytype key;

}redtype;

typedef struct

{

redtype r[MAXSIZE+1];

int length;

}sqlist;

int Partition(sqlist &L,int low,int high)

{

L.r[0]=L.r[low];

int 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 creat(sqlist &L,int n)

{

L.length=0;

for(int i=1;i<=n;i++)

{

cin>>L.r[i].key;

L.length++;

}

}

void pri_(sqlist L)

{

for(int i=1;i<=L.length;i++)

cout<<L.r[i].key<<' ';

cout<<endl;

}

void qsort(sqlist &L,int low,int high)

{

if(low<high)

{

int pivotloc=Partition(L,low,high);

qsort(L,low,pivotloc-1);

qsort(L,pivotloc+1,high);

}

}

int main()

{

sqlist L;

cout<<"请输入表的长度:"<<endl;

int n;

cin>>n;

creat(L,n);

qsort(L,1,L.length);

pri_(L);

return 0;

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