0
0
DSA Goprogramming

Bubble Sort Algorithm in DSA Go

Choose your learning style9 modes available
Mental Model
Bubble sort repeatedly swaps adjacent items if they are in the wrong order, pushing the largest unsorted item to the end each pass.
Analogy: Imagine bubbles rising in water: the biggest bubble slowly moves up to the surface by swapping places with smaller bubbles below it.
Index: 0   1   2   3   4
Array: [5]->[3]->[8]->[4]->[2]
Dry Run Walkthrough
Input: array: [5, 3, 8, 4, 2]
Goal: Sort the array in ascending order using bubble sort
Step 1: Compare 5 and 3, swap because 5 > 3
[3] -> [5] -> [8] -> [4] -> [2]
Why: Smaller number should come before larger, so swap to start sorting
Step 2: Compare 5 and 8, no swap because 5 < 8
[3] -> [5] -> [8] -> [4] -> [2]
Why: Order is correct, no need to swap
Step 3: Compare 8 and 4, swap because 8 > 4
[3] -> [5] -> [4] -> [8] -> [2]
Why: 8 is bigger, so it moves towards the end
Step 4: Compare 8 and 2, swap because 8 > 2
[3] -> [5] -> [4] -> [2] -> [8]
Why: 8 bubbles up to the last position
Step 5: Start second pass: Compare 3 and 5, no swap
[3] -> [5] -> [4] -> [2] -> [8]
Why: 3 < 5, order is correct
Step 6: Compare 5 and 4, swap because 5 > 4
[3] -> [4] -> [5] -> [2] -> [8]
Why: 5 moves towards the end
Step 7: Compare 5 and 2, swap because 5 > 2
[3] -> [4] -> [2] -> [5] -> [8]
Why: 5 moves closer to its correct position
Step 8: Start third pass: Compare 3 and 4, no swap
[3] -> [4] -> [2] -> [5] -> [8]
Why: 3 < 4, order is correct
Step 9: Compare 4 and 2, swap because 4 > 2
[3] -> [2] -> [4] -> [5] -> [8]
Why: 4 moves towards the end
Step 10: Start fourth pass: Compare 3 and 2, swap because 3 > 2
[2] -> [3] -> [4] -> [5] -> [8]
Why: 2 moves to the front, array is sorted now
Result:
[2] -> [3] -> [4] -> [5] -> [8]
Annotated Code
DSA Go
package main

import "fmt"

func bubbleSort(arr []int) {
    n := len(arr)
    for i := 0; i < n-1; i++ {
        swapped := false
        for j := 0; j < n-1-i; j++ {
            if arr[j] > arr[j+1] {
                arr[j], arr[j+1] = arr[j+1], arr[j] // swap adjacent elements
                swapped = true
            }
        }
        if !swapped {
            break // stop if no swaps in this pass
        }
    }
}

func main() {
    arr := []int{5, 3, 8, 4, 2}
    bubbleSort(arr)
    for i, v := range arr {
        if i > 0 {
            fmt.Print(" -> ")
        }
        fmt.Print(v)
    }
    fmt.Println()
}
for i := 0; i < n-1; i++ {
outer loop controls passes over the array
swapped := false
track if any swaps happen this pass
for j := 0; j < n-1-i; j++ {
inner loop compares adjacent pairs up to last sorted element
if arr[j] > arr[j+1] {
compare adjacent elements to decide if swap needed
arr[j], arr[j+1] = arr[j+1], arr[j]
swap elements to bubble larger one up
swapped = true
mark that a swap occurred this pass
if !swapped { break }
stop early if no swaps, array is sorted
OutputSuccess
2 -> 3 -> 4 -> 5 -> 8
Complexity Analysis
Time: O(n^2) because in worst case we compare and swap pairs for each element multiple times
Space: O(1) because sorting is done in place without extra storage
vs Alternative: Compared to more advanced sorts like quicksort (O(n log n)), bubble sort is slower but simpler to understand and implement
Edge Cases
empty array
function returns immediately without error
DSA Go
for i := 0; i < n-1; i++ {
array with one element
no swaps needed, array remains same
DSA Go
for i := 0; i < n-1; i++ {
already sorted array
early break after first pass because no swaps occur
DSA Go
if !swapped { break }
When to Use This Pattern
When you see a problem asking to sort a small list with simple swaps and no extra space, bubble sort is a good first approach because it repeatedly swaps adjacent out-of-order items.
Common Mistakes
Mistake: Not reducing inner loop range each pass, causing unnecessary comparisons
Fix: Use 'n-1-i' as inner loop limit to skip already sorted elements
Mistake: Not breaking early when no swaps happen, leading to extra passes
Fix: Add a swapped flag and break if no swaps in a pass
Summary
Bubble sort repeatedly swaps adjacent elements to sort the array.
Use it for small or nearly sorted lists where simplicity matters more than speed.
The key insight is that each pass pushes the largest unsorted element to its correct place at the end.