Skip to main content

Command Palette

Search for a command to run...

Write a bubble sort program in javascript.?

Published
1 min readView as Markdown

let temp;
function bubbleSort(arr) {
    for (let i = 0; i < arr.length; i++) {
        for (let j = 0; j < (arr.length - i) - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                temp = arr[j]
                arr[j] = arr[j + 1]
                arr[j + 1] = temp
            }
        }
    }
    return arr
}


let arr = [15, 14, 13, 12, 800, 099, 10975, -8, -4556, 11]
let res = bubbleSort(arr)
console.log(res)

More from this blog

Chowhan's blog

29 posts

A Passionate software developer