您當前位置: 南順網絡>> 官方資訊>> 行業動态

冒泡排序

let arr = [2,7,4,1,3,6,5,9,0,8]
let temp = ''

for (let i = 0; i < arr.length; i++){
    for(let j = i; j < arr.length; j++){
        if ( arr[i] > arr[j] ) {

            temp = arr[i]
            arr[i] = arr[j]

            arr[j] = temp

        }
    }


console.log(arr) // [0,1,2,3,4,5,6,7,8,9]

編輯:--ns868