当前位置:网站首页>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;
}
边栏推荐
- E. Best Pair
- tengine 内核参数
- P3500 [poi2010]tes intelligence test (two points & offline)
- Cross domain and jsonp details
- word封面下划线
- Canal synchronizes MySQL data changes to Kafka (CentOS deployment)
- . Net interprocess communication
- During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input
- MIT CMS. 300 session 8 – immersion / immersion
- [HBZ sharing] how to locate slow queries in cloud database
猜你喜欢
Solution of storage bar code management system in food industry
Visio draws Tai Chi
Ue5 small knowledge points to enable the setting of lumen
Mlapi series - 04 - network variables and network serialization [network synchronization]
JVM garbage collector concept
Figure application details
Recommendation | recommendation of 9 psychotherapy books
Visio draw fan
When debugging after pycharm remote server is connected, trying to add breakpoint to file that does not exist: /data appears_ sda/d:/segmentation
电脑钉钉怎么调整声音
随机推荐
满足多元需求:捷码打造3大一站式开发套餐,助力高效开发
flink sql 能同时读多个topic吗。with里怎么写
Overturn your cognition? The nature of get and post requests
[leetcode question brushing day 33] 1189 The maximum number of "balloons", 201. The number range is bitwise AND
Canal synchronizes MySQL data changes to Kafka (CentOS deployment)
Certbot failed to update certificate solution
MySQL learning record 13 database connection pool, pooling technology, DBCP, c3p0
Case of Jiecode empowerment: professional training, technical support, and multiple measures to promote graduates to build smart campus completion system
食品行业仓储条码管理系统解决方案
Meet diverse needs: jetmade creates three one-stop development packages to help efficient development
P3500 [POI2010]TES-Intelligence Test(二分&离线)
拉格朗日插值法
8. Static file
Fedora/REHL 安装 semanage
Yyds dry goods inventory OSI & tcp/ip
Platformio create libopencm3 + FreeRTOS project
MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】
Dynamic programming (tree DP)
View 工作流程
In depth MySQL transactions, stored procedures and triggers