JS实现各类排序算法

冒泡排序:

let arr = [2, 4, 1, 5, 8, 22, 10, 21, 15, 14, 12, 5, 6, 2]

function sort(arr) {
            for (let i = arr.length - 1; i > 0; i--) {
                for (let j = 0; j < i; j++) {
                    if (arr[j] > arr[j + 1]) {
                        let temp = arr[j]
                        arr[j] = arr[j + 1]
                        arr[j + 1] = temp
                    }
                }
            }
            return arr
}

  

选择排序:

let arr = [2, 4, 1, 5, 8, 22, 10, 21, 15, 14, 12, 5, 6, 2]

function sort(arr) {
            let min = null
            for (let i = 0, len = arr.length; i < len; i++) {
                min = arr[i]
                for (let j = i + 1; j < len; j++) {
                    if (arr[j] < min) {
                        let big = min
                        min = arr[j]
                        arr[j] = big
                    }
                }
                arr[i] = min
            }
            return arr
}

  

插入排序:

let arr = [2, 4, 1, 5, 8, 22, 10, 21, 15, 14, 12, 5, 6, 2]

function sort(arr) {
            for (let i = 1, len = arr.length; i < len; i++) {
                let j = i
                let tmp = arr[i]
                while (j > 0 && tmp < arr[j - 1]) {
                    arr[j] = arr[j - 1]
                    j--
                }
                if (i !== j) {
                    arr[j] = tmp
                }
            }
            return arr
}

  

归并排序:

let arr = [2, 4, 1, 5, 8, 22, 10, 21, 15, 14, 12, 5, 6, 2]

function sort(arr) {
            const middle = Math.floor(arr.length / 2)
            if (arr.length < 2) {
                return arr
            }
            const left = arr.slice(0, middle)
            const right = arr.slice(middle)
            return merge(sort(left), sort(right))
        }

function merge(left, right) {
            let result = []
            // 如果任何一个数组为空,就退出循环
            while (left.length && right.length) {
                // 从左右子数组的最小元素中选择较小的元素
                if (left[0] < right[0]) {
                    result.push(left.shift())
                } else {
                    result.push(right.shift())
                }
            }
            // 连接剩余的元素,防止没有把两个数组遍历完整
            return [...result, ...left, ...right]
}

  

快速排序:

let arr = [2, 4, 1, 5, 8, 22, 10, 11, 15, 14, 12, 5, 6, 2]

function sort(arr) {
            if (arr.length < 2) {
                return arr
            }

            let pIndex = Math.floor(arr.length / 2); // 从中间开始确立基准值
            let pVal = arr.splice(pIndex, 1)[0]; // 分离基准值

            let left = []; // 存放小于基数的值
            let right = []; // // 存放大于基数的值

            // 循环数组,将小于等于基准值的放左边,大于基准的放右边
            for (let i = 0, len = arr.length; i < len; i++) {
                if (arr[i] <= pVal) {
                    left.push(arr[i])
                } else {
                    right.push(arr[i])
                }
            }

            return sort(left).concat([pVal], sort(right))
}

  

 

posted @ 2022-07-27 10:42  火星_PGY  阅读(56)  评论(0编辑  收藏  举报