JavaScript implements common algorithms and data structures (update ing)

One, sort

  1. Bubble Sort
  2. Quick sort
  3. Insertion sort
  4. Select sort
1. Bubble sort

The algorithm idea of ​​bubble sort is as follows (ascending sort):

  1. Compare adjacent elements. If the first one is larger than the second, swap the two;
  2. Do the same work for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end, so that the final maximum number is swapped to the last position
  3. Repeat the above steps for all elements except the last element
  4. Repeat steps 1~3 until the sorting is completed

Insert picture description here

function bubbleSort(arr) {
    
    
    var len = arr.length;
    for (var i = 0; i < len - 1; i++) {
    
    
        for (var j = 0; j < len - 1 - i; j++) {
    
    
            if (arr[j] > arr[j+1]) {
    
            // 相邻元素两两对比
                var temp = arr[j+1];        // 元素交换
                arr[j+1] = arr[j];
                arr[j] = temp;
            }
        }
    }
    return arr;
}
2. Quick Sort

        Quick sort is one of the fastest sorting algorithms for processing big data. It is also a divide-and-conquer algorithm. The data is sequentially decomposed into different subsequences containing smaller elements and larger elements through recursive methods. This step will be repeated until All the sequences are ordered, and finally these sub-sequences are spliced ​​together once, and the sorted data can be obtained.
        The algorithm first selects an element from the sequence as the pivot. Then all the data will be carried out around this base number, put the elements less than the changed base number on the left side of it, and all the numbers greater than or equal to it on the right side, repeat the above steps for the left and right decimal columns, until each interval has only 1 number.

Insert picture description here

function quickSort( arr ){
    
    
    if ( arr.length == 0) {
    
    
        return [];
    }
    var left = [];
    var right = [];
    var pivot = arr[0];
    for (var i = 1; i < arr.length; i++) {
    
    
        if (arr[i] < pivot) {
    
    
            left.push( arr[i] );
        } else {
    
    
            right.push( arr[i] );
        }
    }
    return quickSort( left ).concat( pivot, quickSort( right ));
}
3. Insertion sort

The implementation steps are as follows:

  1. Starting from the first element, the element has been sorted by default
  2. Take out the next element and scan from back to front in the sorted sequence of elements
  3. If the element (sorted) is greater than the new element, move the element to the next position
  4. Repeat step 3 until you find the position where the sorted element is less than or equal to the new element
  5. Insert the new element at that position
  6. Repeat steps 2~5 until the sorting is completed

Insert picture description here

function insertionSort(arr) {
    
    
    var len = arr.length;
    var preIndex, current;
    for (var i = 1; i < len; i++) {
    
    
        preIndex = i - 1;
        current = arr[i];
        while(preIndex >= 0 && arr[preIndex] > current) {
    
    
            arr[preIndex+1] = arr[preIndex];
            preIndex--;
        }
        arr[preIndex+1] = current;
    }
    return arr;
}
4. Choose Sort

        Start traversing from the beginning of the array, compare the first element with other elements, record the smallest element, wait for the loop to end, put the smallest element in the first position of the array, and then start from the second position of the array Start to continue with the above steps. When it reaches the penultimate position of the array, all the data is sorted.

Insert picture description here

function selectionSort(arr) {
    
    
    var len = arr.length;
    var minIndex, temp;
    for (var i = 0; i < len - 1; i++) {
    
    
        minIndex = i;
        for (var j = i + 1; j < len; j++) {
    
    
            if (arr[j] < arr[minIndex]) {
    
         // 寻找最小的数
                minIndex = j;                 // 将最小数的索引保存
            }
        }
        temp = arr[i];
        arr[i] = arr[minIndex];
        arr[minIndex] = temp;
    }
    return arr;
}

Guess you like

Origin blog.csdn.net/weixin_43757001/article/details/114588476