当前位置:网站首页>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)边栏推荐
- CODING DevSecOps 助力金融企业跑出数字加速度
- Redis distributed lock principle and its implementation with PHP (1)
- I collect multiple Oracle tables at the same time. After collecting for a while, I will report that Oracle's OGA memory is exceeded. Have you encountered it?
- 30岁汇源,要换新主人了
- Reconnaissance des caractères easycr
- 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
- Aike AI frontier promotion (7.5)
- JS bright blind your eyes date selector
- CPU design practice - Chapter 4 practice task 3 use pre delivery technology to solve conflicts caused by related issues
- Calculate weight and comprehensive score by R entropy weight method
猜你喜欢

Creation and optimization of MySQL index

Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology

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

MySQL之CRUD

Bugku alert

Bugku cyberpunk
![P6183 [USACO10MAR] The Rock Game S](/img/f4/d8c8763c27385d759d117b515fbf0f.png)
P6183 [USACO10MAR] The Rock Game S

Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel

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

数据库学习——数据库安全性
随机推荐
Ionic Cordova project modification plug-in
Redis distributed lock principle and its implementation with PHP (2)
Bugku's steganography
GPS original coordinates to Baidu map coordinates (pure C code)
Severlet learning foundation
Thymeleaf uses background custom tool classes to process text
OSI 七层模型
可转债打新在哪里操作开户是更安全可靠的呢
Talking about how dataset and dataloader call when loading data__ getitem__ () function
Photoshop plug-in - action related concepts - actions in non loaded execution action files - PS plug-in development
Coding devsecops helps financial enterprises run out of digital acceleration
MySQL之CRUD
sql server学习笔记
Cartoon: programmers don't repair computers!
I include of spring and Autumn
CODING DevSecOps 助力金融企业跑出数字加速度
lvgl 显示图片示例
Brief introduction of machine learning framework
Common MySQL interview questions
超越PaLM!北大硕士提出DiVeRSe,全面刷新NLP推理排行榜