当前位置:网站首页>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);
}
}
}
边栏推荐
- Alibaba search new product data API by keyword
- 【翻译】Envoy Fundamentals,这是一个培训课程,使人们能够更快地采用Envoy Proxy。...
- Has been empty, a straightforward, continue to copy the top off!
- Roperties class configuration file & DOS to view the host network situation
- Azure 开发者新闻快讯丨开发者7月大事记一览
- Shell脚本基本编辑规范及变量
- SQL introduction of the first lecture -- MySQL 8.0.29 installation tutorial (Windows 64 - bit)
- The difference between forward and redirect
- How to compare struct, slice, map for equality and the difference between several comparison methods in golang
- Unity3D Application模拟进入前后台及暂停
猜你喜欢
国内首家沉浸式高逼真元宇宙,希元宇宙正式上线
Pytorch框架学习记录2——TensorBoard的使用
The underlying mechanism of the function
Charles replaces the interface response information
MySQL 安装报错的解决方法
弘玑再度入围Gartner 2022 RPA魔力象限并实现位置大幅跃升
state space representation
Hongji was once again shortlisted in the Gartner 2022 RPA Magic Quadrant and achieved a significant jump in position
高并发框架 Disruptor
使用EFR32作为Zigbee/Thread的sniffer的用法
随机推荐
Mysql version upgrade, copy the Data file directly, the query is very slow
[The Mystery of Cloud Native] Cloud Native Background && Definition && Detailed explanation of related technologies?
redis分布式锁的原子保证
Unity3D Application模拟进入前后台及暂停
验证addShutdownHook钩子生效
逆向分析实战2
JQ source code analysis (environment)
Taobao/Tmall get the list of sold product orders API
error: The following untracked working tree files would be overwritten by
Pytorch框架学习记录5——DataLoader的使用
RRU, BBU, AAU
cv2.polylines
数组和结构体
Roperties class configuration file & DOS to view the host network situation
Flutter records and learns different animations (1)
What are Redis server startup after the operation?
ospf 综合实验(重发布,特殊区域)
Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (4) Opening Report
How does the Snapdragon 7 series chip perform?Reno8 Pro proves a new generation of God U
handler+message【消息机制】