剑指offer 快速排序
来源:互联网 发布:nga芙妹淘宝店 编辑:程序博客网 时间:2024/03/29 14:31
快速排序一般的写法,教科书上很详细,这里介绍作者的写法。
程序如下:
#include<iostream>#include<stack>using namespace std;void swap(int *value1, int *value2){ int temp = *value1; *value1 = *value2; *value2 = temp;}int partition(int data[],int length,int start,int end){ if (NULL == data || length <= 0 || start < 0 || end >= length) throw exception("Invalid Data!"); int small = start - 1; for (int index = start; index < end; index++) { if (data[index] <= data[end]) { small++; if (small != index) swap(&data[index],&data[small]); } } small++; swap(&data[small],&data[end]); return small;}void QuickSort(int data[], int length, int start, int end){ if (start == end) return; int index = partition(data,length,start,end); if (start < index) QuickSort(data, length, start, index-1); if (index < end) QuickSort(data, length, index+1, end);}int main(){ int data[] = {5,7,2,4,9,3,8,4,1}; QuickSort(data,9,0,8); for (int i = 0; i < 9; i++) { cout << i << " "; } return 0;}
1 0
- 【剑指offer】快速排序
- 剑指offer--快速排序
- 剑指offer 快速排序
- 剑指offer 快速排序
- 剑指offer 快速排序
- 快速排序-剑指offer
- 剑指offer-快速排序
- 剑指Offer系列---(10)快速排序
- 剑指offer——快速排序
- 【剑指 offer】—— 快速排序
- 剑指Offer——快速排序
- 剑指offer--快速排序变形(5、29)
- 剑指offer—第二章算法之快速排序
- 剑指offer--快速排序递归实现,年龄排序O(N)实现,旋转数组最小值查找
- 剑指offer 归并排序
- 归并排序-剑指offer
- 堆排序-剑指offer
- 剑指offer-排序算法
- 常见服务器错误代号
- MFC SQlite3封装工程
- xss问题
- fgets函数与fputs函数
- 网络课程视频网站
- 剑指offer 快速排序
- j2me版本的俄罗斯方块写法要点
- FlatUi的简单介绍
- 雍正杀“舅”:握着领导把柄,隆科多必须死?
- JAVA 上加密算法的实现用例
- 简明 Python 编程规范
- python matplotlib 画心形线
- hdu 1269 迷宫城堡(tarjan判连通)
- LintCode-最长公共子串