快速排序
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;
}
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;
}
相关文章推荐
- CFile、CStdioFile、FILE和其他文件操作(转+总结)
- idea14使用maven创建web工程
- 一种小波包的实例
- 图像处理算法基础(一)---图像颜色空间转换
- 如何在MFC中添加全局变量和全局函数
- JavaScript数字精度丢失问题总结
- MySQL如何清空慢查询文件
- FPGA 视频拼接器的架构
- MySQL 清空慢查询文件
- 矩阵是怎样变换向量的
- poi生成word表格文档
- 性能测试问题总结4
- 最佳跳槽时机及最缺人地区发布
- torque 安装排错
- [Web前端]梳理-HTML5.3.表单验证
- unix虚拟存储器详解
- try catch 使用
- Altium Designer 转换gerber文件的问题解决
- Ajax ContentType 列表大全
- 软件开发编程的经典原则