# 快速排序

function quickSort (arr, left, right) {
    if (left > right)
        return

    let i, j, t, temp
    i = left
    j = right
    t = arr[left]
    while (i !== j) {
        while (i < j && arr[j] >= t) 
            j--
        while (i < j && arr[i] <= t)
            i++
        if (i < j) {
            temp = arr[i]
            arr[i] = arr[j]
            arr[j] = temp
        }
    }

    arr[left] = arr[i]
    arr[i] = t

    quickSort(arr, left, i-1)
    quickSort(arr, i+1, right)
}

# 选择排序

# 插入排序

# 归并排序

function MergeSort(arr, low, high) {
  if (high <= low)
    return
  let mid = low + parseInt((high - low)/2)
  MergeSort(arr, low, mid)
  MergeSort(arr, mid+1, high)
  merge(arr, low, mid, high)
}

function merge(arr, low, mid, high) {
  let i = low
  let j = mid + 1
  let temp = []
  while (i<=mid && j<=high) {
    if (arr[i] < arr[j]) {
      temp.push(arr[i])
      i++
    } else {
      temp.push(arr[j])
      j++
    }
  }
  while (i<=mid) {
    temp.push(arr[i])
    i++
  }
  while (j<=high) {
    temp.push(arr[j])
    j++
  }
  arr.splice(low, high-low+1, ...temp)
}

let arr = [6, 1, 2, 5, 4, 3, 9, 7, 10, 8]
MergeSort(arr, 0, arr.length-1)
console.log(arr)

# 堆排序