Module 28: Sorting Algorithms

Let's visualize how computers put things in order!

Bubble Sort Visualizer

Watch how Bubble Sort works! It repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order.

Comparisons: 0 Swaps: 0
Ready.

Selection Sort Visualizer

Now see Selection Sort! It divides the list into sorted and unsorted parts, finds the smallest element in the unsorted part, and swaps it with the first unsorted element.

Comparisons: 0 Swaps: 0
Ready.

Comparison Challenge

Think about how many comparisons each sort makes. Which of these lists do you think will require more comparisons using Bubble Sort?

List A (Nearly Sorted)

1 3 2 4 5 6

List B (Reverse Order)

6 5 4 3 2 1

Code Logic: The Comparison

Algorithms are sequences of steps. Let's focus on the core comparison in Bubble Sort's inner loop. Drag the correct code piece to complete the `if` statement that checks if a swap is needed (for ascending order).

if list[j]
drop here
list[j + 1]:
    # Swap elements...

Mini Mission: Swap 'em!

A fundamental step in many sorting algorithms is swapping two elements. In the editor below, write the Python code to swap the values of variables `a` and `b` using a temporary variable `temp`.

Output will show final a and b...

Module 28 Practice Complete!

Great job visualizing and interacting with sorting algorithms! You've explored Bubble Sort, Selection Sort, and the importance of comparisons and swaps.
Ready for more? Try the Advanced Practice for Module 28!