Understanding Bubble Sort Slowly

Understanding Bubble Sort Slowly

Sorting algorithms, I know, I know. Everybody keeps telling you that you have to learn them, no idea why they are important, and they seem like such a hu ge hassle to you. Or maybe you just started learning data structures and algorithms, and this is usually your first stop. Usually, when you start learnin g any source of algorithm, bubble sort is the first algorithm for you to learn. Today, we will talk about bubble sort slowly. We will talk about how it w orks step-by-step, and we will implement it both in JavaScript and in Go. Let’s go.

What is Bubble Sort?

Let’s say you have an array that has eight numbers inside of it. You want to sort this array from smallest to largest, but you have no idea how to do tha t or how to even approach the problem. So what do you do? You cut down the big problem into smaller problems that you can solve. Sure, you have no idea h ow to sort an array with eight numbers into it, but you surely know how to compare two numbers. So you simply do that.

How Bubble Sort Works

Step-by-Step Explanation

  1. Compare and Swap: Start with the first element. Compare every two neighboring numbers in the array and swap them if necessary.
  2. Bubble Up: The largest numbers will “bubble up” to the end of the array after each pass.

Let’s visualize this with an example:

Basic Array

We start with the array [1, 7, 6, 4, 8, 9, 3, 2].

  • Compare 1 and 7. Since 1 is smaller, no swap is needed.
  • Compare 7 and 6. Since 7 is larger, swap them.

Comparing first two elements

  • Continue this process for the entire array. The largest number will move to the end.

Detailed Walkthrough

  • Compare 7 and 4. Swap them.
  • Compare 7 and 8. No swap needed.
  • Compare 8 and 9. No swap needed.
  • Compare 9 and 3. Swap them.
  • Compare 9 and 2. Swap them.

Switching elements

After the first pass, the array looks like this: [1, 6, 4, 7, 8, 3, 2, 9].

Repeat the process until the array is sorted.

Implementing Bubble Sort in JavaScript

Now, let’s translate this into JavaScript code.

function bubbleSort(nums) {
  let swapped;
  do {
    swapped = false;
    for (let i = 0; i < nums.length - 1; i++) {
      if (nums[i] > nums[i + 1]) {
        let temp = nums[i];
        nums[i] = nums[i + 1];
        nums[i + 1] = temp;
        swapped = true;
      }
    }
  } while (swapped);
  return nums;
}

Explanation

  1. Initialization: We start with a swapped variable to track if any swaps were made.
  2. Loop: Use a do-while loop to continue sorting until no swaps are made.
  3. Comparison and Swap: Compare each pair of elements and swap if necessary.

Implementing Bubble Sort in Go

Next, let’s implement the same algorithm in Go.

package main

func bubbleSort(nums []int) []int {
    swapped := true
    for swapped {
        swapped = false
        for i := 0; i < len(nums)-1; i++ {
            if nums[i] > nums[i+1] {
                nums[i], nums[i+1] = nums[i+1], nums[i]
                swapped = true
            }
        }
    }
    return nums
}

Explanation

  1. Initialization: Similar to JavaScript, we use a swapped variable.
  2. Loop: Use a for loop to simulate the do-while loop.
  3. Comparison and Swap: Compare and swap elements as needed.

Conclusion

Bubble sort is a simple and intuitive sorting algorithm. It is often the first algorithm taught when learning about sorting due to its simplicity. By breaking down the problem and understanding the concept of “bubbling up” the largest elements, you can easily implement bubble sort in any programming language.

I hope today that you’ve learned the ins and outs of bubble sort, conceptualized it in your mind, and enjoyed this tutorial. If you did, don’t forget to give a thumbs up and subscribe as well. I’ll see you guys in the next one. Ciao.

Pssst, you also watch this on YouTube:

Did find this post helpful?

Reading and writing files in Golang

Reading and writing files in Golang

Golang is a fantastic language. Its low-level nature and its simplicity make it an absolute powerhouse.

Read More
Getting Started with Sqlite in Go using Gorm

Getting Started with Sqlite in Go using Gorm

In this post I will show you how to communicate with a database in Go using Gorm, an ORM library that simplifies database interactions.

Read More
Using React with AdonisJS to handle Auth - Full stack tutorial

Using React with AdonisJS to handle Auth - Full stack tutorial

JavaScript is an incredibly versatile language that can be used for both front-end and back-end development.

Read More