Bubble Sort Algorithm: Visualization and Animation

Bubble Sort is a simple sorting algorithm often taught to beginners because it’s easy to understand. In the previous articles, we’ve explained what Bubble Sort is and how it works. In this article, we’ll visualize each step in Bubble Sort algorithm to make it even clearer. By the end of this article, you’ll gain an intuitive understanding of how Bubble Sort works, making the entire concept much clearer and easier to grasp.

Why Visualization and Animation are Helpful

Sorting algorithms can sometimes seem abstract. Instead of just reading about how they work, seeing them in action can make a big difference! Visualizations help us understand:

  • How elements are compared: You can actually see which elements are being compared at each step.
  • How elements are swapped: The swapping of elements to put them in the right order becomes very clear.
  • The overall flow of the algorithm: Animation provides a step-by-step view of how the algorithm progresses from an unsorted to a sorted state.

Visualization of Various Steps in Bubble Sort Algorithm

Let’s visualize how Bubble Sort works by applying it to a small array of numbers: [5, 1, 8, 4, 2]. Bubble Sort repeatedly iterates through the array, comparing adjacent elements and swapping them if they’re in the wrong order.

Here’s how it sorts the array step by step:

Step 0: Initial State

At the beginning, the array is unsorted: [5, 1, 8, 4, 2]

Step 1: First Pass

  1. Compare 5 and 1. Since 5 > 1, swap them: [1, 5, 8, 4, 2]
  2. Compare 5 and 8. Since 5 < 8, no swap needed: [1, 5, 8, 4, 2]
  3. Compare 8 and 4. Since 8 > 4, swap them: [1, 5, 4, 8, 2]
  4. Compare 8 and 2. Since 8 > 2, swap them: [1, 5, 4, 2, 8]

After the first pass, the largest element (8) has “bubbled” to the end.

Step 2: Second Pass

  1. Compare 1 and 5. Since 1 < 5, no swap needed: [1, 5, 4, 2, 8]
  2. Compare 5 and 4. Since 5 > 4, swap them: [1, 4, 5, 2, 8]
  3. Compare 5 and 2. Since 5 > 2, swap them: [1, 4, 2, 5, 8]
  4. No need to compare 5 and 8 as the largest element 8 is already in its correct position from the previous pass

Step 3: Third Pass

  1. Compare 1 and 4. Since 1 < 4, no swap needed: [1, 4, 2, 5, 8]
  2. Compare 4 and 2. Since 4 > 2, swap them: [1, 2, 4, 5, 8]
  3. No need to continue comparisons as the largest elements (5, and 8) are already in their correct positions from previous passes

Step 4: Fourth Pass

  1. Compare 1 and 2. Since 1 < 2, no swap needed: [1, 2, 4, 5, 8]
  2. No need to continue comparisons as all elements after element 2 are already in their correct positions

Also, no swaps occurred in this pass, so the array can be said to be fully sorted.

Step 5: Sorted Array

The array is now fully sorted in ascending order: [1, 2, 4, 5, 8]

In summary: Bubble Sort works by repeatedly comparing adjacent elements and swapping them if they are in the wrong order. Elements with higher values “bubble” to the end of the array with each pass until the array is fully sorted.

Step By Step Slide Show

Below is the step by step visualization of bubble sort algorithm along with easy to follow comments:

What’s Next?

Now that you have visualized how bubble sort works, you might want to: