DEV Community

Brendon O'Neill
Brendon O'Neill

Posted on

Sink or Sort

Introduction

As we reach the doubly linked list in the DSA journey, it’s a good point to take a small detour away from data structures and start focusing on algorithms. Up until now, we’ve been learning how to store and organise data. From here on, we’ll begin looking at how we actually work with that data by following a set of defined rules.

The two main types of algorithms we’ll focus on are sorting and searching. These algorithms are designed to help us locate or organise information in a list more efficiently, rather than checking each value one by one.

Sorting plays a huge role in performance. Many searching algorithms rely on data being ordered beforehand, and without that structure, even simple searches can become unnecessarily slow. In this blog, we’ll start with one of the most basic sorting algorithms to understand the core idea before moving on to more efficient approaches later on.

Why do we need to sort

When we search through a list that isn’t sorted, finding a specific piece of information becomes much harder. In the worst case, we might have to check every single value until we find what we’re looking for.

Think of it like a phone book with no order at all. If the names weren’t sorted alphabetically, you’d have to scan through every entry from start to finish just to find one number. If there were hundreds of thousands of names, that would take a long time.

Sorting solves this problem by putting data into a predictable order. Once the data is sorted, we can make smart jumps instead of checking each item one by one. Now, if we have data sorted in our phone book, we can jump straight to the section for a specific letter, instantly narrowing down the search space.

This is why sorting is so important. It makes searching faster, more efficient, and far more scalable as your data grows.

With that foundation in place, let’s start with the most basic sorting algorithm. It’s quick and dirty, not the most efficient, but it’s a great way to understand how sorting works at a fundamental level.

Bubble sort

Bubble sort is one of the simplest sorting algorithms you’ll come across. It’s not fast, and it’s definitely not efficient for large datasets, but it’s a great place to start because the logic is easy to visualise and understand.

Imagine a row of bubbles rising through water. The larger bubbles naturally float upward faster than the smaller ones. Bubble sort works in a very similar way. As we move through the list, larger values gradually “bubble up” toward the end of the array, while smaller values sink toward the beginning.

The algorithm repeatedly steps through the list, comparing two neighbouring values at a time. If they’re in the wrong order, we swap them. After one full pass, the largest value has floated all the way to the end. With each additional pass, the next-largest value finds its correct position, and the unsorted portion of the list becomes smaller and smaller.

Let’s look at the implementation:

for (let i = 0; i < arr.length; i++) {
  for (let j = 0; j < arr.length - i - 1; j++) {
    if (arr[j] > arr[j + 1]) {
      let temp = arr[j];
      arr[j] = arr[j + 1];
      arr[j + 1] = temp;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

How It Works

We use two loops:

  • The outer loop controls how many passes we make through the array.

  • The inner loop compares each value with the one next to it.

On each pass:

  • We compare arr[j] and arr[j + 1]

  • If the left value is greater than the right, we swap them

  • The largest value moves toward the end of the array

Notice the condition arr.length - i - 1 in the inner loop. Each time we complete a full pass, the largest remaining value is guaranteed to be at the end, so we don’t need to check it again. This slightly improves performance by avoiding unnecessary comparisons.

We repeat this process until every value is in the correct position and the array is fully sorted.

Let’s say we start with this array:

[5, 3, 8, 2]
Enter fullscreen mode Exit fullscreen mode

We compare values from left to right, swapping when needed.

First pass:

[5, 3, 8, 2]   compare 5 & 3 → swap
[3, 5, 8, 2]   compare 5 & 8 → no swap
[3, 5, 8, 2]   compare 8 & 2 → swap
[3, 5, 2, 8]   ← 8 bubbles to the end
Enter fullscreen mode Exit fullscreen mode

The largest value (8) has now floated to the end of the array.

Second pass:

[3, 5, 2, 8]   compare 3 & 5 → no swap
[3, 5, 2, 8]   compare 5 & 2 → swap
[3, 2, 5, 8]   ← 5 bubbles into place
Enter fullscreen mode Exit fullscreen mode

Final pass:

[3, 2, 5, 8]   compare 3 & 2 → swap
[2, 3, 5, 8]   ← fully sorted
Enter fullscreen mode Exit fullscreen mode

Each pass pushes the next-largest value toward the end, just like bigger bubbles rising to the surface. As more values settle into place, the number of comparisons needed gets smaller.

Swapping Values

In the example above, we used a temporary variable to swap values. Another common way to do this in JavaScript is with array destructuring:

[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
Enter fullscreen mode Exit fullscreen mode

Both approaches work the same way. The key idea is that we’re simply exchanging two adjacent values when they’re in the wrong order.

Complexity

  • Time Complexity: (O(n^2)) in the worst case, since we may need to compare every pair of values.

  • Space Complexity: (O(1)), because we’re sorting the array in place without using extra memory.

Bubble sort isn’t something you’d use in production for large datasets, but it’s a great starting point for understanding how sorting algorithms work before moving on to faster and more efficient ones.

Conclusion

And that’s our introduction to Bubble Sort, a simple and intuitive sorting algorithm that helps build a strong foundation for understanding how sorting works under the hood.

While it’s not the most efficient option for large datasets, bubble sort is a great learning tool. It shows how values can be compared, swapped, and gradually moved into place by following a clear set of rules.

As you continue through the DSA journey, we’ll build on these ideas with faster and more efficient sorting algorithms, and later combine them with searching techniques to make working with data even more powerful.

If you enjoyed this blog and want to keep learning, check out these other blogs of mine:

Thanks for reading, and happy coding as you keep growing your DSA skills!

Top comments (0)