快速排序
时间:2022-05-04
本文章向大家介绍快速排序,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
java
package com.test.arithmetic;
import java.util.Arrays;
/**
* Two point go together, small is left and big is right.
* Thus the first meet will separate the array to half small and half big.
* Created by Ryan on 2017/3/25/025.
*/
public class QuickSort {
public int count = 0;
public int[] sort(int[] source, int low, int high) {
if (low >= high) {
return source;
}
int first = low;
int last = high;
int key = source[first];
while (first < last) {
//find the first smaller from the end
while (last > first && source[last] >= key) {
last--;
}
if (first < last) {
source[first] = source[last];
first++; //The first one has already been sorted, move to the next one.
}//else first = last, that is the same one, do not need to swap them
//find the first bigger from the start
while (first < last && source[first] <= key) {
first++;
}
if (first < last) {
source[last] = source[first];
last--; //The last one has already been sorted as a bigger, move to the previous one.
}
}
source[first] = key; //put the key
System.out.println("The " + ++count + " sort:");
Arrays.stream(source).forEach(item -> System.out.print(item + ", "));
System.out.println();
sort(source, low, first - 1);
sort(source, first + 1, high);
return source;
}
public void quickSort1(int arr[], int low, int high) {
int l = low;
int h = high;
int key = arr[low];
while (l < h) {
while (l < h && arr[h] >= key) {
h--;
}
if (l < h) {
int temp = arr[h];
arr[h] = arr[l];
arr[l] = temp;
l++;
}
while (l < h && arr[l] <= key) {
l++;
}
if (l < h) {
int temp = arr[h];
arr[h] = arr[l];
arr[l] = temp;
h--;
}
}
System.out.print("l=" + (l + 1) + "h=" + (h + 1) + "key=" + key + "n");
if (l > low) quickSort1(arr, low, l - 1);
if (h < high) quickSort1(arr, l + 1, high);
}
public <T> void swap(T[] source, int i, int j) {
T temp = source[i];
source[i] = source[j];
source[j] = temp;
}
/**
* 方式2 更高效点的代码:
*/
public <T extends Comparable<? super T>> T[] quickSort2(T[] targetArr, int start, int end) {
int i = start + 1, j = end;
T key = targetArr[start];
if (start >= end) return (targetArr);
/**
* 从i++和j--两个方向搜索不满足条件的值并交换
*条件为:i++方向小于key,j--方向大于key
*/
while (true) {
while (targetArr[j].compareTo(key) > 0) j--;
while (targetArr[i].compareTo(key) < 0 && i < j) i++;
if (i >= j) break;
this.swap(targetArr, i, j);
if (targetArr[i] == key) {
j--;
} else {
i++;
}
}
/**
* 关键数据放到‘中间’*
*/
this.swap(targetArr, start, j);
if (start < i - 1) {
this.quickSort2(targetArr, start, i - 1);
}
if (j + 1 < end) {
this.quickSort2(targetArr, j + 1, end);
}
return targetArr;
}
/**
* 方式3:减少交换次数,提高效率
*/
public <T extends Comparable<? super T>> void quickSort3(T[] targetArr, int start, int end) {
int i = start, j = end;
T key = targetArr[start];
while (i < j) {
/*按j--方向遍历目标数组,直到比key小的值为止*/
while (j > i && targetArr[j].compareTo(key) >= 0) {
j--;
}
if (i < j) {
/*targetArr[i]已经保存在key中,可将后面的数填入*/
targetArr[i] = targetArr[j];
i++;
}
/*按i++方向遍历目标数组,直到比key大的值为止*/
/*此处一定要小于等于零,假设数组之内有一亿个1,0交替出现的话,而key的值又恰巧是1的话,那么这个小于等于的作用就会使下面的if语句少执行一亿次。*/
while (i < j && targetArr[i].compareTo(key) <= 0) {
i++;
}
if (i < j) {
/*targetArr[j]已保存在targetArr[i]中,可将前面的值填入*/
targetArr[j] = targetArr[i];
j--;
}
/*此时i==j*/
targetArr[i] = key;
/*递归调用,把key前面的完成排序*/
this.quickSort3(targetArr, start, i - 1);
/*递归调用,把key后面的完成排序*/
this.quickSort3(targetArr, j + 1, end);
}
}
}
Test:
package com.test.arithmetic;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import static org.junit.Assert.*;
/**
* Created by Ryan on 2017/3/25/025.
*/
public class QuickSortTest {
private int[] source;
private Integer[] source2;
private Integer[] source3;
private QuickSort quick;
private int[] expect;
private Integer[] expect2;
private Integer[] expect3;
@Before
public void setUp(){
source = new int[]{13, 16, 1, 1, 5, 15, 13, 15, 13, 8};
source2 = new Integer[]{13, 16, 1, 1, 5, 15, 13, 15, 13, 8};
source3 = new Integer[]{13, 16, 1, 1, 5, 15, 13, 15, 13, 8};
expect = new int[]{1, 1, 5, 8, 13, 13, 13, 15, 15, 16};
expect2 = new Integer[]{1, 1, 5, 8, 13, 13, 13, 15, 15, 16};
expect3 = new Integer[]{1, 1, 5, 8, 13, 13, 13, 15, 15, 16};
quick = new QuickSort();
}
@Test
public void testSort() throws Exception {
int[] source = new int[]{13,16,1,1,5,15,13,15,13,8 };
int[] sort = quick.sort(source, 0, source.length - 1);
Assert.assertArrayEquals(expect, sort);
}
@Test
public void testSort1(){
QuickSort quick = new QuickSort();
quick.quickSort1(source, 0, source.length - 1);
Assert.assertArrayEquals(expect, source);
}
@Test
public void testSort2(){
QuickSort quick = new QuickSort();
quick.quickSort2(source2, 0, source2.length - 1);
Assert.assertArrayEquals(expect2, source2);
}
@Test
public void testSort3(){
QuickSort quick = new QuickSort();
quick.quickSort3(source3, 0, source.length - 1);
Assert.assertArrayEquals(expect3, source3);
}
}
Result:
l=5h=5key=13
l=4h=4key=8
l=1h=1key=1
l=3h=3key=5
l=2h=2key=1
l=9h=9key=15
l=6h=6key=13
l=7h=7key=13
l=8h=8key=15
l=10h=10key=16
The 1 sort:
8, 5, 1, 1, 13, 15, 13, 15, 13, 16,
The 2 sort:
1, 5, 1, 8, 13, 15, 13, 15, 13, 16,
The 3 sort:
1, 5, 1, 8, 13, 15, 13, 15, 13, 16,
The 4 sort:
1, 1, 5, 8, 13, 15, 13, 15, 13, 16,
The 5 sort:
1, 1, 5, 8, 13, 13, 13, 15, 15, 16,
The 6 sort:
1, 1, 5, 8, 13, 13, 13, 15, 15, 16,
The 7 sort:
1, 1, 5, 8, 13, 13, 13, 15, 15, 16,
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- NET Core Kestrel部署HTTPS 一个服务器绑一个证书 一个服务器绑多个证书
- .net core webapi jwt 更为清爽的认证 ,续期很简单(1)
- 用flask来在线管理你的iptables
- Linux Shell命令速查表
- Windows10实现滑动锁屏
- Vue&uni-app在微信浏览器隐藏titleNView的一个方法
- 使用OData服务将SAP C4C自定义BO的TextCollection暴露给外部消费者
- 如何在SAP C4C AdvancedListPane上批量执行若干BO实例的action
- SAP ABAP Webdynpro ALV的link to action的实现方法
- SAP CRM和C4C表格列宽度调整的工作原理
- SAP C4C里如何创建两个具有依赖关系的下拉菜单
- 温故知新——Spring AOP
- tf.random_shuffle 函数
- IndexError: dimension specified as 0 but tensor has no dimensions
- 死磕YOLO系列,YOLOv2的自我修养