当前位置:网站首页>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)边栏推荐
- 我想咨询一下,mysql一个事务对于多张表的更新,怎么保证数据一致性的?
- 当代人的水焦虑:好水究竟在哪里?
- Detailed explanation of usememo, memo, useref and other relevant hooks
- What are the domestic formal futures company platforms in 2022? How about founder metaphase? Is it safe and reliable?
- CODING DevSecOps 助力金融企业跑出数字加速度
- Stm32+bh1750 photosensitive sensor obtains light intensity
- maxcompute有没有能查询 表当前存储容量的大小(kb) 的sql?
- Leetcode: Shortest Word Distance II
- Selection and use of bceloss, crossentropyloss, sigmoid, etc. in pytorch classification
- Redis distributed lock principle and its implementation with PHP (2)
猜你喜欢

Your childhood happiness was contracted by it

DVWA range clearance tutorial

SQL Server learning notes

lv_font_conv离线转换

Detailed explanation of QT creator breakpoint debugger

Garbage collection mechanism of PHP (theoretical questions of PHP interview)

qt creater断点调试程序详解

Install and configure Jenkins

Run faster with go: use golang to serve machine learning

Creation and optimization of MySQL index
随机推荐
"Sequelae" of the withdrawal of community group purchase from the city
The elimination strategy of redis
Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking
STM32+BH1750光敏传感器获取光照强度
Interview shock 62: what are the precautions for group by?
mapper. Comments in XML files
Photoshop插件-动作相关概念-非加载执行动作文件中动作-PS插件开发
计算中间件 Apache Linkis参数解读
Detailed explanation of QT creator breakpoint debugger
What are CSRF, XSS, SQL injection, DDoS attack and timing attack respectively and how to prevent them (PHP interview theory question)
Bugku's Ah Da
Severlet learning foundation
Bugku alert
CPU design practice - Chapter 4 practical task 2 using blocking technology to solve conflicts caused by related problems
sql server学习笔记
Redis distributed lock principle and its implementation with PHP (1)
Interpretation of Apache linkage parameters in computing middleware
Jmeter性能测试:ServerAgent资源监控
sql server char nchar varchar和nvarchar的区别
美团优选管理层变动:老将刘薇调岗,前阿里高管加盟