当前位置:网站首页>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)边栏推荐
- Huawei Hubble incarnation hard technology IPO harvester
- CODING DevSecOps 助力金融企业跑出数字加速度
- What are the domestic formal futures company platforms in 2022? How about founder metaphase? Is it safe and reliable?
- 超越PaLM!北大碩士提出DiVeRSe,全面刷新NLP推理排行榜
- Hongmeng system -- Analysis from the perspective of business
- Select sort and bubble sort
- CPU design practice - Chapter 4 practice task 3 use pre delivery technology to solve conflicts caused by related issues
- Mysql---- function
- Bugku alert
- 社区团购撤城“后遗症”
猜你喜欢

Number protection AXB function! (essence)

社区团购撤城“后遗症”

Bugku easy_ nbt

Selection and use of bceloss, crossentropyloss, sigmoid, etc. in pytorch classification

Creation and use of thymeleaf template

Detailed explanation of QT creator breakpoint debugger

Interpretation of Apache linkage parameters in computing middleware

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

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

CODING DevSecOps 助力金融企业跑出数字加速度
随机推荐
CPU design practice - Chapter 4 practice task 3 use pre delivery technology to solve conflicts caused by related issues
The difference between abstract classes and interfaces in PHP (PHP interview theory question)
Number protection AXB function! (essence)
CODING DevSecOps 助力金融企业跑出数字加速度
GPS original coordinates to Baidu map coordinates (pure C code)
Detailed explanation of usememo, memo, useref and other relevant hooks
Drive brushless DC motor based on Ti drv10970
PHP high concurrency and large traffic solution (PHP interview theory question)
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
【jvm】运算指令
百亿按摩仪蓝海,难出巨头
Photoshop插件-动作相关概念-非加载执行动作文件中动作-PS插件开发
Long list optimized virtual scrolling
OSI 七层模型
Cartoon: what are the attributes of a good programmer?
mapper. Comments in XML files
CPU design related notes
Reasons and solutions for redis cache penetration and cache avalanche
Anaconda uses China University of science and technology source
lv_font_conv离线转换