当前位置:网站首页>2.5快速排序
2.5快速排序
2022-07-30 04:16:00 【TUJC】
2.5、快速排序
2.5.1、快速排序法介绍:
快速排序(Quicksort)是对冒泡排序的一种改进。
基本思想:
通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
快速排序法示意图:

2.5.2、代码实现
要求:对「-9,78,0.23,567,70]进行从小到大的排序,要求使用快速排序法。
说明[验证分析]:
1)如果取消左右递归,结果是-9 -567 0 23 78 70
2)如果取消右递归,结果是-567-9 0 23 78 70
3)如果取消左递归,结果是-9-567 0 23 70 78
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
public class QuickSort {
public static void main(String[] args) {
//int[] arr = {-9,78,0,23,-567,70, -1,900, 4561};
//测试快排的执行速度
// 创建要给80000个的随机的数组
int[] arr = new int[8000000];
for (int i = 0; i < 8000000; i++) {
arr[i] = (int) (Math.random() * 8000000); // 生成一个[0, 8000000) 数
}
System.out.println("排序前");
Date data1 = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date1Str = simpleDateFormat.format(data1);
System.out.println("排序前的时间是=" + date1Str);
quickSort(arr, 0, arr.length-1);
Date data2 = new Date();
String date2Str = simpleDateFormat.format(data2);
System.out.println("排序前的时间是=" + date2Str);
//System.out.println("arr=" + Arrays.toString(arr));
}
public static void quickSort(int[] arr,int left, int right) {
int l = left; //左下标
int r = right; //右下标
//pivot 中轴值
int pivot = arr[(left + right) / 2];
int temp = 0; //临时变量,作为交换时使用
//while循环的目的是让比pivot 值小放到左边
//比pivot 值大放到右边
while( l < r) {
//在pivot的左边一直找,找到大于等于pivot值,才退出
while( arr[l] < pivot) {
l += 1;
}
//在pivot的右边一直找,找到小于等于pivot值,才退出
while(arr[r] > pivot) {
r -= 1;
}
//如果l >= r说明pivot 的左右两的值,已经按照左边全部是
//小于等于pivot值,右边全部是大于等于pivot值
if( l >= r) {
break;
}
//交换
temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
//如果交换完后,发现这个arr[l] == pivot值 相等 r--, 前移
if(arr[l] == pivot) {
r -= 1;
}
//如果交换完后,发现这个arr[r] == pivot值 相等 l++, 后移
if(arr[r] == pivot) {
l += 1;
}
}
// 如果 l == r, 必须l++, r--, 否则为出现栈溢出
if (l == r) {
l += 1;
r -= 1;
}
//向左递归
if(left < r) {
quickSort(arr, left, r);
}
//向右递归
if(right > l) {
quickSort(arr, l, right);
}
}
}
边栏推荐
- Drools (7): WorkBench
- 网页元素解析a标签
- Hongji was once again shortlisted in the Gartner 2022 RPA Magic Quadrant and achieved a significant jump in position
- Mysql version upgrade, copy the Data file directly, the query is very slow
- 基于OpenCV实现的图像拼接(配准)案例
- Eureka Registry
- 逆向理论知识3【UI修改篇】
- Taobao H5 interface to obtain app data 6.0 format
- 宇宙的尽头是银行?聊聊在银行做软件测试的那些事
- Taobao/Tmall get Taobao store details API
猜你喜欢

逆向理论知识3【UI修改篇】

(6) "Digital Electricity" - Diodes and CMOS Gate Circuits (Introduction)

Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (6) Question Opening and Defense PPT

MYSQL 唯一约束

Charles 替换 接口响应信息

What are Redis server startup after the operation?

验证addShutdownHook钩子生效

Pytorch framework learning record 3 - the use of Transform

机器学习:知道通过低方差过滤实现降维过程

MySql 怎么查出符合条件的最新的数据行?
随机推荐
Pytorch framework to study record 6 - the torch. Nn. The Module and the torch nn. Functional. The use of conv2d
QT(39)-vs开发qt程序提示无法打开源文件
Boutique: Taobao/Tmall Get Order Details API for Purchased Products
LeetCode 114. Expand Binary Tree into Linked List (One Question Three Eats)
The first immersive and high-fidelity metaverse in China, Xiyuan Universe is officially launched
Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (3) Background Functions
CMake installation and testing
Forum management system
宇宙的尽头是银行?聊聊在银行做软件测试的那些事
RRU, BBU, AAU
Roperties class configuration file & DOS to view the host network situation
数据库概论 - MySQL的简单介绍
C. Travelling Salesman and Special Numbers (二进制 + 组合数)
Detailed transport layer
Mysql版本升级,直接复制Data文件,查询特别慢
【翻译】Envoy Fundamentals,这是一个培训课程,使人们能够更快地采用Envoy Proxy。...
FreeRTOS Personal Notes - Memory Management
骁龙7系芯片表现如何?Reno8 Pro佐证新一代神U
2021山东省网络搭建与应用赛项试题
The leap second that may cause the next "Millennium Bug" is boycotted by tech giants