乐高拼砌包是什么意思:用C编个快速排序的程序~~谢谢~~

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/27 23:31:23
由于最近的课设做个快速排序的实现~~本人又很菜在C方面~~~
所以请个位高手帮小弟一把~~~~谢谢啊~~~

呵呵,这个问题我给你解决,如下:(已调试成功)
#include<iostream.h>
#include<string.h>

#define MAXSIZE 20

typedef struct
{
int key;
}Redtype;

typedef struct
{
Redtype r[MAXSIZE+1];
int length;
}Sqlist;

int Parttition(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[high].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=Parttition(L,low,high);
Qsort(L,low,pivotloc-1);
Qsort(L,pivotloc+1,high);
}
}

void QuickSort(Sqlist &L)
{
Qsort(L,1,L.length);
}

void main()
{
Sqlist L;
o:
cout<<"此程序提供下列操作"<<endl;
cout<<"首先你先输入一个数字序列"<<endl;
cout<<"1.利用快速排序将此数字序列从小到大排序"<<endl;
cout<<"0.结束程序"<<endl;
cout<<"请输入您所输入的数列的数字的个数"<<endl;
int k;
cin>>k;
cout<<"请您输入一个无序的序列"<<endl;
for(int i=1;i<=k;i++)
{
cin>>L.r[i].key;
}
L.length=k;
cout<<"请您输入您想进行的操作"<<endl;
int h;
cin>>h;

if(h==1)
{
QuickSort(L);
cout<<"排列好的顺序(从小到大的顺序)是"<<endl;
for(i=1;i<=k;i++)
cout<<L.r[i].key<<" ";
goto o;
}

else if(h==0)
{
goto endl;
}
endl:
cout<<endl;

}