Visualizing Sorting Algorithms: Teaching with Interactive Videos

Visualizing Sorting Algorithms: Teaching with Interactive Videos

4 min read · June 12, 2024

One of the biggest challenges in teaching algorithms is helping students develop the right mental model. As experienced computer scientists, we don’t just see algorithms as a series of steps -— we visualize them as dynamic processes manipulating data structures.

When I teach my algorithms course, I emphasize abstraction as a key tool for structuring complex problems. This is why we use pseudo-code instead of a specific programming language: it keeps the focus on the algorithm itself, without the distractions of syntax.

But there’s a deeper challenge: how do we help students see the mental movie that plays in our minds?

The Mental Movie of an Algorithm

At some point in our learning journey, we start seeing algorithms visually. A simple line like:

node = node->next

becomes a pointer moving from one node to another. The execution of an algorithm transforms into a visual process, not just lines of code.

For many of us, this shift happens gradually, often without realizing it. But when I think back, I see how my teachers’ sketches on the board -— their arrows, gestures, and repeated explanations—were crucial. They helped me form the mental model I now use instinctively.

As a researcher in Human-Computer Interaction (HCI), I wanted to explore how we could transfer this mental model more effectively. Could interactive visualizations help students “see” sorting algorithms the way we do?

Bringing Algorithms to Life

To help my students develop this intuition, I created interactive algorithm animations. These animations let students observe the algorithm in action, step by step, reinforcing key concepts like:

In HCI, we call these mechanisms, mental models —- the way we understand how a system behaves.

Mental model diagram by Nielsen and Norman Group

Mental model diagram by Nielsen and Norman Group

How It Works

To create these materials, I used:

Each animation is based on a TypeScript implementation that logs the algorithm’s state at each step. These snapshots are then played as a video, showing how the algorithm progresses over time. I also replicated the pseudo-code styling from CLRS to make it look familiar to the students.

Interactive Sorting Algorithm Videos

Insertion Sort

The first visualization I created was for Insertion Sort -— an intuitive algorithm that helps students understand how elements shift into place. I wanted to show that the integer variables i and j should be understood as pointers to elements of the array.

Parameters

By adjusting playback speed and testing different inputs, students can identify best and worst-case scenarios and analyze the algorithm’s performance.

Selection Sort

Another visualization follows Selection Sort, reinforcing the concept of finding the smallest element and placing it in order.

Parameters

Bubblesort

Bubble Sort is a great example to discuss optimizations. The first video shows the basic algorithm:

Bubblesort(

AA, nn

)
1

for i=1i = 1 to n1n - 1

2

for j=nj = n downto i+1i + 1

3

if A[j]<A[j1]A[j] < A[j - 1]

4

swap A[j]A[j] and A[j1]A[j - 1]

1
5
2
3
6
4
i

Bubblesort Simulation

0:00 / 0:43
Parameters

After discussing potential improvements, students can compare it with an optimized version:

Parameters

Counting Sort

For more advanced students, Counting Sort introduces the idea of non-comparison-based sorting.

Counting-Sort(

AA, nn, kk

)
1

let B[1:n]B[1:n] and C[0:k]C[0:k] be new arrays

2

for i=0i=0 to kk

3

C[i]=0C[i] = 0

4

for j=1j=1 to nn

5

C[A[j]]=C[A[j]]+1C[A[j]] = C[A[j]] + 1

6

for i=1i=1 to kk

7

C[i]=C[i]+C[i1]C[i] = C[i] + C[i-1]

8

for j=nj=n downto 11

9

B[C[A[j]]]=A[j]B[C[A[j]]] = A[j]

10

C[A[j]]=C[A[j]]1C[A[j]] = C[A[j]] - 1

11

for p=1p=1 to nn

12

A[p]=B[p]A[p] = B[p]

AA

1
5
2
3
6
4

BB

CC

0
1
2
3
4
5
6

Counting Sort Simulation

0:00 / 1:16
Parameters

Understanding how the auxiliary arrays work can be tricky, so this visualization helps students track how elements are counted and placed in order. Based on my experience from this semester, I think I still need to iterate on this interactive video.

Radix Sort

Finally, students explore Radix Sort, which builds on Counting Sort and introduces new ways to approach sorting.

Radix-Sort(

AA, nn, dd

)
1

for i=1i = 1 to dd

2

use a stable sort to sort array A[1:n]A[1:n] on digit ii

329
12
657
1234
1567
839
436
720
355

Radix Sort Simulation

0:00 / 0:19
Parameters

What’s Next?

So far, the feedback has been overwhelmingly positive. Students engage with the materials, experiment with different inputs, and actively discuss algorithm behavior in class.

I still have ideas for improvements —- especially in making Counting Sort clearer —- and I plan to expand this approach to other algorithms.

If you’re interested in these visualizations, let me know! I’d love to hear your thoughts and explore ways to make algorithm learning even more intuitive.