【剑指 offer】—— 快速排序
来源:互联网 发布:淘宝售后网页打不开 编辑:程序博客网 时间:2024/04/20 12:27
两个辅助函数:
// 生成区间内的随机整数int RandInRange(int s, int e, unsigned seed=time_t(0)){ srand(seed); return rand()%(e-s)+s;}void Swap(int& a, int& b){ int tmp = a; a = b; b = tmp;}
分区函数:
int partition(int seq[], int len, int start, int end){ if (seq == NULL || len <= 0 || start < 0 || end >= len) throw new exception("Invalid parameters"); int pivot = RandInRange(start, end); Swap(seq[pivot], seq[end]); int small = start - 1; for (int i = start; i < end; ++i) { if (seq[i] < seq[end]) { ++small; if (small != i) Swap(seq[small], seq[i]); } } ++small; Swap(seq[end], seq[small]); return small;}
主调函数,由递归结构定义:
void qsort(int seq[], int len, int start, int end){ if (start == end) return; int idx = partition(seq, len, start, end); if (idx > start) qsort(seq, len, start, idx-1); if (idx < end) qsort(seq, len, idx+1, end);}
0 0
- 剑指offer——快速排序
- 【剑指 offer】—— 快速排序
- 剑指Offer——快速排序
- 【剑指offer】快速排序
- 剑指offer--快速排序
- 剑指offer 快速排序
- 剑指offer 快速排序
- 剑指offer 快速排序
- 快速排序-剑指offer
- 剑指offer-快速排序
- 剑指offer—第二章算法之快速排序
- 剑指Offer系列---(10)快速排序
- 剑指offer——年龄排序问题
- 排序—快速排序
- 剑指offer--快速排序变形(5、29)
- 为了offer系列——快速排序(C++),附测试用例
- Offer——排序算法
- 排序——快速排序
- nyoj 236 心急的C小加
- 自定义XListView控件实现上拉刷新下拉加载以及关联应用的一点感受
- 第5周项目1—三角形类雏形(2、内联成员函数)
- BroadcastReceiver的小细节
- duplicate symbol _OBJC_IVAR_$
- 【剑指 offer】—— 快速排序
- Go语言学习二 :常数和条件语句
- Hello--Spring
- C++实验二:正整数
- HDU1312:Red and Black(DFS)
- Eclispe中web.xml无自动提示问题
- Java守护线程
- UVA 11090 Going in Cycle!!
- 循序渐进之Maven(2) - 第一个Maven项目