当前位置:网站首页>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)边栏推荐
- Select sort and bubble sort
- Interpretation of Apache linkage parameters in computing middleware
- [JVM] operation instruction
- Install PHP extension spoole
- 【jvm】运算指令
- Reconnaissance des caractères easycr
- Handwriting promise and async await
- [recruitment position] infrastructure software developer
- [C question set] of Ⅷ
- Ten billion massage machine blue ocean, difficult to be a giant
猜你喜欢

Misc Basic test method and knowledge points of CTF

NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读

Drive brushless DC motor based on Ti drv10970

Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking

Under the crisis of enterprise development, is digital transformation the future savior of enterprises

Bugku alert

P1451 求细胞数量/1329:【例8.2】细胞

Creation and use of thymeleaf template

Crud de MySQL

Interview shock 62: what are the precautions for group by?
随机推荐
Bugku easy_ nbt
P6183 [USACO10MAR] The Rock Game S
The difference between abstract classes and interfaces in PHP (PHP interview theory question)
Interview shock 62: what are the precautions for group by?
Stop B makes short videos, learns Tiktok to die, learns YouTube to live?
Reconnaissance des caractères easycr
mapper.xml文件中的注释
STM32+BH1750光敏传感器获取光照强度
Anaconda uses China University of science and technology source
Under the crisis of enterprise development, is digital transformation the future savior of enterprises
做研究无人咨询、与学生不交心,UNC助理教授两年教职挣扎史
Type declaration of all DOM elements in TS
[JVM] operation instruction
计算中间件 Apache Linkis参数解读
Crud of MySQL
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
What are the domestic formal futures company platforms in 2022? How about founder metaphase? Is it safe and reliable?
[C question set] of Ⅷ
Creation and optimization of MySQL index
queryRunner. Query method