当前位置:网站首页>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;
}
边栏推荐
- [HBZ sharing] how to locate slow queries in cloud database
- Yyds dry goods inventory OSI & tcp/ip
- Implementation of knowledge consolidation source code 2: TCP server receives and processes half packets and sticky packets
- coreldraw2022新版本新功能介绍cdr2022
- 捷码赋能案例:专业培训、技术支撑,多措并举推动毕业生搭建智慧校园毕设系统
- How does vs change the project type?
- P3500 [POI2010]TES-Intelligence Test(二分&离线)
- 2/13 review Backpack + monotonic queue variant
- newton interpolation
- Overturn your cognition? The nature of get and post requests
猜你喜欢
View 工作流程
coreldraw2022新版本新功能介绍cdr2022
Execution order of scripts bound to game objects
CADD course learning (7) -- Simulation of target and small molecule interaction (flexible docking autodock)
Certbot failed to update certificate solution
Redis —— Redis In Action —— Redis 实战—— 实战篇一 —— 基于 Redis 的短信登录功能 —— Redis + Token 的共享 session 应用— 有代码
How to realize automatic playback of H5 video
Case of Jiecode empowerment: professional training, technical support, and multiple measures to promote graduates to build smart campus completion system
题解:《单词覆盖还原》、《最长连号》、《小玉买文具》、《小玉家的电费》
Guitar Pro 8.0最详细全面的更新内容及全部功能介绍
随机推荐
MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】
Certbot failed to update certificate solution
[Yu Yue education] reference materials of complex variable function and integral transformation of Northwestern Polytechnic University
Platformio create libopencm3 + FreeRTOS project
Case of Jiecode empowerment: professional training, technical support, and multiple measures to promote graduates to build smart campus completion system
R note prophet
查询mysql数据库中各表记录数大小
JVM garbage collector concept
[HBZ sharing] how to locate slow queries in cloud database
Selection of slow motion function
2/13 qaq~~ greed + binary prefix sum + number theory (find the greatest common factor of multiple numbers)
Several important classes in unity
Introduction to hashtable
Lombok原理和同时使⽤@Data和@Builder 的坑
Scala function advanced
Ue5 small knowledge points to enable the setting of lumen
Fedora/REHL 安装 semanage
MySQL learning record 13 database connection pool, pooling technology, DBCP, c3p0
npm命令--安装依赖包--用法/详解
Tengine kernel parameters