算法 - 排序搜索

133 阅读2分钟

一、冒泡排序

  1. 比较所有相邻元素,如果第一个比第二个大,则交换他们
  2. 一轮下来,可以保证最后一个数是最大的
  3. 执行n-1轮,就可以完成排序了
Array.prototype.bubbleSort = function () {
    for (let i = 0; i < this.length - 1; i++) {
        for (let j = 0; j < this.length - 1 - i; j++) {
            if (this[j] > this[j + 1]) {
                const temp = this[j]
                this[j] = this[j + 1]
                this[j + 1] = temp
            }
        }
    }
}

const arr = [5, 4, 3, 2, 1]
arr.bubbleSort()

二、选择排序

  1. 找出数组中最小值,选中它并将其放置在第一位
  2. 接着找到第二小的值,选中它并将其放置在第二位
  3. 以此类推,执行 n-1 轮
Array.prototype.selectionSort = function () {
    for (let i = 0; i < this.length - 1; i++) {
        let indexMin = i // 记录最小值下标
        for (let j = i; j < this.length; j++) {
            if (this[j] < this[indexMin]) {
                indexMin = j
            }
        }
        if (indexMin !== i) {
            const temp = this[i]
            this[i] = this[indexMin]
            this[indexMin] = temp
        }
    }
}

const arr = [5, 4, 3, 2, 1]
arr.selectionSort()

三、插入排序

  1. 从第二个数开始往前比
  2. 如果第一个数比它大,第一个数就往后排
  3. 那它就和前面的数比较,插入合适和位置
  4. 以此类推进行到最后一个数
Array.prototype.insertionSort = function () {
    for (let i = 1; i < this.length; i++) {
        const temp = this[i]
        let j = i
        while (j > 0) {
            if (this[j - 1] > temp) {
                this[j] = this[j - 1]
            } else {
                break;
            }
            j = j - 1
        }
        this[j] = temp
    }
}

const arr = [5, 4, 3, 2, 1]
arr.insertionSort()

四、归并排序

  • 归并排序思路:
  1. 分:把数组劈成两半,再递归地对子数组进行“分”操作,直到分成一个个单独的数
  2. 合:把两个数合并为有序数组,再对有序数组进行合并,直到全部子数组合并为一个完整数组
  • 合并有序数组:
  1. 新建一个空数组res,用于存放最终排序后的数组
  2. 比较两个有序数组的头部,较小者出队并推出res中
  3. 如果两个数组还有值,就重复第二步
分的时间复杂度是O(logN)
合的时间复杂度是O(n)
总体时间复杂度是O(n*logN)
Array.prototype.mergeSort = function () {
    const rec = (arr) => {
        if (arr.length === 1) {
            return arr
        }
        const mid = Math.floor(arr.length / 2);
        const left = arr.slice(0, mid)
        const right = arr.slice(mid, arr.length)
        const orderLeft = rec(left)
        const orderRight = rec(right)
        const res = []
        while (orderLeft.length || orderRight.length) {
            if (orderLeft.length && orderRight.length) {
                res.push(orderLeft[0] < orderRight[0] ? orderLeft.shift() : orderRight.shift())
            }
            else if (orderLeft.length) {
                res.push(orderLeft.shift())
            }
            else if (orderRight.length) {
                res.push(orderRight.shift())
            }
        }
        return res
    }
    const res = rec(this)
    res.forEach((n, i) => {
        this[i] = n
    })
}

const arr = [5, 4, 3, 2, 1]
arr.mergeSort()

五、快速排序

  • 快速排序思路:
  1. 分区:从数组中任意选择一个元素作为“基准”,所有比基准小的元素放在基准的前面,比基准大的元素放在基准的后面
  2. 递归:递归地对基准前后的子数组进行分区
递归操作的时间复杂度是O(logN)
分区操作的时间复杂度是O(n)
总体时间复杂度是O(n*logN)
Array.prototype.quickSort = function () {
    const rec = (arr) => {
        if (arr.length === 1 || arr.length === 0) { return arr }
        const left = []
        const right = []
        const mid = arr[0]
        for (let i = 1; i < arr.length; i++) {
            if (arr[i] < mid) {
                left.push(arr[i])
            } else {
                right.push(arr[i])
            }
        }
        return [...rec(left), mid, ...rec(right)]
    }
    const res = rec(this)
    res.forEach((n, i) => { this[i] = n })
}

const arr = [5, 4, 3, 2, 1, 0]
arr.quickSort()

六、二分搜索

  • 二分搜索的前提是数组是有序的

  • 二分搜索思路:

  1. 从数组的中间元素开始,如果中间元素正好是目标值,则搜索结束
  2. 如果目标值大于或小于中间元素,则在大于或小于中间元素的那一半数组中搜索
每一次比较都使得搜索范围缩小一半
时间复杂度:O(logN)
Array.prototype.binarySearch = function (item) {

    let low = 0 // 数组最小下标
    let high = this.length - 1 // 数组最大下标

    while (low <= high) {
        const mid = Math.floor((low + high) / 2)
        const element = this[mid]
        if (element < item) {
            low = mid + 1
        }
        else if (element > item) {
            high = mid - 1
        }
        else {
            return mid
        }
    }
    return -1
}

const arr = [1, 2, 3, 4, 5]
console.log(arr.binarySearch(11))