当前位置:网站首页>After sorting (bubble sorting), learn to continuously update other sorting methods
After sorting (bubble sorting), learn to continuously update other sorting methods
2022-07-27 21:40:00 【Xiao Tang learns to catch babies】
= = = = = = = = = Bubble sort = = = = = = = =
One 、 Bubble sorting means that a maximum or minimum number can be selected for each round of comparison ( Make them bubble )
Two 、 After each round of comparison, put the largest number after that round of comparison at the end
example :int[] arr = { 9 , 5 , 3 , 18, 2 } ( Take small to large platoon as an example )
1、 In the first round, all elements are compared in pairs in order , If the value of the previous element Larger than the value of the following element , Then switch places , After such a round, the last value is the largest
The first round of comparison :
= = = = = = =9 , 5 , 3 , 18, 2 = = = = = = = =
for the first time 9 and 5 Compare ,9 Big , In exchange for :
5 , 9 , 3 , 18, 2
The second time 9 and 3 Compare ,9 Big , In exchange for :
5 , 3 , 9 , 18, 2
third time 9 and 18 Compare ,18 Big , Don't swap :
5 , 3 , 9 , 18, 2
The fourth time 18 and 2 Compare ,18 Big , In exchange for :
5 , 3 , 9 , 2,18
The first round of comparison is over , Compared four times , Get a maximum number .
2、 The second round repeats the process of the first round , Only the largest number is placed at the end , There is no need to compare the last number .
The second round of comparison :
= = = = = = = = = =5 , 3 , 9 , 2,18 = = = = = = = = =
for the first time 5 and 3 Compare ,5 Big , In exchange for :
3, 5 , 9 , 2, 18
The second time 5 and 9 Compare ,9 Big , Don't swap :
3 , 5 , 9 , 2 , 18
third time 9 and 2 Compare ,9 Big , In exchange for :
5 , 3 , 2 , 9, 18
The second round of comparison is over , Compared three times , Get the second largest number .
3、 The third round repeats the process of the second round , Only the largest number and the second largest number are placed at the end , There is no need to compare the last number .
The third round of comparison :
= = = = = = = = = =5 , 3 , 2 ,9, 18 = = = = = = = = =
for the first time 5 and 3 Compare ,5 Big , In exchange for :
3, 5 , 2 , 9, 18
The second time 5 and 2 Compare ,5 Big , In exchange for :
3 , 2 , 5 , 9, 18
The third round of comparison is over , Compared twice , Get a third largest number .
4、 The fourth round repeats the process of the third round , Only the largest number 、 The second largest number and the third largest number are placed at the end , There is no need to compare the last number .
Fourth round comparison :
= = = = = = = = = = 3 , 2 , 5 , 9, 18 = = = = = = = = =
for the first time 3 and 2 Compare ,3 Big , In exchange for :
**2 **, 3 , 5 , 9, 18
thus , All the numbers have been compared .
3、 ... and 、 Come to the rule :
1、 If there is n Number , that We need to compare n-1 round
2、 In comparison k round when , Just compare == n - k == Time .
such as : Compare the second round , Need to compare 5 - 2 = 3 Time . Because a maximum number has been compared in the first round , Left at this time 4 Number , Just compare 3 Time .
Four 、 Code implementation
Core code :
int [ ] arr = { 9,5,3,18,2};
for (int i = 0 ; i < arr.length-1 ;i++ ) {// Control the number of cycles , 5 Number ,4 round
for (int j = 0 ; j < arr.length - 1 - i ;j++ ) { After each round of comparison, the last number will not be compared
if(arr[j] >= arr[j+1]){ // If the number in front is larger than that in the back, it will be exchanged , All the way to the end
temp = arr[j];// Exchange value
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
边栏推荐
- Mobilevit learning notes
- Up to 7.5gbps! The world's first 5nm 5g baseband snapdragon X60 release: support the aggregation of all major bands!
- Commercial delay of self-developed 5g chip? Apple iPhone will adopt Qualcomm 5g chip in the next four years
- Under the epidemic, the mobile phone supply chain and offline channels are blocked! Sales plummeted and inventory was serious!
- 递归/回溯刷题(上)
- 高并发遇到死锁了,如何搞?
- 除了「加机器」,其实你的微服务还能这样优化
- For 3nm and below processes, ASML new generation EUV lithography machine exposure
- Some operations about Anaconda (installing software and quickly opening)
- 华为成立全球生态发展部:全力推进HMS全球生态建设
猜你喜欢

怎么还有人问 MySQL 是如何归档数据的呢?

zibbix安装部署

"Geography language" large model Wenxin Ernie geol and its application

为什么要使用MQ消息中间件?这几个问题必须拿下

监听服务器jar运行,及重启脚本

In addition to "adding machines", in fact, your micro service can be optimized like this

如何实现一个好的知识管理系统?

零钱通项目(两个版本)含思路详解

异常-Exception

Characteristics and determination scheme of Worthington mushroom polyphenol oxidase
随机推荐
疫情之下,手机供应链及线下渠道受阻!销量骤降库存严重!
成员方法及其传参机制
Samsung's most advanced EUV production line has been put into operation: the 7Nm capacity this year will be three times that of last year
华为成立全球生态发展部:全力推进HMS全球生态建设
监听服务器jar运行,及重启脚本
Oppo core making plan officially announced: the first chip or oppo M1
C language - Introduction - grammar - pointer (12)
Characteristics and determination scheme of Worthington mushroom polyphenol oxidase
微软商店无法下载应用,VS2019无法下载插件问题解决方案
聊聊 MySQL 事务二阶段提交
Can single mode and multi-mode of industrial switches replace each other?
Design of noise reduction link based on DSP
美司法部增加针对华为的指控,包括窃取商业秘密等16项新罪名
哈希表的查找与插入及删除
Worthington phospholipase A2 study phosphatidylcholine 2-acetylhydrolase
Technical practice behind bloom model: how to refine 176billion parameter model?
如何实现一个好的知识管理系统?
LinkedList underlying source code
面向3nm及以下工艺,ASML新一代EUV光刻机曝光
MobileVIT学习笔记