Selection sort 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.

This article will describe selection sort, and the difference between selection sort and bubble sort!
Because many people can't accurately explain the difference between selection sort and bubble sort when they think back carefully, and even think they are the same!

selection sort

Selection sort is a simple and intuitive sorting algorithm. As the name suggests, the core point of selection sort is to choose, choose the maximum or minimum value in the array! , and then logically placed in the specified location.

Selection sort implementation principle

  • First find the smallest (largest) element in the unsorted sequence and store it at the beginning of the sorted sequence;
  • Then, continue to find the smallest (largest) element from the remaining unsorted elements, and then put it at the end of the sorted sequence;
  • And so on until all elements are sorted;

insert image description here

the code


function selectionSort(array) {
    //外循环控制次数
    for (var i = 0; i < array.length; i++) {
        //假定最小值用于比较
        var min = array[i];
        //j=i+1使其从剩余元素中进行筛查
        for (var j = i + 1; j <= array.length; j++) {
            //从剩余数字中寻找最小值
            if (min > array[j]) {
                //更新最小值
                min = array[j];
                //交换a[j]和a[i]
                var item = array[j];
                array[j] = array[i];
                array[i] = item;
            }
        }

    }
    console.log("selectionSort result:", array);
}

selectionSort([4, 5, 1, 3, 2]);

output value:
insert image description here

the complexity

Time complexity of selection sorting,
time complexity: O(n^2);
space complexity: O(n);

Comparison between selection sort and bubble sort

the difference:

  • Bubble sort: pairwise comparison, instant exchange.
  • Selection method sorting: find the most value first, and exchange once.

Pros and cons:

  • Selection sort is faster than bubble sort. The number of exchange of selection sort elements is O(n), and the number of exchange of bubble sort elements is O(n^2). Since the CPU time required for exchange is more than the CPU time required for comparison, when the value of n is small, selection sort is more expensive than risk. Bubble sort is fast;
  • Bubble sort is simpler to implement than selection sort. If the amount of data to be sorted is small and the efficiency requirements are not high, bubble sorting can fully meet;

message

It seems that the logic of selection sorting and bubble sorting is the same, and both can realize the sorting function, but in fact, the calculation speed is not the same, which is the charm of the basic algorithm.

The bigger the circle, the more unpredictable possibilities!

Guess you like

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