美文网首页前端博文
JS排序算法总结

JS排序算法总结

作者: 不得不爱XIN | 来源:发表于2019-05-24 14:51 被阅读0次

    冒泡排序

    function bubbleSort(arr) {
        var len = arr.length;
        for (var i = 0; i < len; i++) {
            for (var j = 0; j < len - 1 - i; j++) {
                if (arr[j] > arr[j+1]) {        //相邻元素两两对比
                    var temp = arr[j+1];        //元素交换
                    arr[j+1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        return arr;
    }
    

    选择排序

    function selectionSort(arr) {
        var len = arr.length;
        var minIndex, temp;
        for (var i = 0; i < len - 1; i++) {
            minIndex = i;
            for (var j = i + 1; j < len; j++) {
                if (arr[j] < arr[minIndex]) {     //寻找最小的数
                    minIndex = j;                 //将最小数的索引保存
                }
            }
            temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }
        return arr;
    }
    

    插入排序

    function insertionSort(arr) {
        var len = arr.length;
        var preIndex, current;
        for (var i = 1; i < len; i++) {
            preIndex = i - 1;
            current = arr[i];
            while(preIndex >= 0 && arr[preIndex] > current) {
                arr[preIndex+1] = arr[preIndex];
                preIndex--;
            }
            arr[preIndex+1] = current;
        }
        return arr;
    }
    

    希尔排序

    希尔排序是插入排序的一种更高效率的实现。它与插入排序的不同之处在于,它会优先比较距离较远的元素。希尔排序的核心在于间隔序列的设定。

    function shellSort(arr) {
        var len = arr.length,
            temp,
            gap = 1;
        while(gap < len/3) {          //动态定义间隔序列
            gap =gap*3+1;
        }
        for (gap; gap > 0; gap = Math.floor(gap/3)) {
            for (var i = gap; i < len; i++) {
                temp = arr[i];
                for (var j = i-gap; j >= 0 && arr[j] > temp; j-=gap) {
                    arr[j+gap] = arr[j];
                }
                arr[j+gap] = temp;
            }
        }
        return arr;
    }
    

    归并排序

    function mergeSort(arr) {  //采用自上而下的递归方法
        var len = arr.length;
        if(len < 2) {
            return arr;
        }
        var middle = Math.floor(len / 2),
            left = arr.slice(0, middle),
            right = arr.slice(middle);
        return merge(mergeSort(left), mergeSort(right));
    }
    
    function merge(left, right)
    {
        var result = [];
    
        while (left.length && right.length) {
            if (left[0] <= right[0]) {
                result.push(left.shift());
            } else {
                result.push(right.shift());
            }
        }
    
        while (left.length)
            result.push(left.shift());
    
        while (right.length)
            result.push(right.shift());
    
        return result;
    }
    

    快速排序

    方法1:

    const quickSort = function (array) {
        if (array.length <= 1) {
            return array;
        }
        const pivotIndex = parseInt(array.length / 2);
        const pivot = Number(array.splice(pivotIndex, 1));
        const left = [];    const right = [];
        for (let i = 0; i < array.length; i++) {
            if (array[i] < pivot) {
                left.push(array[i]);
            } else {
                right.push(array[i]);
            }
        }
        return quickSort(left).concat([pivot], quickSort(right));
    };
    

    方法2:

    function quickSort(array, left, right) {
        if (left < right) {
            let x = array[right], i = left - 1;
            for (let j = left; j <= right; j++) {
                if (array[j] <= x) {
                    i++;
                    const temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
            quickSort(array, left, i - 1);
            quickSort(array, i + 1, right);
        }
        return array;
    }
    // left = 0; right = array.length-1
    

    堆排序

    function heapSort(array) {
        let length = array.length;
        for (let i = parseInt(array.length / 2) - 1; i >= 0; i--) {
            heap(array, i, length);
        }
        for (let j = length - 1; j >= 1; j--) {
            const temp = array[0];
            array[0] = array[j];
            array[j] = temp;
            heap(array, 0, --length);
        }
        return array;
    }
    function heap(array, x, length) {
        let l = 2 * x + 1, r = 2 * x + 2, largest = x;
        if (l < length && array[l] > array[largest]) {
            largest = l;
        }
        if (r < length && array[r] > array[largest]) {
            largest = r;
        }
        if (largest != x) {
            const temp = array[x];
            array[x] = array[largest];
            array[largest] = temp;
            heap(array, largest, length);
        }
    }
    

    计数排序

    function countSort(array) {
        const newArray = [], C = [];
        let min = array[0];
        let max = array[0];
        for (let i = 0; i < array.length; i++) {
            if (min >= array[i]) {
                min = array[i];
            }
            if (max <= array[i]) {
                max = array[i];
            }
            if (C[array[i]] = C[array[i]]) {
                C[array[i]]++;
            } else {
                C[array[i]] = 1;
            }
        }
        for (let j = min; j < max; j++) {
            C[j + 1] = (C[j + 1] || 0) + (C[j] || 0);
        }
        for (let k = array.length - 1; k >= 0; k--) {
            newArray[C[array[k]] - 1] = array[k];
            C[array[k]]--;
        }
        return newArray;
    }
    

    对比

    function countingSort(arr, maxValue) {
        var bucket = new Array(maxValue+1),
            sortedIndex = 0;
            arrLen = arr.length,
            bucketLen = maxValue + 1;
    
        for (var i = 0; i < arrLen; i++) {
            if (!bucket[arr[i]]) {
                bucket[arr[i]] = 0;
            }
            bucket[arr[i]]++;
        }
    
        for (var j = 0; j < bucketLen; j++) {
            while(bucket[j] > 0) {
                arr[sortedIndex++] = j;
                bucket[j]--;
            }
        }
    
        return arr;
    }
    

    相关文章

      网友评论

        本文标题:JS排序算法总结

        本文链接:https://www.haomeiwen.com/subject/vkfgzqtx.html