js排序算法总汇

各种排序以数字排序为例编写排序代码

1、冒泡排序  (时间复杂度O(n*n))

比较相邻元素,如果第一个比第二个大,则交换它们

一轮循环下来遍历交换下来,可以保证最后一个数是最大的

执行上面n - 1轮

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

2、选择排序(时间复杂度O(n*n))

找到数组中最小值,选中它并将其放置在第一的位置

接着找到第二小,选中它并将其放置到第二的位置上

以此类推,执行n - 1轮

Array.prototype.selectionSort = function(){
    for(i = 0; i < this.length - 1; i++){
        let indexMin = i;
        for(let j = i; j < this.length; j++){
            if(this[j] < this[indexMin]){
                index = j;
            }
        };
        if(indexMin != i){
            const temp = this[i];
            this[i] = this[indexMin];
            this[indexMin] = temp;
        }
    }
}

3、插入排序(时间复杂度O(n*n))

从第二个数开始往前比

比它大就往后排

一次类推进行到最后一个数

Array.prototype.insertSort = function(){
    for(let i= 1; i < this.length; i++){
        let temp = this[i];
        let j = i;
        while(j >0){
            if(this[j - 1] > temp){
                this[j] = this[j - 1]
            }else{
                break;
            }

            j--;
        }
        this[j] = temp
    }
}

4、归并排序(时间复杂度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;});
}

5、快速排序(时间复杂度O(n*logn))

分区:从数组中任意选择一个“基准”,所有比基准小的元素放在基准前面,比基准大的元素放在基准后面。

Array.prototype.quickSort = function(){
    let rec = (arr)=>{
        if(arr.length === 1){return arr}
        let left = [];
        let right = [];
        let 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;
    })
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值