Bubble sort in javascript

Bubble Sort Algorithm in Javascript

Algorithm, Javascript, Sorting, Trending

Bubble sort is a simple algorithm to sort a unsorted array. We will swap the the elements with the adjacent element.

Please find the implementation below.

let arr = [64, 34, 25, 12, 22, 11, 90];
for (i = 0; i <= arr.length - 1; i++) {
    for (j = 0; j <= (arr.length - 1) - i - 1; j++) {
        if (arr[j] > arr[j + 1]) {
            let num = arr[j];
            arr[j] = arr[j + 1];
            arr[j + 1] = num;
        }
    }
}
console.log(arr);

References:

https://www.geeksforgeeks.org/bubble-sort/

Also Please check

One thought on “Bubble Sort Algorithm in Javascript

Leave a Reply