当前位置:网站首页>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)边栏推荐
- mapper. Comments in XML files
- Database learning - Database Security
- [detailed explanation of Huawei machine test] happy weekend
- Misc Basic test method and knowledge points of CTF
- 超越PaLM!北大硕士提出DiVeRSe,全面刷新NLP推理排行榜
- The difference between SQL Server char nchar varchar and nvarchar
- 计算中间件 Apache Linkis参数解读
- Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
- NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
- Change multiple file names with one click
猜你喜欢

Change multiple file names with one click

qt creater断点调试程序详解

OSI 七层模型

lv_font_conv离线转换

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

sql server学习笔记

How can I quickly check whether there is an error after FreeSurfer runs Recon all—— Core command tail redirection

1330:【例8.3】最少步数

Bugku's Ping

Common redis data types and application scenarios
随机推荐
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
STM32+BH1750光敏传感器获取光照强度
Common MySQL interview questions (1) (written MySQL interview questions)
I want to inquire about how to ensure data consistency when a MySQL transaction updates multiple tables?
sql server char nchar varchar和nvarchar的区别
How to paste the contents copied by the computer into mobaxterm? How to copy and paste
Fr exercise topic --- comprehensive question
SQL Server learning notes
Photoshop plug-in - action related concepts - actions in non loaded execution action files - PS plug-in development
Number protection AXB function! (essence)
计算中间件 Apache Linkis参数解读
easyOCR 字符识别
Crud de MySQL
当代人的水焦虑:好水究竟在哪里?
The difference between abstract classes and interfaces in PHP (PHP interview theory question)
The elimination strategy of redis
Calculate weight and comprehensive score by R entropy weight method
百亿按摩仪蓝海,难出巨头
Select sort and bubble sort
Run faster with go: use golang to serve machine learning