1. 选择排序,时间复杂度:O(四种排序算法 - 图1)
  2. 快速排序,时间复杂度:O(四种排序算法 - 图2)
  3. 归并排序,时间复杂度:O(四种排序算法 - 图3)
  4. 计数排序,时间复杂度:O(四种排序算法 - 图4)

    选择排序

    ```javascript / 选择排序的递归写法 / let selectionSort = (arr) => { if (arr.length > 2) {
    1. //let min = Math.min.apply(null,arr)
    2. //let minIndex = arr.indexOf(min)
    3. let min = arr[0]
    4. let minIndex = 0
    5. for (let i = 0; i < arr.length; i++) {
    6. if (min > arr[i]) {
    7. min = arr[i]
    8. minIndex = i
    9. }
    10. }
    11. return arr.splice(minIdex,1).concat(selectionSort(arr))
    } else {
    1. return arr[0] < arr[1] ? arr : arr.reverse()
    } }
  1. <a name="ZqguH"></a>
  2. ## 快速排序
  3. ```javascript
  4. /*
  5. 快速排序
  6. */
  7. let quickSort = (arr) => {
  8. if (arr.length <= 1) { return arr }
  9. let pivotIndex = Math.floor(arr.length / 2)
  10. let pivot = arr.splice(pivotIndex, 1)[0]
  11. let left = []
  12. let right = []
  13. for (let i = 0; i < arr.length; i++) {
  14. if (arr[i] >= pivot) {
  15. right.push(arr[i])
  16. } else {
  17. left.push(arr[i])
  18. }
  19. }
  20. return quickSort(left).concat([pivot], quickSort(right))
  21. }

归并排序

  1. /*
  2. 归并排序
  3. */
  4. let mergeSort = (arr) => {
  5. if (arr.length === 1) {
  6. return arr
  7. }
  8. let pivotIndex = Math.floor(arr.length / 2)
  9. let left = arr.slice(0, pivotIndex)
  10. let right = arr.slice(pivotIndex)
  11. return merge(mergeSort(left), mergeSort(right))
  12. }
  13. let merge = (a, b) => {
  14. if (a.length === 0) { return b }
  15. if (b.length === 0) { return a }
  16. return a[0] > b[0]
  17. ? [b[0]].concat(merge(b.slice(1), a))
  18. : [a[0]].concat(merge(a.slice(1), b))
  19. }

计数排序

  1. /*
  2. 计数排序
  3. */
  4. let countSort =(arr)=>{
  5. let hashTable = {}
  6. let max = arr[0]
  7. let min = arr[0]
  8. let result = []
  9. for(let i=0; i<arr.length;i++){
  10. if(!(arr[i] in hashTable)){
  11. hashTable[arr[i]] = 1
  12. }else{
  13. hashTable[arr[i]] += 1
  14. }
  15. if(arr[i] > max){max = arr[i]}
  16. if(arr[i] < min){min = arr[i]}
  17. }
  18. for(let j=0;j<=max;j++){
  19. if( j in hashTable){
  20. for( let i =0; i<hashTable[j]; i++){
  21. result.push(j)
  22. }
  23. }
  24. }
  25. return result
  26. }

选择排序(循环写法)

  1. /*
  2. 选择排序的循环写法
  3. */
  4. let min = (numbers)=>{
  5. if(numbers.length > 2)
  6. {return min([numbers[0],min(numbers.slice(1))])
  7. }else{
  8. return Math.min.apply(null,numbers)
  9. }
  10. }
  11. let minIndex = (numbers)=> numbers.indexOf(min(numbers))
  12. let swap =(array,x,y) =>{
  13. let temp = numbers[y]
  14. numbers[y] = numbers[x]
  15. numbers[x] = temp
  16. }
  17. let sort =(numbers)=>{
  18. for ( let i = 0 ; i < numbers.length-1; i++){
  19. console.log(`----`)
  20. console.log(`i: ${i}`)
  21. let index = minIndex(numbers.slice(i))+i
  22. console.log(`index: ${index}`)
  23. console.log(`min: ${numbers[index]}`)
  24. if (numbers[i] > numbers[index]){
  25. swap(numbers,index,i)
  26. }
  27. }
  28. return numbers
  29. }