快排算法
2016-07-29 17:17
127 查看
void QuickSort(int *a, int n)
{
if (n < 1) return;
int L = 0;
int R = n - 1;
int temp = a[0];
while (L < R)
{
while (L < R && a[R] >= temp) --R;
a[L] = a[R];
while (L < R && a[L] <= temp) ++L;
a[R] = a[L];
}
a[L] = temp;
QuickSort(a, L);
QuickSort(a + L + 1, n - L - 1);
}
int main()
{
int n,temp;
int a[] = {2, 1, 5, 8};
QuickSort(a, 4);
cout<<"the values after sort:"<<endl;
for (int i = 0;i < 4; ++i)
cout << a[i] <<" ";
}
{
if (n < 1) return;
int L = 0;
int R = n - 1;
int temp = a[0];
while (L < R)
{
while (L < R && a[R] >= temp) --R;
a[L] = a[R];
while (L < R && a[L] <= temp) ++L;
a[R] = a[L];
}
a[L] = temp;
QuickSort(a, L);
QuickSort(a + L + 1, n - L - 1);
}
int main()
{
int n,temp;
int a[] = {2, 1, 5, 8};
QuickSort(a, 4);
cout<<"the values after sort:"<<endl;
for (int i = 0;i < 4; ++i)
cout << a[i] <<" ";
}
相关文章推荐
- Map冲突
- 学习IOS第一天
- IFS域内字段分隔符
- DatePickerDialog和EditText配合使用
- QT--一些零散的代码备份
- Spring中MultipartHttpServletRequest实现文件上传 生成缩略图
- android服务
- Linux2.6内核--进程调度理论
- ajax循环执行
- Git命令行使用教程01
- thinkPHP bug解决
- @MappedSuperclass 解决父类属性不能传递给子类
- Android必知必会-App 常用图标尺寸规范汇总
- 图像压缩编码和解码原理
- eCryptfs - close系统调用
- Android Studio你不知道的调试技巧
- python 循环读取txt文档 并转换成csv
- hdu 3294 Manacher模版题
- centos7下 tomcat的安装与配置
- R语言常用命令集