当前位置:网站首页>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)
边栏推荐
- P6183 [USACO10MAR] The Rock Game S
- 我想咨询一下,mysql一个事务对于多张表的更新,怎么保证数据一致性的?
- 美团优选管理层变动:老将刘薇调岗,前阿里高管加盟
- How can I quickly check whether there is an error after FreeSurfer runs Recon all—— Core command tail redirection
- 社区团购撤城“后遗症”
- Install PHP extension spoole
- Ctfshow web entry explosion
- go学习 ------jwt的相关知识
- JMeter performance test: serveragent resource monitoring
- 市值蒸发超百亿美元,“全球IoT云平台第一股”赴港求生
猜你喜欢
Run faster with go: use golang to serve machine learning
百亿按摩仪蓝海,难出巨头
Super wow fast row, you are worth learning!
你童年的快乐,都是被它承包了
Machine learning notes - gray wolf optimization
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
数据库学习——数据库安全性
Ten billion massage machine blue ocean, difficult to be a giant
CPU design related notes
Crud de MySQL
随机推荐
PHP high concurrency and large traffic solution (PHP interview theory question)
机器学习框架简述
I spring and autumn blasting-1
超越PaLM!北大硕士提出DiVeRSe,全面刷新NLP推理排行榜
CPU design related notes
MySQL之CRUD
Mysql---- function
I include of spring and Autumn
[12 classic written questions of array and advanced pointer] these questions meet all your illusions about array and pointer, come on!
lvgl 显示图片示例
Common MySQL interview questions (1) (written MySQL interview questions)
Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
Au - delà du PARM! La maîtrise de l'Université de Pékin propose diverse pour actualiser complètement le classement du raisonnement du NLP
GPS原始坐标转百度地图坐标(纯C代码)
CPU design practice - Chapter 4 practice task 3 use pre delivery technology to solve conflicts caused by related issues
[C question set] of Ⅷ
我想咨询一下,mysql一个事务对于多张表的更新,怎么保证数据一致性的?
ionic cordova项目修改插件
CODING DevSecOps 助力金融企业跑出数字加速度
JMeter performance test: serveragent resource monitoring