当前位置:网站首页>Bubble sort, insert sort
Bubble sort, insert sort
2022-07-05 15:20:00 【Shepherd Wolf】
Bubble sort :
Each round brings out the biggest ( At the end of the line )
var arr = [ 5,9,1,66,33,42,90,7 ]
function bubbleFn (arr) {
for (var i = 0; i < arr.length - 1; i++) {
for (var j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j+1]) {
var temp = arr[j]
arr[j] = arr[j+1]
arr[j+1] = temp
}
}
}
return [...arr]
}
var res = bubbleFn(arr)
console.log(res)Insertion sort :
Take out a number for each round ( This number goes to the ordered array , Find your place )

var arr = [ 5,9,1,4,8,22,6 ]
function insertFn (arr) {
var targetArr = []
targetArr.push(arr[0])
for (var i = 1; i <= arr.length - 1; i++) { // Original array
var originItem = arr[i]
for (var j = targetArr.length - 1; j >= 0; j--) {
var targetItem = targetArr[j]
if (targetItem < originItem) {
targetArr.splice(j+1, 0, originItem)
break
}
if (j === 0) {
targetArr.unshift(originItem)
}
}
}
return targetArr
}
var res = insertFn(arr)
console.log(res)边栏推荐
- 数据库学习——数据库安全性
- 美团优选管理层变动:老将刘薇调岗,前阿里高管加盟
- CPU design related notes
- Brief introduction of machine learning framework
- Under the crisis of enterprise development, is digital transformation the future savior of enterprises
- easyOCR 字符识别
- Detailed explanation of QT creator breakpoint debugger
- Anaconda uses China University of science and technology source
- "Sequelae" of the withdrawal of community group purchase from the city
- Ctfshow web entry information collection
猜你喜欢

MySQL之CRUD

Common PHP interview questions (1) (written PHP interview questions)

Bugku's steganography

Dark horse programmer - software testing -10 stage 2-linux and database -44-57 why learn database, description of database classification relational database, description of Navicat operation data, de
![P6183 [USACO10MAR] The Rock Game S](/img/f4/d8c8763c27385d759d117b515fbf0f.png)
P6183 [USACO10MAR] The Rock Game S

超越PaLM!北大碩士提出DiVeRSe,全面刷新NLP推理排行榜

Mongdb learning notes

Interpretation of Apache linkage parameters in computing middleware

可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成

CPU design related notes
随机推荐
【jvm】运算指令
Bugku's eyes are not real
Photoshop插件-动作相关概念-ActionList-ActionDescriptor-ActionList-动作执行加载调用删除-PS插件开发
想问下大家伙,有无是从腾讯云MYSQL同步到其他地方的呀?腾讯云MySQL存到COS上的binlog
Photoshop plug-in - action related concepts - actions in non loaded execution action files - PS plug-in development
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
Anaconda uses China University of science and technology source
计算中间件 Apache Linkis参数解读
easyOCR 字符识别
Can gbase 8A view the location of SQL statement history?
Two Bi development, more than 3000 reports? How to do it?
美团优选管理层变动:老将刘薇调岗,前阿里高管加盟
华为哈勃化身硬科技IPO收割机
Where is the operation of convertible bond renewal? Is it safer and more reliable to open an account
GPS原始坐标转百度地图坐标(纯C代码)
做研究无人咨询、与学生不交心,UNC助理教授两年教职挣扎史
Database learning - Database Security
机器学习框架简述
Detailed explanation of QT creator breakpoint debugger
1330: [example 8.3] minimum steps