当前位置:网站首页>Bubble sort
Bubble sort
2022-07-06 04:36:00 【A distant youth】
Bubble sort is a simple sort algorithm . It repeatedly visits the sequence to be sorted , Compare two elements at a time , If they are in the wrong order, exchange them . The job of the interview sequence is to repeat until there is no need to exchange , That is to say, the sequence has been sorted . The name of this algorithm comes from the fact that the smaller the elements, the more slowly “ floating ” Go to the top of the list .
1.1 Algorithm description
- Compare adjacent elements . If the first one is bigger than the second one , Just exchange the two of them ;
- Do the same for each pair of adjacent elements , From the beginning of the first couple to the end of the last couple , So the last element should be the largest number ;
- Repeat the above steps for all elements , Except for the last one ;
- Repeat step 1~3, Until sorting is complete .
1.2 Dynamic diagram demonstration
1.3 Code implementation
function bubbleSort(arr) {
var len = arr.length;
for (var i = 0; i < len - 1; i++) {
for (var j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j+1]) { // Comparison of two adjacent elements
var temp = arr[j+1]; // Element exchange
arr[j+1] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
边栏推荐
- 2/13 qaq~~ greed + binary prefix sum + number theory (find the greatest common factor of multiple numbers)
- coreldraw2022新版本新功能介绍cdr2022
- Visio draws Tai Chi
- [try to hack] John hash cracking tool
- Brief tutorial for soft exam system architecture designer | general catalog
- MySQL reported an error datetime (0) null
- Yyds dry goods inventory OSI & tcp/ip
- Dynamic programming (tree DP)
- 1291_ Add timestamp function in xshell log
- Platformio create libopencm3 + FreeRTOS project
猜你喜欢
Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022
1291_ Add timestamp function in xshell log
[Yu Yue education] reference materials of complex variable function and integral transformation of Northwestern Polytechnic University
Visio draw fan
Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
English Vocabulary - life scene memory method
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (柔性对接 AutoDock)
[Zhao Yuqiang] deploy kubernetes cluster with binary package
满足多元需求:捷码打造3大一站式开发套餐,助力高效开发
Patent | subject classification method based on graph convolution neural network fusion of multiple human brain maps
随机推荐
[05-1, 05-02, 05-03] network protocol
VNCTF2022 WriteUp
Mysql数据库慢sql抓取与分析
NPM command -- install dependent packages -- Usage / explanation
P2022 interesting numbers (binary & digit DP)
Tengine kernel parameters
Sqlserver query results are not displayed in tabular form. How to modify them
newton interpolation
电脑钉钉怎么调整声音
查询mysql数据库中各表记录数大小
拉格朗日插值法
QML和QWidget混合开发(初探)
Comprehensive ability evaluation system
Canal synchronizes MySQL data changes to Kafka (CentOS deployment)
Solutions: word coverage restoration, longest serial number, Xiaoyu buys stationery, Xiaoyu's electricity bill
HotSpot VM
View 工作流程
Basic use of MySQL (it is recommended to read and recite the content)
SharedPreferences source code analysis
牛顿插值法