快速排序 (递归思想)
来源:互联网 发布:note express for mac 编辑:程序博客网 时间:2024/06/07 14:09
#include <iostream>
#include <stdlib.h>
using namespace std;
void QuickSort(int *array, int len) {
if (len <= 1)
return;
int*left = new int[len], *right = new int[len];
int left_idx = 0, right_idx = 0;
int key = array[0];
for (int i = 1; i < len; i++) {
if (array[i] < key)
left[left_idx++] = array[i];
if (array[i] > key)
right[right_idx++] = array[i];
}
QuickSort(left, left_idx);
QuickSort(right, right_idx);
int idx = 0;
for (int i = 0; i < left_idx; i++)
array[idx++] = left[i];
array[idx++] = key;
for (int i = 0; i < right_idx; i++)
array[idx++] = right[i];
delete[] left;
delete[] right;
}
int main() {
int a[10] = { 0,9,7,1,4,2,3,5,6,8 };
QuickSort(a, 10);
for(int i=0;i<10;i++)
cout << a[i];
system("pause");
return 0;
}
#include <stdlib.h>
using namespace std;
void QuickSort(int *array, int len) {
if (len <= 1)
return;
int*left = new int[len], *right = new int[len];
int left_idx = 0, right_idx = 0;
int key = array[0];
for (int i = 1; i < len; i++) {
if (array[i] < key)
left[left_idx++] = array[i];
if (array[i] > key)
right[right_idx++] = array[i];
}
QuickSort(left, left_idx);
QuickSort(right, right_idx);
int idx = 0;
for (int i = 0; i < left_idx; i++)
array[idx++] = left[i];
array[idx++] = key;
for (int i = 0; i < right_idx; i++)
array[idx++] = right[i];
delete[] left;
delete[] right;
}
int main() {
int a[10] = { 0,9,7,1,4,2,3,5,6,8 };
QuickSort(a, 10);
for(int i=0;i<10;i++)
cout << a[i];
system("pause");
return 0;
}
阅读全文
0 0
- 快速排序 (递归思想)
- 快速排序(递归与分治的思想)
- 快速排序 : 分治和递归思想
- 快速排序(递归)
- 快速排序(递归)
- 快速排序(递归)
- Java使用分治递归的思想实现快速排序
- 【数据结构】大量数据(20万)的快速排序的递归与非递归算法、三数取中思想
- 排序--递归排序(快速排序)
- 快速排序(递归和非递归)
- 快速排序(递归和非递归)
- 快速排序实现(递归+非递归)
- 快速排序(递归+非递归)
- 快速排序(递归版本)
- 快速排序(非递归)
- 快速排序(递归版本)
- 快速排序(递归实现)
- 快速排序(递归版)
- 视频流并发测试工具srs-bench使用问题汇总
- C++ function关键字
- hdu 1511 最小路径覆盖问题
- 内连接查询 (select * from a join b on a.id = b.id) 与 关联查询 (select * from a , b where a.id = b.id)的区别
- SVN服务器的搭建和使用(二)
- 快速排序 (递归思想)
- 下载一个新内核想编译一个内核驱动之前工作
- JDK1.6.0+Tomcat6.0的安装配置
- Android自带的四种线程池
- Hibernate----QBC语句
- Java数组排序-冒泡排序
- mongodb常用命令
- Unity之AssetBundle自动打包工具
- HSSFWorkbook强制换行