当前位置:网站首页>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;
}
边栏推荐
- flink sql 能同时读多个topic吗。with里怎么写
- Guitar Pro 8.0最详细全面的更新内容及全部功能介绍
- The value of two date types is subtracted and converted to seconds
- Visio draws Tai Chi
- Meet diverse needs: jetmade creates three one-stop development packages to help efficient development
- C. The third problem
- 11. Intranet penetration and automatic refresh
- E. Best Pair
- R note prophet
- Crawler notes: improve data collection efficiency! Use of proxy pool and thread pool
猜你喜欢
Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
CertBot 更新证书失败解决
[Chongqing Guangdong education] engineering fluid mechanics reference materials of southwestjiaotonguniversity
Data processing methods - smote series and adasyn
捷码赋能案例:专业培训、技术支撑,多措并举推动毕业生搭建智慧校园毕设系统
About some basic DP -- those things about coins (the basic introduction of DP)
[Yu Yue education] reference materials of complex variable function and integral transformation of Northwestern Polytechnic University
Easyrecovery靠谱不收费的数据恢复电脑软件
Overturn your cognition? The nature of get and post requests
CADD课程学习(8)-- 化合物库虚拟筛选(Virtual Screening)
随机推荐
Ue5 small knowledge freezerendering view rendered objects in the cone
canal同步mysql数据变化到kafka(centos部署)
Certbot failed to update certificate solution
ue5 小知识点 开启lumen的设置
Understanding of processes, threads, coroutines, synchronization, asynchrony, blocking, non blocking, concurrency, parallelism, and serialization
C. The Third Problem(找规律)
Mysql database storage engine
动态规划(树形dp)
Guitar Pro 8.0最详细全面的更新内容及全部功能介绍
The most detailed and comprehensive update content and all functions of guitar pro 8.0
About some basic DP -- those things about coins (the basic introduction of DP)
VPP性能测试
1291_ Add timestamp function in xshell log
Solutions: word coverage restoration, longest serial number, Xiaoyu buys stationery, Xiaoyu's electricity bill
Deep learning framework installation (tensorflow & pytorch & paddlepaddle)
BOM - location, history, pop-up box, timing
P3500 [poi2010]tes intelligence test (two points & offline)
During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input
Explain in simple terms node template parsing error escape is not a function
Yyds dry inventory automatic lighting system based on CC2530 (ZigBee)