1. Bubble sort

Bubble sort starts with the first item in the sort sequence, comparing adjacent elements, and if the first element is larger than the second, switching their positions until the end, where the maximum value is in the last digit. Repeat the above for all elements except the trailing element.

1.1 Personal understanding of bubbling sort

Starting with the first element of the array, compare the adjacent values of the array. If the first element is larger than the second element, swap the two values until the last element. Repeat the same process, starting with the first.

1.2 Implementation Code

/** * bubble sort *@param Arr The array to be sorted */
function bubbleSort(arr) {
    let max;
    for (let i = 0; i < arr.length - 1; i++) {
        for (let j = 0; j < arr.length - 1 - i; j++) {
            max = arr[j];
            if (max > arr[j + 1]) {
                arr[j] = arr[j + 1];
                arr[j + 1] = max; }}}return arr;
}

console.log(bubbleSort([1.3.4.2.5.8.5.6.4.7.9.14.10]))
Copy the code

2. Insert sort

Insertion sort treats the first element of the sequence to be sorted as an ordered sequence and the second element to the last element as an unsorted sequence. Scan the unordered sequence from beginning to end, inserting each scanned element into the appropriate place in the ordered sequence.

2.1 What do I understand about insertion sort

Consider the first item of the array to be a sorted array, start with the second item, and move forward. If the previous item is larger than the current one, switch places, otherwise stop.

2.2 Implementation Code

/** * insert sort *@param Arr Sorted array *@return {array}* /
function insertSort(arr) {
    let currentValue;
    let preIndex;
    for (let i = 1; i < arr.length; i++) {
        currentValue = arr[i];
        preIndex = i - 1;
        while (preIndex >= 0 && currentValue < arr[preIndex]) {
            arr[preIndex + 1] = arr[preIndex];
            preIndex--;
        }
        arr[preIndex + 1] = currentValue;
    }
    return arr;
}

console.log(insertSort([1.3.4.2.5.8.5.6.4.7.9.14.10]))
Copy the code

3. What is selection sort

Select sort is to store the maximum value (minimum value) in the unsorted sequence to the beginning of the sorted sequence, and then find the minimum value from the remaining sorted sequence into the end of the sorted sequence. And so on until all the elements are sorted.

3.1 What do you think is selection sorting

Find the maximum (minimum) value in the first array, and then start from the second maximum (minimum) value in the second array, and repeat until the sorting is complete.

3.2 Implementation Code

/** * select sort *@param Arr sorted array */
function selectSort(arr) {
    let min;
    let minIndex;
    for (let i = 0; i < arr.length; i++) {
        min = arr[i];
        minIndex = i;
        for (let j = i + 1; j < arr.length; j++) {
            if (min > arr[j]) {
                min = arr[j];
                minIndex = j;
            }
        }
        arr[minIndex] = arr[i];
        arr[i] = min;
    }
    return arr;
}

console.log(selectSort([1.3.4.2.5.8.5.6.4.7.9.14.10]))
Copy the code