Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-004

Bubble Sort

Problem

Sort a list of integers in ascending order and return the sorted list.

Implement bubble sort: repeatedly walk the list and swap adjacent out-of-order pairs until a full pass makes no swaps. Return a new list; the input may be empty or contain duplicates.

Hint

After each pass the largest remaining element bubbles to the end; stop early if a pass swaps nothing.

Test cases

case 1
in: [[5,2,9,1]]
expected: [1,2,5,9]
case 2
in: [[]]
expected: []
case 3
in: [[3]]
expected: [3]
case 4
in: [[4,4,2,2]]
expected: [2,2,4,4]
case 5
in: [[-1,-5,0]]
expected: [-5,-1,0]
Starter codePython
def bubble_sort(arr):
    # Your code here
    pass

5 test cases · run your code against them in the editor