Bubble sorting of javascript algorithm sorting


Event address: CSDN 21-day learning challenge

foreword

Many people have heard of the classic sorting algorithm, and many people may have used it, but there are also many people who have never heard of it. why? Now that we have more and more frameworks and dependent packages, we will be able to use the actual scene of sorting and encapsulate it into a function as a business. Therefore, some people only know the function but not its operating logic.

Based on the above, in order to better understand the operation logic of the function, I sorted out some operation rules of the basic sorting method, as well as some personal understanding, hoping to give you some help.

Bubble Sort

Bubble sorting is one of the basic sorting algorithms. Its core idea is to compare adjacent elements pairwise, the larger number sinks, and the smaller number rises. The big number comes after, and the process is like a bubble from small to large, so it is called bubble sort.

Principle of Bubble Sort

  • The traversal is to be sorted, and two adjacent elements are compared in turn;
  • The order is correct: the representative position is correct and no exchange is required;
  • The order is wrong: exchange two elements to make the order correct;

Each pass can only be sure to put a number in the correct sort. That is, the first pass can only determine the return of the last digit, the second pass can only return the second-to-last digit, and so on. If there are n numbers to be sorted, only n-1 numbers need to be sorted, that is, n-1 operations are required.

Let the length of the array be n, the traversal index is i and starts from 0,
because each traversal will place the current maximum value at the position of n - 1 - i, that is, [ n-1-i,n-1 after each traversal ] The elements of the interval are arranged in positive order, and [0,n-1-i] is arranged in disorder;

And "every trip" needs to compare two adjacent numbers starting from the first digit, put the larger number behind, and move back one digit after the comparison is completed to continue comparing the next two adjacent numbers Size relationship, repeat this step until the last number that has not yet returned.

insert image description here

the code

function bubbleSort(array) {
    
    
    let length = array.length;
    //遍历数组所有元素
    for (let i = 0; i < length - 1; i++) {
    
    
        //遍历未排序数组,即[0,length-1-i]的区间
        for (let j = 0; j < length - 1 - i; j++) {
    
    
            //两个挨着的元素进行比较,如果前一个比后一个大,则位置交换
            if (array[j] > array[j + 1]) {
    
    
                let item = array[j];
                array[j] = array[j + 1];
                array[j + 1] = item;
            }
        }
    }
    console.log("bubbleSort result:", array);
}


bubbleSort([1, 2, 3, 10, 9, 17, 6, 5, 7]);

insert image description here

the complexity

Time complexity: O(n^2);
Space complexity: O(n^2);

message

The above is the workflow of bubble sorting, I hope it can give you some inspiration!

Not all research is meaningful, but if you want to delve deeper, it must be meaningful

Guess you like

Origin blog.csdn.net/Long861774/article/details/126329331