六种JavaScript数组排序方法

冒泡排序

//冒泡排序
function sort(arr) {
        for (let i = 0; i < arr.length - 1; i++) {
            for (let j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] < arr[j + 1]) {
                    let temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        return arr.toString();
    }
    console.log(sort([23, 45, 65, 16, 97, 12, 54]));

// 冒泡排序优化
    function sort2(arr) {
        for (let i = 0; i < arr.length - 1; i++) {
            let flag = false;
            for (let j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    let temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    flag = true;
                }
            }
            if (!flag) {
                break;
            }
        }
        return arr.toString();
    }
// ES6中的sort
let newArr = arr.sort(function (a, b) {
   return a - b  //升序 默认
   // return b - a //倒序
});
console.log(newArr);

选择排序

let arr1 = [23, 45, 65, 16, 97, 12, 54];
    function selectSort(arr) {
        for (let i = 0; i < arr.length; i++) {
            let min = i;
            for (let j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[min]) {
                    min = j;
                }
            }
            if (min != i) {
                let temp = arr[i];
                arr[i] = arr[min];
                arr[min] = temp;
            }
        }
        return arr;
    }
    console.log(selectSort(arr1));

插入排序

 let arr1 = [23, 45, 65, 16, 97, 12, 54];
    function insertSort(arr) {
        for (let i = 1; i < arr.length; i++) {
            let temp = arr[i];
            let j = i - 1;
            while (j >= 0 && arr[j] > temp) {
                arr[j + 1] = arr[j];
                j--;
            }
            arr[j + 1] = temp;
        }
        return arr;
    }
    console.log(insertSort(arr1));

归并排序

let arr1 = [23, 45, 65, 16, 97, 12, 54];
    function mergeSort(arr) {
        if (arr.length <= 1) {
            return arr;
        }
        let middle = Math.floor(arr.length / 2);
        let left = arr.slice(0, middle);
        let right = arr.slice(middle);
        return merge(mergeSort(left), mergeSort(right));
    }
    console.log(mergeSort(arr1));

快速排序

 let arr1 = [23, 45, 65, 16, 97, 12, 54];
    function quickSort(arr) {
        if (arr.length <= 1) {
            return arr;
        }
        let pivotIndex = Math.floor(arr.length / 2);
        let pivot = arr.splice(pivotIndex, 1)[0];
        let left = [];
        let right = [];
        for (let i = 0; i < arr.length; i++) {
            if (arr[i] < pivot) {
                left.push(arr[i]);
            } else {
                right.push(arr[i]);
            }
        }
        return quickSort(left).concat([pivot], quickSort(right));
    }
    console.log(quickSort(arr1));

希尔排序

   let arr1 = [23, 45, 65, 16, 97, 12, 54];
    function shellSort(arr) {
        let len = arr.length;
        let temp;
        let gap = 1;
        while (gap < len / 3) {
            gap = gap * 3 + 1;
        }
        for (gap; gap > 0; gap = Math.floor(gap / 3)) {
            for (let i = gap; i < len; i++) {
                temp = arr[i];
                for (let j = i - gap; j >= 0 && arr[j] > temp; j -= gap) {
                    arr[j + gap] = arr[j];
                }
                arr[j + gap] = temp;
            }
        }
        return arr;
    }
    console.log(shellSort(arr1));

你可能感兴趣的:(javascript,前端)