(算法)排序复习
2020-12-13 04:30
标签:result math 逻辑 pos sel 前端 monk ret 有一个 原理: 当i=0,首先找到最小的元素放在起始的位置,然后i=1,再然后找到最小的元素放到最左i=1的位置,然后i=2,... 动画演示 动画 逻辑解释 首先,讲数字分割成两份区域,在讲数字分割成两份区域,只到每块区域只有一个元素(这个过程是递归过程) 截下来,讲分割的每块区域进行合并组合 计数排序 动画 逻辑 动画 自己修改版(不知道会不会写错,因为我自己写错了,所以还是改成前端看起来比较顺眼的) ............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................... (算法)排序复习 标签:result math 逻辑 pos sel 前端 monk ret 有一个 原文地址:https://www.cnblogs.com/fangdongdemao/p/11111785.html选择排序
const SelectSort = arr => {
if (arr == null || arr.length {
arr[i] = arr[i] ^ arr[j]
arr[j] = arr[i] ^ arr[j]
arr[i] = arr[i] ^ arr[j]
}
let arr = [1, 2, 10, 3, 4, 5, 6]
console.log(SelectSort(arr))
插入排序
const InsertSort = arr => {
if (arr == null || arr.length =0&&arr[j]>arr[j+1];j--) {
swap(arr,j,j+1)
}
}
return arr
}
const swap = (arr, i, j) => {
arr[i] = arr[i] ^ arr[j]
arr[j] = arr[i] ^ arr[j]
arr[i] = arr[i] ^ arr[j]
}
let arr = [1, 2, 10, 3, 4, 5, 6]
console.log(InsertSort(arr))
归并排序
const mergeSort=array=>{
if(array.length
[9] [3]|[4] [5] |[10][9] | [6] [4]
出栈
[3,9]|[4,5] |[9,10]|[4,6]
[3,4][5,9]
[3,4,5,9] | [4,6,9,10]
//left[0]{
let result=[];
while (left.length > 0 && right.length > 0) {
if (left[0]
桶排序
const bucketSort = arr => {
if (arr == null || arr.length 0)
let oArr = []
for (let i = 0; i 0) {
oArr.push(i)
}
}
return oArr
}
console.log(bucketSort([10, 9, 7, 6, 3]))
基数排序
//获取每位的数字位数
const getDigit = (x, d) => {
let a = Math.pow(10, d)
return Math.floor(x / a) % 10
}
const radixSort=(arr)=> {
//最大位数
let maxDigit = String(Math.max(...arr)).length
let counter = []
for (let i = 0; i
//获取每位的数字位数
const getDigit = (x, d) => {
let a = Math.pow(10, d)
return Math.floor(x / a) % 10
}
const radixSort = (arr) => {
let maxDigit = String(Math.max(...arr)).length
let counter = Array.from({length: 10}, () => [])
for (let i = 0; i {
while (val.length > 0) {
arr[b++]=val.shift()
}
})
}
return arr
}
let arr = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]
console.log(radixSort(arr))
//[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50]
上一篇:《汇编语言》-- 控制执行流程
下一篇:Python学习9——异常