1.插入排序

function insertSort(array){
    var i = 0,
        j = 0,
        len = array.length,
        flag = 0
    for(i=1; i<len; i++){
        if(array[i]<array[i-1]){
            flag = array[i]
            array[i] = array[i-1]
            for(j=i-2; j>=0 && flag < array[j]; j--){
                array[j+1] = array[j]
            }
            array[j+1] = flag
        }
    }
}
  • 插入排序是稳定的排序方法

  • 时间复杂度 O(n²)

2.冒泡排序

function bubbleSort(array){
    var i = 0,
        j = 0,
        len = array.length
    for(i=0; i<len-1;i++){
        for(j=i+1; j<len; j++){
            if(array[j]<array[i]){
                array[j] = array[j] + array[i]
                array[i] = array[j] - array[i]
                array[j] = array[j] - array[i]
            }
        }
    }
}
  • 插入排序是稳定的排序方法

  • 时间复杂度 O(n²)

3.快速排序

function partition(array, low, high){
    var flag = array[low]
    while(low<high){
        while(array[high]>=flag && low<high) high--
        array[low] = array[high]
        while(array[low]<=flag && low<high) low++
        array[high] = array[low] 
    }
    array[low] = flag
    return low
}
function qSort(array, low, high){
    if(low < high){
        var prvoloc = partition(array, low, high)
        qSort(array, low, prvoloc-1)
        qSort(array, prvoloc+1, high)
    }
}
function quickSort(array){
    qSort(array, 0, array.length-1)
}
  • 插入排序是不稳定的排序方法

  • 时间复杂度 O(nlogn)

4.选择排序

简单选择排序

function selectSort(array){
    var i = 0,
        j = 0,
        temp = 0,
        len = array.length
    for(i=0; i< len; i++){
        j = array.indexOf(Math.min.apply(null, array.slice(i)))
        if(i!==j){
            temp = array[i]
            array[i] = array[j]
            array[j] = temp
        }
    }
}
  • 这种简单选择排序是稳定的排序方法

  • 时间复杂度 O(n²)

堆排序

function HeapAdjust(array, s, m){
    var key = array[s]
    for(var j = 2*s; j<=m; j*=2){
        if(j<m && array[j]<array[j+1]){
            j = j+1
        }
        if(key >= array[j]){
            break
        }
        array[s] = array[j]
        s = j
    }
    array[s] = key
}
function heapSort(array){
    array.unshift(0)
    var i = 0,
        length = array.length - 1
    for(i=Math.floor(length/2); i>0; i--){
        HeapAdjust(array, i, length)
    }
    for(i=length; i>0; --i){
        var temp = array[i]
        array[i] = array[1]
        array[1] = temp
        HeapAdjust(array, 1, i-1)
    }
    
}
  • 堆排序是不稳定的排序方法

  • 时间复杂度 O(nlogn)

5.归并排序

function merge(left, right){
    let result = []
    
    while(left.length && right.length){
        if(left[0] <= right[0]){
            result.push(left.shift())
        }else {
            result.push(right.shift())
        }
    }
    if(left.length){
        return result.concat(left)
    }
    if(right.length){
        return result.concat(right)
    }
}
function mergeSort(array){
    if(array.length <= 1){
        return array
    }
    let mid = Math.ceil(array.length / 2),
        left = array.slice(0, mid),
        right = array.slice(mid)
    
    return merge(mergeSort(left), mergeSort(right))
}
  • 归并排序是不稳定的排序方法

  • 时间复杂度 O(nlogn)


jhhfft
590 声望40 粉丝

Write the Code. Change the World.