bubbleSort

25 阅读1分钟
let bubbleSort = function (arr) {
    let temp;
    for(let i =0; i < arr.length; i++){
        for(let j = 0; j < arr.length; j++){
            if(arr[j] > arr[j+1]){
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
    console.log(arr);
}

bubbleSort([9,8,7,6,5]

image.png

image.png

image.png

image.png

image.png