ship
SalesForce Simplified

Your Go-To Resource for Streamlined Solutions and Expert Guidance

mountains
Empower Your Business
Dive deep into the world of CRM excellence, where innovation meets practicality, and transform your Salesforce experience with Forceshark's comprehensive resources

Sorting Arrays in Apex Using Bubble Sort

Let’s be honest - bubble sort in Apex is like bringing a spoon to a sword fight . It might not be the most efficient or necessary tool in your Apex toolkit, but hey, sometimes we just want to understand how things work under the hood, right? So let’s take a good ol’ walk through the classic Bubble Sort algorithm, and why - even if it's rarely used in real-world Apex scenarios, it’s still a great way to understand sorting logic and control flow.

What is Bubble Sort?

Bubble Sort is one of the simplest sorting algorithms out there. It works by repeatedly comparing each pair of adjacent elements in an array and swapping them if they’re in the wrong order. Think of it like bubbles rising to the surface - the largest elements "bubble up" to the top (or end) of the array with each pass.

Simple? Yes. Fast? Not really. But it’s perfect for learning how loops, conditionals, and array manipulation work - especially in Apex.

Here's the Code We're Using

Integer[] nums = new Integer[]{5, 3, 8, 4, 2};
Integer[] sorted = bubbleSort(nums.clone());

System.debug('Original: ' + nums);
System.debug('Sorted: ' + sorted);

public static Integer[] bubbleSort(Integer[] arr) {
    Integer n = arr.size();
    Boolean swapped;

    for (Integer i = 0; i < n; i++) {
        swapped = false;

        for (Integer j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                Integer temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                swapped = true;
            }
        }

        if (!swapped) {
            break;
        }
    }

    return arr;
}

What’s Happening Here?

  • We clone the original array so we don’t mess with the original data.
  • The outer loop makes multiple passes over the array.
  • The inner loop compares and swaps adjacent values if the left is bigger than the right.
  • The swapped flag is key: it checks if any swaps were made in the current pass. If none? The array is sorted, and we stop early.

Quick Example: [5, 3, 8, 4, 2]

  • Pass 1: → [3, 5, 4, 2, 8]
  • Pass 2: → [3, 4, 2, 5, 8]
  • Pass 3: → [3, 2, 4, 5, 8]
  • Pass 4: → [2, 3, 4, 5, 8]

No swaps? We’re done.

Why Use Bubble Sort in Apex?

Okay, real talk - Apex is not the place where you’ll usually be hand-coding sorting algorithms. You’ve got more efficient built-in methods like List.sort(). But learning how bubble sort works gives you:

Fundamentals a solid understanding of loops and array operations
Optimization insight into algorithm optimization (like the swapped flag)
Pragmatism a better grasp of when not to reinvent the wheel

Plus, if you're ever in a place where you can't use built-in sorting or need a custom sort order, you’ll be glad you know the basics.

Final Thoughts

Bubble sort may be the wooden spoon of sorting algorithms - simple, old-school, and not very sharp - but it’s still worth having in your kitchen drawer. In Apex, it might not win any performance awards, but it gives you a clear look at the logic behind sorting.

And sometimes, that’s all you need to level up.