当前位置:网站首页>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);
}
}
}
边栏推荐
- phpoffice edit excel document
- mysql 结构、索引详解
- Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (1) Development Overview
- Eureka Registry
- Many overseas authoritative media hotly discuss TRON: laying the foundation for the decentralization of the Internet
- swagger使用教程——快速使用swagger
- ospf 导图
- 【C进阶】数组传参与函数指针
- MYSQL 唯一约束
- 使用EFR32作为Zigbee/Thread的sniffer的用法
猜你喜欢

Thymeleaf简介

Data Lake: Data Integration Tool DataX

Eureka Registry

Forum management system

New LaaS protocol Elephant Swap provides ePLATO with sustainable premium space

Atomic Guarantees of Redis Distributed Locks

Roperties class configuration file & DOS to view the host network situation

Pytorch框架学习记录7——卷积层

Redis【超详解!!!】

Redis "super explanation!!!!!!"
随机推荐
宇宙的尽头是银行?聊聊在银行做软件测试的那些事
Go书籍大全-从初级到高级以及Web开发
[ 云原生之谜 ] 云原生背景 && 定义 && 相关技术详解?
[Node accesses MongoDB database]
CMake installation and testing
Flink学习第一天——什么是批量、流式计算?
Unity3D Application模拟进入前后台及暂停
MySQL 字符串拼接 - 多种字符串拼接实战案例
(redistribute, special comprehensive experiment ospf area)
Usage of exists in sql
高并发框架 Disruptor
Arrays and Structures
cv2.polylines
函数的底层机制
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (6) Question Opening and Defense PPT
Azure 开发者新闻快讯丨开发者7月大事记一览
【C语言】程序环境和预处理
SSM框架简单介绍
What are Redis server startup after the operation?
MySql 怎么查出符合条件的最新的数据行?