算法---数组的去重和排序

数组的去重
let arr = [1,2,3,5,7,4,3]
let list = []

list = [...new Set(arr)]
list = Array.from(new Set(arr))
for(let i=0;i
拿数组中的每项向新容器中存储,如果已经存储过了,把当前项干掉
let obj = {}
for(let i=0;i
arr.sort((a,b) => a-b)
arr.join('@') + '@'
let reg = /(\d+@)\1*/g
arr.replace(reg, (val, group1) => {
    arr.push(Number(group1.slice(0,group1.length-1)))
})

数组的排序

// 冒泡排序
function bubble(arr) {
    let temp = null
    for(let i=0; iarr[j+1]) {
                temp = arr[j]
                arr[j] =  arr[j + 1]
                arr[j + 1] = temp
            }
        }
    }
    return arr
}
// 插入排序
for(let i=o; i=0;j--) {
        let b = handle[j]
        if(a > b) {
            handle.splice(j+1,0,a)
            break
        }
        if(j === 0) {
            handle.unshift(a)
        }
    }
}
// 快排序
function quick() {
    if(arr.length <= 1) {
        return arr
    }
    let middleIndex = Math.floor(arr.length/2)
    let middleValue = arr.splice(middleIndex,1)[0]
    let arrLeft = [], arrRight = [];
    for(let i=0; i

你可能感兴趣的:(算法,数据结构,javascript)