插入 | 希尔 | 冒泡 | 快速 | 选择 | 归并排序

①插入排序—insertion sort

这里写图片描述

//Insertion Sorting
arr = [53,27,36,15,2,42];
function insertionSort(arr){
    var copy,len,temp;
    copy = arr.slice(0);
    len = copy.length;
    for(var i = 1;i < len;i++){
        temp = copy[i];
        var j = i -1;
        while(j >= 0 && copy[j] > temp){
            copy[j+1] = copy[j];
            j--;
        }
        copy[j+1] = temp;
    }
    return copy;
}
insertionSort(arr);//[2, 15, 27, 36, 42, 53]

②希尔排序—shell sort

//shell Sorting [insertionSort + gap]
arr = [53,27,36,15,2,42];
function shellSort(arr){
    var copy,len,gap;
    copy = arr.slice(0);
    len = copy.length;
    gap = Math.floor(len / 2);
    //easy to lose
    while(gap !== 0){
        for(var i = gap;i < len;i++){
            var j = i - gap;
            var temp = copy[i];
            while(j >= 0 && copy[j] > temp){
                copy[j + gap] = copy[j];
                j = j -gap;
            }
            copy[j + gap] = temp;
        }
         gap = Math.floor(gap / 2);
    }
    return copy;
}
shellSort(arr);//[2, 15, 27, 36, 42, 53]

③冒泡排序—bubble sort

这里写图片描述

//Bubble Sorting
arr = [53,27,36,15,2,42];
function bubbleSort(arr){
    var copy,len;
    copy = arr.slice(0);
    len = copy.length;
    for(var i = 0;i < len;i++){
        var temp = copy[i];
        for(var j = len-i-1;j < len;j++){
            if(copy[j] > copy[j+1]){
                temp = copy[j+1];
                copy[j+1] = copy[j];
                copy[j] = temp;
            }
        }
    }
    return copy;
}
bubbleSort(arr);//[2, 15, 27, 36, 42, 53]

④快速排序—quick sort

//Quick Sorting
arr = [53,27,36,15,2,42];
function quickSort(arr){
    var copy,pivotIndex,pivot;
    copy = arr.slice(0);
    //copy.length
    if(copy.length <= 1) return copy;
    pivotIndex = Math.floor(copy.length / 2);
    pivot = copy.splice(pivotIndex,1)[0];
    var left = [];
    var right = [];
    for(var i = 0;i < copy.length;i++){
        if(copy[i] < pivot){
            left.push(copy[i]);
        }else{
            right.push(copy[i]);
        }
    }
    return quickSort(left).concat(pivot,quickSort(right));
}
quickSort(arr);//[2, 15, 27, 36, 42, 53]

⑤选择排序—selection sort

//Selection Sorting
arr = [53,27,36,15,2,42];
function selectionSort(arr){
    var copy,len,minIndex,temp;
    copy = arr.slice(0);
    len = copy.length;
    for(var i = 0;i < len-1;i++){
        minIndex = i;
        for(var j = i+1;j < len;j++){
            if(copy[minIndex] > copy[j]){
                minIndex = j;
            }
        }
        temp = copy[i];
        copy[i] = copy[minIndex];
        copy[minIndex] = temp;
    }
    return copy;
}
selectionSort(arr);//[2, 15, 27, 36, 42, 53]

⑥归并排序—merge sort

//Merge Sorting
arr = [53,27,36,15,2,42];
function mergeSort(arr){
    if(arr.length < 2) return arr;
    var middle = Math.floor(arr.length / 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;
}
mergeSort(arr);//[2, 15, 27, 36, 42, 53]

参考博客:
【JS家的排序算法】:http://www.jianshu.com/p/1b4068ccd505
【基本算法学习(一)之希尔排序(JS)】:https://segmentfault.com/a/1190000006950201

gitHub地址【可自行下载修改测试】:https://github.com/yycer/share

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值