Home  >  Article  >  Web Front-end  >  Completely understand JavaScript bubble sort and selection sort in one minute

Completely understand JavaScript bubble sort and selection sort in one minute

WBOY
WBOYforward
2021-12-16 18:49:332386browse

In this article, we will learn about the relevant knowledge of bubble sorting and selection sorting in JavaScript. Bubble sorting requires immediate exchange every time a comparison is made, while selection sorting is to find the smallest unsorted number and what it should be. Exchange elements at their positions. Selection sorting has fewer exchanges, which improves computational efficiency to a certain extent. I hope everyone has to help.

Completely understand JavaScript bubble sort and selection sort in one minute

JavaScript Bubble Sort and Selection Sort

Bubble Sort

  • Principle:

Compare two adjacent elements and swap the element with the larger value to the right until the right. Note that the cores are adjacent.

  • Idea:

Compare two adjacent numbers in turn, put the smaller number in front and the larger number in front later. After the first round, the largest number in the array will be ranked last.

Second round: Then the array compares the adjacent numbers from the first number to the remaining numbers in order, and ranks the largest number at the end.

Repeat steps until sorting is complete.

Note: By the end of the penultimate round, there will be one number left in the last round, which must be the smallest, so there is no need to sort. That is, only use the length of the sorted array minus one (arr.length-1) round

Algorithm visualization:

Completely understand JavaScript bubble sort and selection sort in one minute

The code is as follows:

 <script>
        function ismaopao(arr) {
            //控制比较轮数
            for (var i = 0; i < arr.length - 1; i++) {
                //冒泡排序,两两交换,从头开始做比较(大数下沉)
                for (var j = 0; j < arr.length - 1 - i; j++) {
                    //arr.length-1-i,因为前面的判断已经找到最大的值,就不需要与找到的大数做比较了
                    if (arr[j] > arr[j + 1]) {
                        var a;
                        a = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = a;
                    }
                }
            }
            return arr;
        }
        console.log(ismaopao([6, 3, 4, 5, 2, 1]))
    </script>

The results are as follows:

Completely understand JavaScript bubble sort and selection sort in one minute

Select sort

  • Ideas:

Assume that the number in the first position of the array is the smallest, and then compare it with each subsequent number. As long as a smaller number is found, exchange the subscript corresponding to the value. Note that it is a subscript. After the first round of searching, you can lock the position of the minimum value (that is, find the subscript) and then exchange the values.

In the second round, it is assumed that the second position has the smallest number. At this time, the first value of the array is ignored (because it is already the smallest value found in the first round) and then the subscript is exchanged with the subsequent smallest value. After locking Then exchange the values.

Repeat steps until sorting is complete.

Note: By the end of the penultimate round, there will be one number left in the last round, which must be greater than the previous numbers, so there is no need to sort. That is, only use the length of the sorted array minus one (arr.length-1) round

Algorithm visualization:

Completely understand JavaScript bubble sort and selection sort in one minute

The code is as follows:

There is no encapsulation, you can encapsulate it yourself

<script>
        //选择排序,比冒泡排序次数少
        var arr = [5, 3, 4, 2, 1]
        var min = 0; //定义一个Min为数组的下标
        for (var i = 0; i < arr.length - 1; i++) {
            min = i;
            for (var j = i + 1; j < arr.length; j++) {
                if (arr[min] > arr[j]) {
                    min = j; //交换下标,就是交换位置
                }
            }
            var a = 0;
            // 现在min的值就是对应着数组最小值的下标,
            // 然后再用下标为i对应数组中的值来交换,i随着每一轮的变化而变化
            a = arr[min];
            arr[min] = arr[i];
            arr[i] = a;
        }
        console.log(arr);
    </script>

The results are as follows:

Completely understand JavaScript bubble sort and selection sort in one minute

##Related video tutorial recommendations:

jQuery video tutorial

The above is the detailed content of Completely understand JavaScript bubble sort and selection sort in one minute. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete