当前位置:网站首页>2.5 Quick Sort
2.5 Quick Sort
2022-07-30 04:30:00 【TUJC】
2.5、快速排序
2.5.1、快速排序法介绍:
快速排序(Quicksort)是对冒泡排序的一种改进.
基本思想:
通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列.
快速排序法示意图:
2.5.2、代码实现
要求:对「-9,78,0.23,567,70]进行从小到大的排序,要求使用快速排序法.
说明[验证分析]:
1)If cancel the recursive around,结果是-9 -567 0 23 78 70
2)If cancel the right recursive,结果是-567-9 0 23 78 70
3)If cancel the left recursion,结果是-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);
}
}
}
边栏推荐
- Usage of exists in sql
- How does MySql find out the latest data row that meets the conditions?
- Eureka Registry
- 【C进阶】数组传参与函数指针
- Roperties class configuration file & DOS to view the host network situation
- Data Lake: Data Integration Tool DataX
- 深圳见!云原生加速应用构建专场:来看云原生 FinOps、SRE、高性能计算场景最佳实践
- Based on all volunteers - H and D1 XR806 rare plant monitoring device
- state space representation
- Advanced [C] array to participate in the function pointer
猜你喜欢
2.5快速排序
Pytorch框架学习记录3——Transform的使用
深圳见!云原生加速应用构建专场:来看云原生 FinOps、SRE、高性能计算场景最佳实践
MySQL String Concatenation - Various String Concatenation Practical Cases
宇宙的尽头是银行?聊聊在银行做软件测试的那些事
@WebServlet注解(Servlet注解)
Arrays and Structures
[The Mystery of Cloud Native] Cloud Native Background && Definition && Detailed explanation of related technologies?
WEB 渗透之信息收集
Go书籍大全-从初级到高级以及Web开发
随机推荐
Eureka Registry
Mysql version upgrade, copy the Data file directly, the query is very slow
cnpm installation steps
sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
The first day of Flink learning - what is batch and streaming computing?
Android Studio 实现登录注册-源代码 (连接MySql数据库)
C. Qualification Rounds(思维,特情)
error: The following untracked working tree files would be overwritten by
The VUX Datetime component compute-days-function dynamically sets the date list
How to compare struct, slice, map for equality and the difference between several comparison methods in golang
【Redis高手修炼之路】Jedis——Jedis的基本使用
Azure 开发者新闻快讯丨开发者7月大事记一览
js 操作在当前日期加减(天、周、月、年数)
labelme的使用技巧
[MRCTF2020]Hello_ misc
Data Lake: Data Integration Tool DataX
MySQL data query (subtotal and sorting)
Install MySQL Database on Kylin V10 Operating System
KubeMeet 报名 | 「边缘原生」线上技术沙龙完整议程公布!
《构建之法》笔记---第十章 典型用户和场景