Sorting Algorithms

Six sorting algorithms with animated demos, Python code, and kid-friendly explanations

🫧 Bubble Sort

Like bubbles rising to the surface — big numbers float to the end!

Animated Demo
Unsorted
Comparing
Swapping
Sorted
1 / 0
SlowFast

How does it work?

Imagine you have a row of numbered cards. You look at two cards next to each other. If the bigger number is on the left, you swap them. Then you move one step to the right and check again. After one full pass, the BIGGEST number has "bubbled" all the way to the end! You keep doing this over and over, and each pass, the next biggest number finds its home.

šŸ’” Bubble sort is so slow that computer scientists use it as the classic example of what NOT to do — but it's great for learning!

šŸ šŸ Python
def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(n - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
    return arr

Speed & Space

O(n²)
Best case
O(n²)
Average
O(n²)
Worst case
O(1)
Extra space
Stable sort?āœ“ Yes(equal items keep their original order)