Selection, bubbling, and insertion 3 simple sorting summary

Selection, bubbling, and insertion 3 simple sorting summary

Select sort

Operation: Starting from the 0th position, gradually compare with 1, 2, 3...n-1, and replace the smallest number to the 0th position; starting from the first position, gradually compare with 2, 3, 4...n- 1 For comparison, replace the second smallest number to the first place; and so on.

Features: By traversing the unfixed data, find the smallest value, and fix it in the corresponding position in ascending order. Repeat this operation until the unfixed data length is 0.

Time complexity: O(n²)

Code:

let length = 5;
let arr = new Array(length);
for(let i = 0;i<length;i++){
    
    
    arr[i] = length - i;
}
let num;
for(let i = 0;i<length-1;i++){
    
    
    for(let j = i+1;j<length;j++){
    
    
        if(arr[i] > arr[j]){
    
    
            num = arr[i];
            arr[i] = arr[j];
            arr[j] = num;
        }
    }
}

Bubble Sort

Operation: Compare the 0th place with the 1st place, arrange the numerical values ​​from small to large, then compare the 1st place with the 2nd place, and also arrange the numerical values ​​from small to large... until the n-2th place and the first place Comparing n-1 digits, the largest number is replaced with n-1 digits; comparing the 0th digit with the first digit, sorting the values ​​from small to large, and then comparing the first digit with the second digit, and the same value Arrange from smallest to largest... until the n-3th place is compared with the n-2th place, the second largest number is replaced to the n-2th place; and so on.

Feature: By comparing the unfixed data in pairs, fix the maximum value of the unfixed data to the end of the unfixed data, and repeat this operation until the length of the unfixed data is 0.

Time complexity: O(n²)

Code:

let length = 5;
let arr = new Array(length);
for(let i = 0;i<length;i++){
    
    
    arr[i] = length - i;
}
let num;
for(let i = 0;i<length-1;i++){
    
    
    for(let j = 0;j<length-i;j++){
    
    
        if(arr[j] > arr[j + 1]){
    
    
            num = arr[j];
            arr[j] = arr[j + 1];
            arr[j + 1] = num;
        }
    }
}

Optimize the code:

let length = 5;
let arr = new Array(length);
for(let i = 0;i<length;i++){
    
    
    arr[i] = length - i;
}
let num,flag;
for(let i = 0;i<length-1;i++){
    
    
    flag = false;
    for(let j = 0;j<length-i;j++){
    
    
        if(arr[j] > arr[j + 1]){
    
    
            num = arr[j];
            arr[j] = arr[j + 1];
            arr[j + 1] = num;
            flag = true;
        }
    }
    if(!flag){
    
    
        break;
    }
}

Insertion sort

Operation: Starting from the first digit, compare the first digit with the 0th digit, and the values ​​are arranged in ascending order; then starting from the second digit, compare the second digit with the first digit, and compare the first digit with the 0th digit , The numerical value is arranged in ascending order; and so on, starting from the nth position, the nth position is compared with the n-1th position... until the first and the 0th position, the numerical values ​​are arranged in ascending order.

Features: Start traversal from the 1st position to the n-1th position, ensuring that after each traversal, all data from the 0th position to the current traverse position are arranged in ascending order.

Time complexity: O(n²)

Code:

let length = 5;
let arr = new Array(length);
for(let i = 0;i<length;i++){
    
    
    arr[i] = length - i;
}
let num;
for(let i = 0;i<length-1;i++){
    
    
    for(let j = i+1;j>0;j--){
    
    
        if(arr[j - 1] > arr[j]){
    
    
            num = arr[j];
            arr[j] = arr[j - 1];
            arr[j - 1] = num;
        }else{
    
    
            break;
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_35508835/article/details/108696569