Backbencher.dev

Bubble Sort Using JavaScript

Last updated on 20 Jan, 2023

Here is the bubble sort algorithm:

function bubbleSort(arr) {
  // Loop through all items
  for (let i = 0; i < arr.length; i++) {
    // Loop to move one item at time to right by swaping
    for (let j = 0; j < arr.length - 1 - i; j++) {
      // Swap positions if n is greater than n+1 value
      if (arr[j] > arr[j + 1]) {
        const temp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = temp;
      }
    }
  }
  return arr;
}

Big O of Bubble sort is O(n^2).

--- ○ ---
Joby Joseph
Web Architect