Bubble Sort


Given an array of integers numbers, sort the array in ascending order using the bubble sort algorithm.


  1. Compare each pair of adjacent elements in the list from the beginning to end.
  2. If the elements are in the wrong order (i.e., the left element is greater than the right element), swap them.
  3. Repeat steps 1 and 2 until the end of the list is reached.
  4. Start again from step 1 until no more swaps are needed.


bubbleSort([9,4,7,3,5,8,1,6,2]); // Output: [1,2,3,4,5,6,7,8,9]

Time Complexity

Bubble Sort has a worst-case and average time complexity of O(n^2). However, in the best case scenario where the input array is already sorted, Bubble Sort has a time complexity of O(n).