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]))
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter