Bubble sort compares adjacent elements and swaps them if they are in the wrong order.


Bubble sort time and space complexity

The algorithm is named for the way smaller or larger elements “bubble” to the top of the list. Time complexity is O(n 2) average and worst. The bubble sort is slow. It is only for beginners to explore the sorting techniques. The space complexity is O(1).

Bubble sort


Implement bubble sort

Java

Javascript

Python

Doodle

bubble sort doodle


Free download

Download Java, JavaScript and Python code
View how bubble sort in action
Sorting doodles compilation (YouTube)

Can you explain Bubble sort in two sentences?

bubble sort

Bubble sort compares adjacent elements and swaps them if they are in the wrong order. The algorithm is named for the way smaller or larger elements “bubble” to the top of the list.