šŸƒ Bubble Sort Algorithm with Playing Cards

Cards being compared
Cards swapped
Compared, no swap needed
In final sorted position
Initial Array
Starting with our unsorted cards
5
2
8
3
6
Pass 1, Step 1: Compare 5 and 2
5 > 2, so we need to swap them
5
2
8
3
6
5 > 2 → SWAP
2
5
8
3
6
Pass 1, Step 2: Compare 5 and 8
5 < 8, so no swap needed
2
5
8
3
6
5 < 8 → NO SWAP
2
5
8
3
6
Pass 1, Step 3: Compare 8 and 3
8 > 3, so we need to swap them
2
5
8
3
6
8 > 3 → SWAP
2
5
3
8
6
Pass 1, Step 4: Compare 8 and 6
8 > 6, so we need to swap them
2
5
3
8
6
8 > 6 → SWAP
2
5
3
6
8
āœ… 8 is now in its final position!
Pass 2, Step 1: Compare 2 and 5
2 < 5, so no swap needed
2
5
3
6
8
2 < 5 → NO SWAP
2
5
3
6
8
Pass 2, Step 2: Compare 5 and 3
5 > 3, so we need to swap them
2
5
3
6
8
5 > 3 → SWAP
2
3
5
6
8
Pass 2, Step 3: Compare 5 and 6
5 < 6, so no swap needed
2
3
5
6
8
5 < 6 → NO SWAP
2
3
5
6
8
āœ… 6 is now in its final position!
Pass 3, Step 1: Compare 2 and 3
2 < 3, so no swap needed
2
3
5
6
8
2 < 3 → NO SWAP
2
3
5
6
8
Pass 3, Step 2: Compare 3 and 5
3 < 5, so no swap needed
2
3
5
6
8
3 < 5 → NO SWAP
2
3
5
6
8
āœ… 5 is now in its final position!
Pass 4, Step 1: Compare 2 and 3
2 < 3, so no swap needed
2
3
5
6
8
2 < 3 → NO SWAP
2
3
5
6
8
āœ… 3 is now in its final position!
šŸŽ‰ Final Sorted Array
All cards are now in ascending order!
2
3
5
6
8
āœ… Bubble sort complete! The array is fully sorted.

How Bubble Sort Works:

1. Compare adjacent elements in the array

2. If they are in the wrong order, swap them

3. Continue through the array until no more swaps are needed

4. After each pass, the largest unsorted element "bubbles up" to its correct position

Time Complexity: O(n²) in worst case, O(n) in best case (already sorted)

Space Complexity: O(1) - sorts in place