当前位置:网站首页>Brief introduction of bubble sort and quick sort
Brief introduction of bubble sort and quick sort
2022-07-25 17:42:00 【Suddenly】
1、 Bubble sort
Compare two at a time adjacent The elements of , And in accordance with the Ascending or descending Replace the position according to the rules of , You need to use double-layer loop traversal , Each cycle will only sort one value , A total of n-1 Time ;
stability : Stable , Two elements with the same value will not exchange positions ;
Time complexity :O(n2);
Spatial complexity :1
usage : Suitable for small amounts of data ;
Code implementation :
public static void main(String[] args) {
// Bubble sort
int[] a = {
2, 5, 9, 7, 8, 1, 4};
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - 1 - i; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
2、 Quick sort
First step : Find a benchmark , In a dichotomy , Find a... From the array base Elements ( Generally, the first element is the cardinality ) Divide a string of data into two strings ;
The second step : Partition , Rearrange the array , Will be less than base Put the element on the left , Greater than base Put the element on the right , Put the same value anywhere , bring base Become an intermediate element ;
The third step : recursive , Sort the elements of the two groups separately ;
stability : unstable , Two elements with the same value may exchange positions ;
Time complexity :O(nlogn), If
baseIf it is just the maximum or minimum value, it is O(n2);Spatial complexity :O(nlogn), If
baseIf it is just the maximum or minimum value, it is O(n);usage : Suitable for small amounts of data ;
Code implementation :
public static void main(String[] args) {
int[] b = {
4, 5, 9, 7, 8, 1, 4, 3, 7, 9, 10};
quickSort(b, 0, b.length - 1);
}
private static void quickSort(int[] b, int left, int right) {
if (left > right) {
return;
}
int i = left;
int j = right;
//temp It's the reference level
int temp = b[left];
while (i < j) {
// First look at the right side , To the left
while (temp <= b[j] && i < j) {
j--;
}
// Look at the left side , Increase right in turn
while (temp >= b[i] && i < j) {
i++;
}
// If the conditions are met, exchange
if (i < j) {
int t = b[j];
b[j] = b[i];
b[i] = t;
}
}
// Compare cardinality with i=j Location data exchange , Generate new cardinality at one time , And the data at both ends will be processed recursively
b[left] = b[i];
b[i] = temp;
// Recursively call the left half array
quickSort(b, left, j - 1);
// Recursively call right half array
quickSort(b, j + 1, right);
}
边栏推荐
- 【PHP伪协议】源码读取、文件读写、任意php命令执行
- An article about ultrasonic humidifier
- ROS learning notes (IV) ROS cannot solve rosdep init or update
- 食品安全 | 八问八答带你重新认识小龙虾!这样吃才对!
- Tkinter module advanced operations (I) -- transparent buttons, transparent text boxes, custom buttons and custom text boxes
- With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
- Mongodb cluster and sharding
- 交叉验证(cv)学习笔记
- 11. Camera and lens
- PostgreSQL里有只编译语句但不执行的方法吗?
猜你喜欢

世界各地的标志性建筑物

With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development

「数字安全」警惕 NFT的七大骗局

我也是醉了,Eureka 延迟注册还有这个坑!

An article about ultrasonic humidifier

自动化测试 PO设计模型

EDI docking commercehub orderstream

STM32 PAJ7620U2手势识别模块(IIC通信)程序源码详解

What is an IP SSL certificate and how to apply for it?

Idea 必备插件
随机推荐
面试官:说说 log.Fatal 和 panic 的区别
Wu Enda logistic regression 2
PHP解决并发问题的几种实现
mongodb 集群及分片
go语言context控制函数执行超时返回
mysql case when
After consulting about how to deal with DDL in Flink SQL client, how to add fields and jobs to the mapping table in Fink SQL?
Redis源码与设计剖析 -- 15.RDB持久化机制
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
Summary of knowledge points for final review of server-side architecture design
How to prevent the unburned gas when the city gas safety is alarmed again?
go接口变量的类型断言
约瑟夫环问题
Mongodb cluster and sharding
Three dimensional function display of gray image
哈夫曼树的构建
2022/7/23
如何看一本书
Automated test Po design model
P2P 之 UDP穿透NAT的原理与实现