当前位置:网站首页>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;
}边栏推荐
- 8. Static file
- VPP performance test
- 2/13 review Backpack + monotonic queue variant
- Ue5 small knowledge freezerendering view rendered objects in the cone
- [HBZ sharing] how to locate slow queries in cloud database
- The most detailed and comprehensive update content and all functions of guitar pro 8.0
- Vulnerability discovery - vulnerability probe type utilization and repair of web applications
- Recommendation system (IX) PNN model (product based neural networks)
- Explain in simple terms node template parsing error escape is not a function
- 拉格朗日插值法
猜你喜欢
![[Yu Yue education] reference materials of complex variable function and integral transformation of Northwestern Polytechnic University](/img/22/ead74bc121a64910ef6ef374cd029b.png)
[Yu Yue education] reference materials of complex variable function and integral transformation of Northwestern Polytechnic University

Introduction to hashtable

Practical development of member management applet 06 introduction to life cycle function and user-defined method

Implementation of knowledge consolidation source code 1: epoll implementation of TCP server

CADD course learning (7) -- Simulation of target and small molecule interaction (flexible docking autodock)

Visio draw fan

查询mysql数据库中各表记录数大小

Mysql数据库慢sql抓取与分析

How do programmers teach their bosses to do things in one sentence? "I'm off duty first. You have to work harder."

ETCD数据库源码分析——etcdserver bootstrap初始化存储
随机推荐
Knowledge consolidation source code implementation 3: buffer ringbuffer
How does vs change the project type?
word封面下划线
[Chongqing Guangdong education] Suzhou University English film and Television Appreciation reference materials
Complete list of common functions of turtle module
NPM command -- install dependent packages -- Usage / explanation
2328. 网格图中递增路径的数目(记忆化搜索)
Basic explanation of turtle module - draw curve
Script lifecycle
深入浅出node模板解析错误escape is not a function
Vulnerability discovery - vulnerability probe type utilization and repair of web applications
Guitar Pro 8.0最详细全面的更新内容及全部功能介绍
Canal synchronizes MySQL data changes to Kafka (CentOS deployment)
In depth MySQL transactions, stored procedures and triggers
[HBZ share] reasons for slow addition and deletion of ArrayList and fast query
Platformio create libopencm3 + FreeRTOS project
2/13 qaq~~ greed + binary prefix sum + number theory (find the greatest common factor of multiple numbers)
C. The third problem
【Try to Hack】john哈希破解工具
Uva1592 Database