Bubble sort compares adjacent elements and swaps them if they are in the wrong order. The implementation uses two nested for loop to check two numbers in an array. The optimized solution uses a flag to skip the remaining passes. The bubble sort gif uses animations to explains the process.

Bubble sort gif


How bubble sort works?

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.


Bubble sort gif

bubble sort doodle


Implement bubble sort

Java

Javascript

Python

Doodle

bubble sort doodle


Free download

Download Java, JavaScript and Python code
Sorting doodles compilation (YouTube)