• js排序方法


    function swap(ary, x, y) {
        if (x === y) return
        let temp = ary[x]
        ary[x] = ary[y]
        ary[y] = temp
    
    }
    
    //生成随机数:
    function random(n) {
        let ary = new Array(n)
        return ary.fill(0).map(_ => Math.random() * n | 0)
    
    }
    
    //选择排序( 不稳定):
    function selectSort(ary) {
        for (var i = 0; i < ary.length - 1; i++) {
            let min = i
            let change
            for (var j = i + 1; j < ary.length; j++) {
                if (ary[j] < ary[min]) {
                    min = j
                }
            }
            swap(ary, min, i)
            //     change = ary[min]
            //     ary[min] = ary[i]
            //     ary[i] = change
        }
        return ary
    }
    
    //冒泡排序:
    function bubbleSort(ary, comparator) {
        let flag
        let l = ary.length
        for (let i = l - 1; i >= 0; i--) {
            flag = false
            for (let j = 0; j < i; j++) {
                //if (ary[j + 1] < ary[j]) {
                if (comparator(ary[j], ary[j + 1]) > 0) {
                    flag = true
                    swap(ary, j, j + 1)
                }
            }
            if (!flag) {
                break
            }
        }
        return ary
    }
    
    function comparator(a, b) {
        if (a > b) {
            return 1
        } else {
            return -1
        }
    }
    
    //归并排序 稳定的
    function mergeSort(ary) {
        if (ary.length < 2) {
            return ary.slice()
        }
    
        var mid = Math.floor(ary.length / 2)
        var left = mergeSort(ary.slice(0, mid))
        var right = mergeSort(ary.slice(mid))
        var result = []
    
        while (left.length && right.length) {
            if (left[0] <= right[0]) {
                result.push(left.shift())
            } else {
                result.push(right.shift())
            }
        }
    
        result.push(...left, ...right)
    
        return result
    }
    
    
    
    //快速排序:
    function partition(ary, comparator, start = 0, end = ary.length - 1, ) {
        if (start >= end) {
            return
        }
    
        var pivotIndex = Math.floor(Math.random() * (end - start + 1) + start)
        var pivot = ary[pivotIndex]
    
        swap(ary, pivotIndex, end)
    
        for (var i = start - 1, j = start; j < end; j++) {
            if (comparator(ary[j], pivot) < 0) {
                i++
                swap(ary, i, j)
            }
        }
    
        swap(ary, i + 1, end)
        partition(ary, comparator, start, i)
        partition(ary, comparator, i + 2, end)
        return ary
    }
    
    function quickSort(ary, comparator = (a, b) => a - b) {
        return partition(ary, comparator)
    }
    
    //堆排序
    // function reheap(ary, topIndex, endIndex = ary.length - 1) {
    //     if (topIndex > endIndex) {
    //         return
    //     }
    
    //     var largestIndex = topIndex
    //     var leftIndex = topIndex * 2 + 1
    //     var rightIndex = topIndex * 2 + 1
    
    //     if (leftIndex <= endIndex && ary[leftIndex] > ary[largestIndex]) {
    //         largestIndex = leftIndex
    //     }
    
    //     if (rightIndex <= endIndex && ary[rightIndex] > ary[largestIndex]) {
    //         largestIndex = rightIndex
    //     }
    
    //     if (largestIndex !== topIndex) {
    //         swap(ary, largestIndex, topIndex)
    //         reheap(ary, largestIndex, endIndex)
    //     }
    // }
    
    // function heapify(ary) {
    //     for (var i = ary.length - 1; i >= 0; i--) {
    //         reheap(ary, i)
    //     }
    //     return ary
    // }
    
    // function heapSort(ary) {
    //     heapify(ary)
    //     for (var i = ary.length - 1; i >= 1; i--) {
    //         swap(ary, 0, i)
    //         reheap(ary, 0, i - 1)
    //     }
    //     return ary
    // }
    function reheap(ary, topIndex, endIndex = ary.length - 1) {
        if (topIndex > endIndex) {
            return
        }
    
        var largestIndex = topIndex
        var leftIndex = topIndex * 2 + 1
        var rightIndex = topIndex * 2 + 2
    
        if (leftIndex <= endIndex && ary[leftIndex] > ary[largestIndex]) {
            largestIndex = leftIndex
        }
        if (rightIndex <= endIndex && ary[rightIndex] > ary[largestIndex]) {
            largestIndex = rightIndex
        }
    
        if (largestIndex != topIndex) {
            swap(ary, largestIndex, topIndex)
            reheap(ary, largestIndex, endIndex)
        }
    }
    
    
    //把一个数组调整成一个堆
    function heapify(ary) {
        for (var i = ary.length - 1; i >= 0; i--) {
            reheap(ary, i)
        }
        return ary
    }
    
    // 堆排序
    function heapSort(ary) {
        heapify(ary)
        for (var i = ary.length - 1; i >= 1; i--) {
            swap(ary, 0, i)
            reheap(ary, 0, i - 1)
        }
        return ary
    }
    
    
    
    class PriorityQueue {
        constructor(comparator = (a, b) => a - b) {
            this.heap = []
            this.comparator = comparator
        }
    
        _show() {
            return
            showTree(el, this.heap)
        }
    
        _swap(i, j) {
            if (i !== j) {
                var temp = this.heap[i]
                this.heap[i] = this.heap[j]
                this.heap[j] = temp
            }
            this._show()
            return this
        }
    
        _reverseReheap(pos) {
            var parentPos = (pos - 1) / 2 | 0
            if (parentPos >= 0 && this.comparator(this.heap[pos], this.heap[parentPos]) < 0) {
                this._swap(pos, parentPos)
                this._reverseReheap(parentPos)
            }
        }
    
        push(val) {
            this.heap.push(val)
            this._reverseReheap(this.heap.length - 1)
        }
    
        _reheap(topIndex, endIndex = this.heap.length - 1) {
            if (topIndex > endIndex) {
                return
            }
    
            var ary = this.heap
    
            var largestIndex = topIndex
            var leftIndex = topIndex * 2 + 1
            var rightIndex = topIndex * 2 + 2
    
            if (leftIndex <= endIndex && this.comparator(ary[leftIndex], ary[largestIndex]) < 0) {
                largestIndex = leftIndex
            }
            if (rightIndex <= endIndex && this.comparator(ary[rightIndex], ary[largestIndex]) < 0) {
                largestIndex = rightIndex
            }
    
            if (largestIndex != topIndex) {
                swap(ary, largestIndex, topIndex)
                this._reheap(largestIndex, endIndex)
            }
    
        }
    
        pop() {
            if (heap.length === 1) {
                return this.heap.pop()
            }
            var result = this.heap[0]
            this.heap[0] = this.heap.pop()
            this._reheap(0)
            return result
        }
    }

    请给你自己加油!

  • 相关阅读:
    android app记录执行日志 捕获奔溃异常 ,存储日志到文件
    对扩展openflow协议的一点思考
    Echarts柱状图
    python实现插入排序
    MyBatis -- 对表进行增删改查(基于注解的实现)
    pat解题报告【1082】
    [Swift]LeetCode974. 和可被 K 整除的子数组 | Subarray Sums Divisible by K
    [Swift]LeetCode976. 三角形的最大周长 | Largest Perimeter Triangle
    [Swift]LeetCode345. 反转字符串中的元音字母 | Reverse Vowels of a String
    [Swift]LeetCode343. 整数拆分 | Integer Break
  • 原文地址:https://www.cnblogs.com/l8l8/p/8697847.html
Copyright © 2020-2023  润新知