快速排序一般采用递归方法(详见快速排序及其优化),但递归方法一般都可以用循环代替。本文实现了java版的非递归快速排序。

更多:数据结构与算法合集

思路分析

  采用非递归的方法,首先要想到栈的使用,通过阅读递归调用部分的代码,思考如何用栈来代替。递归调用的核心代码是 pivot = partition(a, low, high); 每次循环都必须包含这句核心代码,可以想到,如果要对该行代码实现循环,只能对low和high采取操作,所以我们在栈中压入low和high,每个循环弹出一对low和high,用于核心代码的实现,当栈空后就说明没有需要排序的部分了,结束循环。
  下面是递归代码和根据递归代码修改成的非递归代码。

递归部分代码:

	/**
* 递归调用
*/
public void qSort(int[] a, int low, int high) {
int pivot;
if (low >= high)
return; //原始递归操作
// pivot = partition(a, low, high); // 将数列一分为二
// qSort(a, low, pivot - 1); // 对低子表排序
// qSort(a, pivot + 1, high); // 对高子表排序 // 优化递归操作
while (low < high) {
pivot = partition(a, low, high); // 将数列一分为二
qSort(a, low, pivot - 1); // 对低子表排序
low = pivot + 1;
}
}

  

修改成的非递归代码:

	/**
* 非递归
*/
public void qSort2(int[] a, int low, int high) {
int pivot;
if (low >= high)
return;
Stack<Integer> stack = new Stack<Integer>();
stack.push(low);
stack.push(high);
while (!stack.empty()) {
// 先弹出high,再弹出low
high = stack.pop();
low = stack.pop();
pivot = partition(a, low, high);
// 先压low,再压high
if (low < pivot - 1) {
stack.push(low);
stack.push(pivot - 1);
}
if (pivot + 1 < high) {
stack.push(pivot + 1);
stack.push(high);
}
}
}

  

注意点:栈弹出的顺序与压入的顺序相反,要小心栈的压入与弹出操作。

完整Java代码

(含测试代码)

import java.util.Arrays;
import java.util.Stack; /**
*
* @Description 快速排序的递归与非递归实现
*
* @author yongh
* @date 2018年9月14日 下午2:39:00
*/
public class QuickSort {
public void quickSort(int[] a) {
if (a == null)
return;
qSort(a, 0, a.length - 1);
}
/**
* 递归
*/
public void qSort(int[] a, int low, int high) {
int pivot;
if (low >= high)
return; //原始递归操作
// pivot = partition(a, low, high); // 将数列一分为二
// qSort(a, low, pivot - 1); // 对低子表排序
// qSort(a, pivot + 1, high); // 对高子表排序 // 优化递归操作
while (low < high) {
pivot = partition(a, low, high); // 将数列一分为二
qSort(a, low, pivot - 1); // 对低子表排序
low = pivot + 1;
}
} public void quickSort2(int[] a) {
if (a == null)
return;
qSort2(a, 0, a.length - 1);
}
/**
* 非递归
*/
public void qSort2(int[] a, int low, int high) {
int pivot;
if (low >= high)
return;
Stack<Integer> stack = new Stack<Integer>();
stack.push(low);
stack.push(high);
while (!stack.empty()) {
// 先弹出high,再弹出low
high = stack.pop();
low = stack.pop();
pivot = partition(a, low, high);
// 先压low,再压high
if (low < pivot - 1) {
stack.push(low);
stack.push(pivot - 1);
}
if (pivot + 1 < high) {
stack.push(pivot + 1);
stack.push(high);
}
}
} /**
* 对数组a中下标从low到high的元素,选取基准元素pivotKey,
* 根据与基准比较的大小,将各个元素排到基准元素的两端。
* 返回值为最后基准元素的位置
*/
public int partition(int[] a, int low, int high) { // 三数取中,将中间元素放在第一个位置
if (a[low] > a[high])
swap(a, low, high);
if (a[(low + high) / 2] > a[high])
swap(a, (low + high) / 2, high);
if (a[low] < a[(low + high) / 2])
swap(a, (low + high) / 2, low); int pivotKey = a[low]; // 用第一个元素作为基准元素
while (low < high) { // 两侧交替向中间扫描
while (low < high && a[high] >= pivotKey)
high--;
a[low] = a[high];
// swap(a, low, high); //比基准小的元素放到低端
while (low < high && a[low] <= pivotKey)
low++;
a[high] = a[low];
// swap(a, low, high); //比基准大的元素放到高端
}
a[low] = pivotKey; // 在中间位置放回基准值
return low; // 返回基准元素所在位置
} public void swap(int[] a, int i, int j) {
int temp;
temp = a[j];
a[j] = a[i];
a[i] = temp;
} // =========测试代码=======
//测试的为非递归方法quickSort2()
public void test1() {
int[] a = null;
quickSort2(a);
System.out.println(Arrays.toString(a));
} public void test2() {
int[] a = {};
quickSort2(a);
System.out.println(Arrays.toString(a));
} public void test3() {
int[] a = { 1 };
quickSort2(a);
System.out.println(Arrays.toString(a));
} public void test4() {
int[] a = { 3, 3, 3, 3, 3 };
quickSort2(a);
System.out.println(Arrays.toString(a));
} public void test5() {
int[] a = { -3, 6, 3, 1, 3, 7, 5, 6, 2 };
quickSort2(a);
System.out.println(Arrays.toString(a));
} public static void main(String[] args) {
QuickSort demo = new QuickSort();
demo.test1();
demo.test2();
demo.test3();
demo.test4();
demo.test5();
}
}

  

