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

快速排序 C++

2011-02-26 08:40 337 查看
/*

快速排序
*/
#include<iostream.h>
#include<iomanip.h>
#include<time.h>
#include<stdlib.h>
#define MAXI 10
typedef int KeyType;
typedef int ElemType;
struct rec{
KeyType key;
ElemType data;
};
typedef rec sqlist[MAXI];
class kuaisu
{//类定义
public:
kuaisu(sqlist a,int m):n(m)
{
for(int i=0;i<n;i++)
b[i]=a[i];

}
void quicksort(int s,int t)
{
int i;
if(s<t)
{
i=part(s,t);
quicksort(s,i-1);
quicksort(i+1,t);
}
else
return;
}
int part(int s,int t)
{
int i,j;
rec p;
i=s;
j=t;
p=b[s];
while(i<j)
{
while(i<j && b[j].key>=p.key)
{
j--;
}
b[i]=b[j];
while(i<j && b[i].key<=p.key)
{
i++;
}
b[j]=b[i];

}
b[i]=p;
output();
return i;

}
void output()
{
for(int i=0;i<n;i++)
cout<<setw(4)<<b[i].key;
cout<<endl;
}
private:
sqlist b;
int n;
};
void main()
{
cout<<"The result is :/n";
sqlist a1;
int i,n=MAXI,low=0,high=9;
srand(time(0));//Gets the system time.
//time returns the time in elapsed seconds. There is no error return.
for(i=0;i<n;i++)
a1[i].key=rand()%80;
kuaisu px(a1,n);
cout<<"before sort the array is:/n";
px.output();
cout<<"The action of 排序/n";
px.quicksort(low,high);
cout<<"排序后:/n";
px.output();
cin.get();

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