bubble sort
Wed Oct 12 2022 13:32:20 GMT+0000 (Coordinated Universal Time)
Saved by
@RedJohn
const bubbleSort = (arr) => {
let noSwaps = false
let temp;
for(let i = arr.length; i > 0; i++) {
noSwaps = true
for(let j = 0; j < i - 1; j++) {
if(arr[j] > arr[j + 1]) {
temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
noSwaps = false
}
}
if (noSwaps) break
}
return arr
}
console.log(bubbleSort([1,8,16,11,6,3,2,10,45,5,4]))
content_copyCOPY
Comments