【快速】排序, T(n) = O(nlgn),O(n^2) S(n) = O(lgn) --- 不稳定
2013-12-14 20:03
411 查看
public static void quickSort(RS[] arrRS,int low,int high){ // 保存【分割点】 int partition; // 【段长】大于1 if(low < high){ // 一次分割,获得【分割点】 和 两个【子段】 partition = quickPartition(arrRS,low,high); //【递归】两个子段 quickSort(arrRS, low, partition-1); quickSort(arrRS, partition+1, high); } } //分割 19 private static int quickPartition(RS[] arrRS, int left, int right) { // 拿起左边第一个,作【标尺】 RS tempRS = arrRS[left]; // 【段长】大于1,未结束 while (left < right) { /*################ 左 <<=== 右 ##################*/ while (left < right && arrRS[right].val>=tempRS.val) { right--; } if (left < right) { // 若未结束,右边大的,放到左边 left 索引处,left 右移 arrRS[left] = arrRS[right]; left ++; } /*################ 左 ===>> 右 ##################*/ while (left < right && arrRS[left].val<tempRS.val) { left++; } if (left < right) { // 若未结束,左边小的,放到右边 right 索引处,right 左移 arrRS[right] = arrRS[left]; right --; } } // 【标尺】归【分割点】 arrRS[left] = tempRS; // 返回分割点 return left; }
相关文章推荐
- (C# 基础) 类访问修饰符
- LA 3713 Astronauts
- LA 3211 Now or later
- uva 11324
- LA 5135 Mining Your Own Business
- http://kb.cnblogs.com/page/130970/ -http协议
- poj 2942 LA3523 点-双…
- uva 10047
- STL中优先队列的用法
- 并查集及其路径的压缩
- 求欧拉回路的路径 dfs的一种新运用
- poj 2676
- poj 2531
- poj 2251
- poj 2488
- 网络流 SAP优化算法 (…
- 网络流之Dinic算法
- 基于增广路的网络流算法
- poj 1789 最小生成树
- poj 1469