Front-end common algorithm summary js

1. Bubble sort

Compare two adjacent elements, if the previous one is greater than the latter one, the two elements will be swapped, and after executing a complete outer for loop, a maximum element will be determined to the end of the array. If the length of the sorted array is n, then the first time to determine a maximum number needs to be compared n-1 times, and the second time n-2 times, so the time complexity is (n-1) + (n-2) + ... + 1 to derive the expression n(n - 1 )/2, so the complexity is O(n^2), but in In the best case, it only needs to be executed n-1 times

Animation display(Illustration reproduced: http://www.guoyaohua.com)
insert image description here

js implementation

const arr = [8,342,645,7645,5673,234,63,456,346,54]

/**
 * 冒泡排序
 */
const mpFn = (list) => {
    
    
  for(var i = 0; i < list.length - 1; i++) {
    
    
    for (var j = 0; j < list.length - 1 - i; j++) {
    
    
        if (list[j] > list[j + 1]) {
    
    
          var a = list[j]
          list[j] = list[j + 1]
          list[j + 1] = a
        }
    }
  }
}

mpFn(arr)
console.log(arr)

However, the above writing method has disadvantages. Maybe the messy array will be sorted a few times, and it will continue to sort. It is not necessary, it can be optimized

const arr = [8,54,63,234,342,346,456,645,7645,5673]

/**
 * 冒泡排序
 */
const mpFn = (list) => {
    
    
  var num = 0
  for(var i = 0; i < list.length - 1; i++) {
    
    
    var isOk = true // 是否已经排好了
    for (var j = 0; j < list.length - 1 - i; j++) {
    
    
      ++num
      if (list[j] > list[j + 1]) {
    
     // 进了这个循环就代表没排好
        var a = list[j]
        list[j] = list[j + 1]
        list[j + 1] = a
        isOk = false
      }
    }
    if (isOk) {
    
     // 如果排好了跳出循环
      break
    }
  }
  console.log(num, '执行次数')
}


mpFn(arr)
console.log(arr)

2. Selection sort

Loop through the array to be sorted. Each cycle finds the maximum (minimum) value of the array, cuts this value out of the array, and then inserts it at the corresponding position. Then to determine a minimum (maximum value) for the first time, it needs to be compared n-1 times, and for the second time, n-2 times, so the time complexity is (n-1) + (n-2) + ... + 1. The expression n(n - 1 )/2 is derived, so the complexity is O(n^2), and n(n - 1 )/ 2 times

Animation display(Illustration reproduced: http://www.guoyaohua.com)
selection sort
js code implementation

const arr = [63,234,7645,342,346,456,54,645,8,5673]

/**
 * 选择排序
 */
const xzFn = (list) => {
    
    
  var num = 0
  for(var i = 0; i < list.length - 1; i++) {
    
    
    let minIdx = i
    for (var j = i + 1; j <= list.length - 1; j++) {
    
    
      ++num
      if (list[minIdx] > list[j]) {
    
    
        minIdx = j
      }
    }
    if (minIdx !== i) {
    
    
      const [value]  = list.splice(minIdx, 1)
      list.splice(i, 0, value)
    }
  }
  console.log(num, '执行次数')
}


xzFn(arr)
console.log(arr)

3. Insertion sort

Loop the array to be sorted, start from the second element, and compare the current value with the previous value in turn. If it is greater than the previous one and smaller than the latter, then insert it into it, or if it is smaller than the first element, then it will be inserted at the front. Then, to complete a comparison, the first time needs 1, and the second time needs at most 2 times, and so on. 1 + 2 + ... + n - 1 derives the expression n(n - 1 )/2, so the complexity is O(n^2), but in the best case In the case of only need to execute n-1 times, this algorithm is theoretically easier than bubbling selection, select fewer times

Animation display(Illustration reproduced: http://www.guoyaohua.com)
insert image description here
js implementation

const arr = [63,234,7645,342,346,456,54,645,8,5673]
/**
 * 插入排序
 */

const crFn = (list) => {
    
    
  var num = 0
  for(var i = 1; i <= list.length - 1; i++) {
    
    
    let insertIdx = -1
    for (var j = 0; j < i; j++) {
    
    
      ++num
      if (j === 0 && list[j] >= list[i]) {
    
    
        insertIdx = j
      } else if (j > 0 && list[j-1] <= list[i] && list[i] <= list[j]) {
    
    
        insertIdx = j
      }
    }
    if (insertIdx !== -1) {
    
    
      const [value]  = list.splice(i, 1)
      list.splice(insertIdx, 0, value)
    }
  }
  console.log(num, '执行次数')
}


crFn(arr)
console.log(arr)

4. Quick row

The array to be sorted, using the first element of the array as the boundary, splits it into an array smaller than the boundary and an array greater than or equal to the boundary. The split array is split again, that is, recursive judgment, until the length of the array to be split is 1, in other words, recursion.

Animation display(Illustration reproduced: http://www.guoyaohua.com)
insert image description here
js implementation

const arr = [63,234,7645,342,346,456,54,645,8,5673]
let num = 0
/**
 * @info 快排
 */
const kpFn = (list) => {
    
    
  console.log(qfFn(list))
  console.log(num, '执行次数')
}

/**
 * @info 将数组切分为两个数组
 */
const qfFn = (list) => {
    
    
  if (list.length === 1 || list.length === 0) {
    
    
    num++
    return list
  } else {
    
    
    const minArr = []
    const maxArr = []
    for (var i = 1; i < list.length; i++) {
    
    
      num++
      if (list[0] >= list[i]) {
    
    
        minArr.push(list[i])
      } else {
    
    
        maxArr.push(list[i])
      }
    }
    return [...qfFn(minArr), list[0], ...qfFn(maxArr)]
  }
}


kpFn(arr)
// console.log(arr)

Guess you like

Origin blog.csdn.net/weixin_44441196/article/details/122347424