Skip to content

Latest commit

 

History

History

bubble-sort

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Bubble Sort

Source: https://www.algoexpert.io/questions/bubble-sort
Difficulty: easy
Category: Sorting


Write a function that takes in an array of integers and returns a sorted version of that array. Use the Bubble Sort algorithm to sort the array.

#Sample Input

array = [8, 5, 2, 9, 5, 6, 3]

#Sample Output

[2, 3, 5, 5, 6, 8, 9]

Hints

Hint 1 Traverse the input array, swapping any two numbers that are out of order and keeping track of any swaps that you make. Once you arrive at the end of the array, check if you have made any swaps; if not, the array is sorted and you are done; otherwise, repeat the steps laid out in this hint until the array is sorted.
Optimal Space & Time Complexity Best: O(n) time | O(1) space - where n is the length of the input array
Average: O(n^2) time | O(1) space - where n is the length of the input array
Worst: O(n^2) time | O(1) space - where n is the length of the input array