Front-end interview questions: js implements array bubble sorting and selection sorting

When sorting the elements in the array from small to large or from large to small, the array method sort can be used very easily. But in the interview, the interviewer likes to ask questions about bubble sorting and selection sorting. Let’s Learn about bubble sorting and selection sorting:

Bubble Sort

First prepare an out-of-order array:

var arr = [3, 1, 5, 6, 4, 9, 7, 2, 8]

Ideas

1. First traverse the array and compare the two adjacent ones. If the previous one is larger than the next one, then change the two positions

for (var i = 0; i < arr.length; i++) {
    
    
  // 判断,如果数组中的当前一个比后一个大,那么两个交换一下位置
  if (arr[i] > arr[i + 1]) {
    
    
    var tmp = arr[i]
    arr[i] = arr[i + 1]
    arr[i + 1] = tmp
  }
}

2. After the array is traversed, the last number is the largest one. After the traversal, the array will become [3, 1, 5, 6, 4, 7, 2, 8, 9]

3. Then we execute the above code several times. How many times to traverse according to the length of the array

for (var j = 0; j < arr.length; j++) {
    
    
  for (var i = 0; i < arr.length; i++) {
    
    
    // 判断,如果数组中的当前一个比后一个大,那么两个交换一下位置
    if (arr[i] > arr[i + 1]) {
    
    
      var tmp = arr[i]
      arr[i] = arr[i + 1]
      arr[i + 1] = tmp
    }
  }
}

4. By analogy, finally the array will be arranged in order [1, 2, 3, 4, 5, 6, 7, 8, 9]

Code optimization

1. Imagine a problem. Suppose the length of the array is 9. After the eighth row is finished, the next eight numbers have been arranged in order, and the smallest remaining one must be at the top, so the ninth time has no meaning , Because the smallest one is already at the forefront, and there won’t be any change of position, then we don’t need the ninth traversal, so we can reduce it once

2. The second question, the first time, the largest number has been placed at the end, then in the second time, in fact, the penultimate and the last need not be compared, because we just want to count down The second largest is placed in the penultimate position. Even if it is compared, it will not change positions. The third-to-last digit does not need to be compared with the last two, and so on. In fact, every time When traversing, traverse the current number of times-1 time

Final code

 var arr = [3, 1, 5, 6, 4, 9, 7, 2, 8]//先准备一个乱序的数组
        for (var j = 0; j < arr.length - 1; j++) {
    
    
        //按照数组长度来进行多次遍历
            for (var i = 0; i < arr.length - 1 - j; i++) {
    
    
                // 判断,如果数组中的当前一个比后一个大,那么两个交换一下位置
                if (arr[i] > arr[i + 1]) {
    
    
                var tmp = arr[i]
                arr[i] = arr[i + 1]
                arr[i + 1] = tmp
                //借助第三个变量tmp,对满足条件的两项调换彼此位置
                }
            }
        }
        document.write(arr)//对排序完之后的数组在页面上输出

Select sort

Ideas

1. First assume that the 0th in the array is the index of the smallest number

2. Then traverse the array, as long as there is a number smaller than me, then replace the index recorded before

3. After knowing the end of the array traversal, you can find the smallest index, and then change the smallest index to the 0th position

4. Come to the second traversal, assuming that the first is the index of the smallest number

5. After traversing the array once, find the index of the number smaller than mine

6. Change position after traversal

7. By analogy, you can also sort the array

Final code

var arr = [3, 1, 5, 6, 4, 9, 7, 2, 8]
        for (var j = 0; j < arr.length - 1; j++) {
    
    
            var minIndex = j
    
            for (var i = j + 1; i < arr.length; i++) {
    
    
                if (arr[i] < arr[minIndex]) {
    
    
                minIndex = i
                }
            }
            if (minIndex !== j) {
    
    
                var tmp = arr[minIndex]
                arr[minIndex] = arr[j]
                arr[j] = tmp   
            }
        }
        document.write(arr)

Guess you like

Origin blog.csdn.net/horizon12/article/details/108325074