Sorting algorithm summary (JavaScript code) - Part01

Recently, because the new coronavirus is trapped in the home, so review the next few sorting algorithms, with their own understanding, summed up in their own words.

1, Bubble sorting

Bubble sorting is to compare each number with all the numbers on the left, then exchange, and put the largest number on the right. and so on.

Bubble sorting involves repeatedly visiting the array to be sorted, comparing two elements at a time, and then swapping. In comparison, bubble sorting is the simplest sort.

Bubble sorting: time complexity O(n2); space complexity O(1); stable sorting; in situ sorting.

function BubbleSort(arr){
   for(var i=0;i<arr.length;i++){
      for(var j=0;j<arr.length-1-i;j++){  //Key point 1: J < arr.length-1-i, because the numbers on the left are all to be compared, it is arr.length-1-i, for example, when i=2, it is actually the first (10-1-2 = 7) number to be compared
            var temp=arr[j+1];
            arr[j+1] = arr[j];
            arr[j] = temp;

2, Select sort

Select sort is to exchange the smaller number with the number on the left, and so on! -----Only the smallest number is compared with the number on the left, and the smallest number is used as the benchmark to move and exchange the smallest number; when the smallest number is found, exchange.

You can think like this: find the smallest element in the array, and then exchange it with the first element of the array; then find the smallest element in the remaining elements, and exchange it with the second element of the array, and so on, until the entire array is sorted.

Selection sorting: time complexity O(n2); space complexity O(1); unstable sorting; in situ sorting.

function selectionSort(arr){
    for( var i=0;i<arr.length;i++ ){
        var minIndex = i;  //Key point 1: assume i is the lowest subscript
        for(var j=i+1;j < arr.length;j++){  //Key point 2: the subscript of j starts from i+1
            if(arr[j] < arr[minIndex]){
                minIndex = j;
        // Key point three: exchange two numbers
        var temp = arr[i];
        arr[i] = arr[minIndex];
        arr[minIndex] = temp;

3, Insert sort

Insertion sorting is that each number is sorted better than all the numbers on the left. If it is small, insert it directly (this is a direct insert, not an exchange)

Insertion sort: time complexity O(n2), space complexity O(1), stable sort, in place sort

function insertSort(arr){
    for(var i=0;i<arr.length;i++){
        var temp = arr[i];   //Key point 1: note that arr[i] needs to be saved first
        var j;  //j save where element e should be inserted
        for(j=i; j>0 && arr[j-1] > arr[i] ;j--){  //Key point 2: arr [j-1] > arr [i]; key point 3: note here j--
            arr[j] = arr[j-1]     //Key point 4: arr[j] = arr[j-1]... Pay attention to j after this step--
        arr[j] = temp;     //Key 5: arr[j] is equal to the original value of e

4, Quick sort

In comparison, quick sorting is a more troublesome sort. Quick sorting is achieved by separating a single sorting into two separate parts! You need to use recursion and call yourself.

Fast sorting: time complexity O(nlogn), time complexity O(logn), unstable sorting, in place sorting.

The most difficult one is the sorting function!

function quickSort(arr){
    var n = arr.length;
//Fast sorting of arr[l...r] parts
function __quickSort(arr,l,r){

    var p = __partition(arr,l,r);  //p is the key point of the number in the middle of the array: you need to know to find an intermediate number p, which is divided into two parts and sorted separately (the first number at the beginning is p by default)
    __quickSort(arr,l,p-1);   //Sort the parts of arr[l...p-1]
    __quickSort(arr,p+1,r);   //Sort the parts of arr[p+1...r]
//Quick sorting of arr[l...r] section
//Return p to make arr [L... P-1] < arr [P]; arr [P + 1... R] > arr [P]
function __partition(arr,k,r){
    var v = arr[k];    //Key 2: save the first number
    var j = k;          //Key point 3: set a sentinel j, which is equal to the starting number at the beginning
    for(var i=k+1;i<=r;i++){
        if(arr[i] < v){      //Key point 4: each number is compared with v
            exchange(arr,j+1,i);    //Key point 5: exchange the values of arr[j+1] and arr[i]
            j++;       //Key point 6: j + +, note that only if conditions are met can they be added;
    exchange(arr,k,j);    //Key point 7: note that there is also an exchange here, which exchanges the numbers below k and j;
    return j;       //Key 8: returns j, where the value of arr has changed!
//Exchange two numbers
function exchange(arr,p,k){
  var temp = arr[p];
  arr[p] = arr[k];
  arr[k] = temp;


37 original articles published, 71 praised, 20000 visitors+
Private letter follow

Posted by Ace_Online on Wed, 05 Feb 2020 07:17:39 -0800