算法----快排算法
来源:互联网 发布:阿里云 储值卡 余额 编辑:程序博客网 时间:2024/04/18 22:08
实际上快排算很好理解:
举个例子:有一个数组a,在数组中选择一个数x,然后把数组中所有小于等于x的数放到x左边,大于x的数放到右边,然后再利用分治递归思想把整个数组排成有序系列。
其实现代码如下:
其中partition划分思想是这样的:选择数组中最后一个数为划分标准a[r],i记录第一个大于a[r]的数,从数组p的位置遍历到r的位置,每当a[j]小于等于a[r]的时候就把i位置的数和j位置的数交换一下,然后i++,这时候i位置的住依然是第一个大于a[r]的数。
#include <iostream>using namespace std;int partition(int *a,int p,int r){int i = p;for (int j = p; j <= r - 1; ++j){if (a[j] <= a[r]){swap(a[i], a[j]);i++;}}swap(a[i], a[r]);return i;}void quick_sort(int *a, int p, int r){if (p < r){int q = partition(a, p, r);quick_sort(a, q + 1, r);quick_sort(a, p, q - 1);}}int main(){int i;int a[] = { 88, 1, 22, 99, 55, 77, 33, 66, 44, 70 };cout << "After the quick sort,the data is:" << endl;quick_sort(a, 0, 9);for (const auto i : a)cout << i << " ";cout << endl;return 0;}
0 0
- 算法----快排算法
- 快排算法
- 快排算法
- 快排算法
- 快排算法cmp
- 快排算法
- 算法导论--->快排
- 排序算法--快排
- 快排算法quickSort
- 快排算法
- 随机快排算法
- [算法]快排
- c#快排算法
- 算法 排序 快排
- 快排算法
- 大话算法-快排
- 算法-快排
- 快排算法
- testlink和mantis两种管理工具集成
- 灰盒测试与单元测试的区别
- LeetCode(65) Valid Number
- 【Nginx】nginx虚拟机设置
- angular学习日志02
- 算法----快排算法
- ubuntu下安装程序的三种方法
- UIday1501:NSString的几种常用方法
- 使用BigDecimal进行精确数字运算
- OC-代码块的运用
- PPP拨号
- Python Regular Expression
- C++内存管理
- JAVA连接mysql