快速排序的非递归版本
来源:互联网 发布:suse和ubuntu 编辑:程序博客网 时间:2024/05/21 14:02
快速排序非递归版本
public static void quickSortDemo() { int[] array = { 0, 2, 11, 121, 18, 99, 3, 5, 101, 22, 9, 100, -12 }; System.out.println(Arrays.toString(array)); ; quicksort(array); System.out.println(Arrays.toString(array)); } public static void quicksort( int[] array ) { if (array == null || array.length == 1) return; // 存放开始与结束索引,借助于栈数据结构记录每一个待分割的序列的起始下标和结束下标 Stack<Integer> s = new Stack<Integer>(); // 压栈 s.push(0); s.push(array.length - 1); // 利用循环里实现 while (!s.empty()) { int right = s.pop(); int left = s.pop(); // 如果最大索引小于等于左边索引,说明结束了 if (right <= left) continue; int i = partition(array, left, right); if (left < i - 1) { s.push(left); s.push(i - 1); } if (i + 1 < right) { s.push(i + 1); s.push(right); } } } // 找到轴心,进行交换,分割操作。 public static int partition( int[] data , int first , int end ) { int temp; int i = first, j = end; if (first < end) { temp = data[i]; // 当i=j的时候,则说明扫描完成了 while (i < j) { // 从右边向左边扫描找到一个小于temp的元素 while (j > i && data[j] > temp) j--; if (i < j) { // 将该元素赋值给temp data[i] = data[j]; // 赋值后就应该将i+1指向下一个序号 i++; } // 然后从左边向右边开始扫描,找到一个大于temp的元素 while (i < j && temp > data[i]) i++; if (i < j) { // 将该元素赋值给temp data[j] = data[i]; // 赋值后就应该将j-1指向前一个序号 j--; } } // 将轴数据放在i位置中 data[i] = temp; } return i; }
0 0
- 非递归版本的快速排序
- 快速排序的非递归版本
- 快速排序 归并排序的非递归版本 备忘
- 非递归的快速排序
- 快速排序----非递归
- 快速排序 非递归
- 非递归快速排序
- 快速排序 非递归
- 快速排序非递归
- 每日编程7之快速排序非递归版本
- 快速排序的非递归算法
- 非递归的快速排序算法
- 快速排序的非递归实现
- php的非递归快速排序实现
- 非递归的快速排序实现(转)
- 快速排序的非递归实现
- 快速排序的非递归实现
- 快速排序的非递归实现
- JAVA中的排序
- Ubuntu Application List
- LinkedList
- MD控件(二) CollapsingToolbarLayout
- 204 IOC 仿照laravel实现一个最简容器 2
- 快速排序的非递归版本
- 小模拟(没啥技术含量)
- scala-特质(trait)
- 集合的一些应用 扑克牌 年龄排序
- VS2015常用快捷键
- androidstudio Ndk 环境配置
- MFC ActiveX控件初步 调试方法
- Archive for required library:E:program Files/apache-maven-3.1.0/repository/org/springframework/sprin
- 初认Map