null
[]
[]
[, , , , ]
[-, , , , , , , , ]

QuickSort

收获

  递归改为非递归,联想到栈的使用,根据对核心代码的循环,确定栈中存储什么数据。

更多:数据结构与算法合集

【Java】快速排序的非递归实现的更多相关文章

  1. 排序算法练习--JAVA(插入、直接选择、冒泡、快速排序、非递归快速排序)

    排序算法是数据结构中的经典算法知识点,也是笔试面试中经常考察的问题,平常学的不扎实笔试时候容易出洋相,回来恶补,尤其是碰到递归很可能被问到怎么用非递归实现... package sort; impor ...

  2. 8皇后以及N皇后算法探究,回溯算法的JAVA实现,非递归,循环控制及其优化

    上两篇博客 8皇后以及N皇后算法探究,回溯算法的JAVA实现,递归方案 8皇后以及N皇后算法探究,回溯算法的JAVA实现,非递归,数据结构“栈”实现 研究了递归方法实现回溯,解决N皇后问题,下面我们来 ...

  3. 【Java】 归并排序的非递归实现

    归并排序可以采用递归方法(见:归并排序),但递归方法会消耗深度位O(longn)的栈空间,使用归并排序时,应该尽量使用非递归方法.本文实现了java版的非递归归并排序. 更多:数据结构与算法合集 思路 ...

  4. 算法笔记_013:汉诺塔问题(Java递归法和非递归法)

    目录 1 问题描述 2 解决方案  2.1 递归法 2.2 非递归法 1 问题描述 Simulate the movement of the Towers of Hanoi Puzzle; Bonus ...

  5. 自己写算法---java的堆的非递归遍历

    import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { S ...

  6. 数据结构二叉树的递归与非递归遍历之java,javascript,php实现可编译(1)java

    前一段时间,学习数据结构的各种算法,概念不难理解,只是被C++的指针给弄的犯糊涂,于是用java,web,javascript,分别去实现数据结构的各种算法. 二叉树的遍历,本分享只是以二叉树中的先序 ...

  7. 二叉树3种递归和非递归遍历(Java)

    import java.util.Stack; //二叉树3种递归和非递归遍历(Java) public class Traverse { /******************一二进制树的定义*** ...

  8. JAVA 遍历文件夹下的所有文件(递归调用和非递归调用)

    JAVA 遍历文件夹下的所有文件(递归调用和非递归调用) 1.不使用递归的方法调用. public void traverseFolder1(String path) { int fileNum = ...

  9. Java实现非递归删除目录

    最近在学C#的文件系统, 发现C#的文件系统貌似比java的东西少一点, 居然连删除目录都直接做好封装了, 想到学java的时候还要自己写递归删除, 好像没写过非递归的,就在网上查了下, 关于非递归删 ...

随机推荐

  1. 【BZOJ4500】矩阵(差分约束)

    [BZOJ4500]矩阵(差分约束) 题面 BZOJ 然而权限题 题解 显然拆分行和列.不妨设这一行/列总共加减的值是\(p\),那么每一个限制就是两个数的和为一个特定的数.这样子不好做,反正是一个二 ...

  2. Qt QGraphicsItem 绕中心旋转、放缩

    最近用到了QGraphicsItem,可以通过QGraphicsItemAnimation使其产生动画效果. QGraphicsItemAnimation自带了setPosAt().setRotati ...

  3. tomcat 性能调优

    1. 内存 windows在bin/catalina.bat的注释下第一行加入 set JAVA_OPTS=-Xms2048m -Xmx2048m -Xss128K -XX:PermSize=64m ...

  4. PHP多条件模糊查询

    所使用的方法:$sqlArr=array();array_push();implode(); 原理, 一.建立sql语句前半句,并且建立一个空数组. 二.根据条件是否为空来判断是否向数组中添加元素.如 ...

  5. linux命令总结free命令

    free 命令是什么 free 命令是一个显示系统中空闲和已用内存大小的工具.free 命令的输出和 top 命令相似.大多数Linux发行版已经含有 free 命令. 如何运行 free 想要运行, ...

  6. POJ 3537 multi-sg 暴力求SG

    长为n的一列格子,轮流放同种棋子,率先使棋子连成3个者胜. 可以发现每次放一个棋子后,后手都不能放在[x-2,x+2]这个区间,那么相当于每次放棋将游戏分成了两个,不能放棋者败. 暴力求SG即可 /* ...

  7. c#的as,is 运算符

  8. SCI写作经典替换词

  9. Value = undefined

    Value = undefined Javascript在计算机程序中,经常会声明无值的变量.未使用值来声明的变量,其值实际上是 undefined. 在执行过以下语句后,变量 carname 的值将 ...

  10. iframe引入网页

    <!DOCTYPE html> <html> <body> <iframe src="/example/html/demo_iframe.html& ...