fudx / learnNotes

刷题笔记哦
0 stars 0 forks source link

8、冒泡排序 #8

Open fudx opened 1 year ago

fudx commented 1 year ago
// 冒泡排序
var arr = [2,7,4,1,23]
fudx commented 1 year ago

function bubbleSort(arr) {
     for(let i = 0; i < arr.length; i++) {
            for(let j = 0; j < arr.length - i; j++) {
                 if(arr[j] > arr[j+1]) {
                      [arr[j], arr[j+1]] = [arr[j+1], arr[j]]
                 }
            }
     }
    return arr
}
const test = [2,7,4,1,23]
console.log(bubbleSort(arr)) // 1,2,4,7,23