当前位置:网站首页>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;
}边栏推荐
- 11. Intranet penetration and automatic refresh
- Solution of storage bar code management system in food industry
- flink sql 能同时读多个topic吗。with里怎么写
- BOM - location, history, pop-up box, timing
- Visio draws Tai Chi
- Comprehensive ability evaluation system
- 8. Static file
- 满足多元需求:捷码打造3大一站式开发套餐,助力高效开发
- PTA tiantisai l1-078 teacher Ji's return (15 points) detailed explanation
- P2102 floor tile laying (DFS & greed)
猜你喜欢

Easyrecovery靠谱不收费的数据恢复电脑软件

Stable Huawei micro certification, stable Huawei cloud database service practice

Is the mode of education together - on campus + off campus reliable

颠覆你的认知?get和post请求的本质

Visio draws Tai Chi

Patent | subject classification method based on graph convolution neural network fusion of multiple human brain maps

coreldraw2022新版本新功能介绍cdr2022

Mysql database storage engine

Introduction to hashtable

Database - MySQL storage engine (deadlock)
随机推荐
QML和QWidget混合开发(初探)
npm命令--安装依赖包--用法/详解
VPP performance test
How does vs change the project type?
【HBZ分享】ArrayList的增删慢查询快的原因
Slow SQL fetching and analysis of MySQL database
[tomato assistant installation]
coreldraw2022新版本新功能介绍cdr2022
电脑钉钉怎么调整声音
In depth MySQL transactions, stored procedures and triggers
Yyds dry inventory automatic lighting system based on CC2530 (ZigBee)
Lambda expression learning
[leetcode question brushing day 33] 1189 The maximum number of "balloons", 201. The number range is bitwise AND
Guitar Pro 8.0最详细全面的更新内容及全部功能介绍
Lombok原理和同时使⽤@Data和@Builder 的坑
Complete list of common functions of turtle module
Is the mode of education together - on campus + off campus reliable
Lagrange polynomial
Basic explanation of turtle module - draw curve
POI add border