algorithm

Find the minimum value from the data to be sorted and swap it with the leftmost value of the array

Time complexity


( n 1 ) + ( n 1 ) + . . . + 1 = O ( n 2 ) (n – 1) + (n – 1) +… +1=O(n2)

js

const input = [4.2.15.2.5.6.21.67.2.3]
const sort = (arr) = > {
    for (let i = 0; i < arr.length; i++) {
        let min = arr[i]
        let 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(sort(input))

Copy the code