Posted in

Sorting Algorithms Explained: Bubble, Selection & Insertion Sort (with Swift Examples)

sorting algorithm
Spread the love
Reading Time: 3 minutes

Sorting is one of the most common programming problems, often asked in interviews. In this post, we’ll cover three beginner-friendly algorithms:

  • Bubble Sort – largest element “bubbles” to the end each pass
  • Selection Sort – repeatedly selects the smallest element
  • Insertion Sort – inserts each element into the correct place

We’ll explain each with Swift code examples and step-by-step illustrations.

🔹 Bubble Sort

Bubble Sort repeatedly compares adjacent elements and swaps them if they are in the wrong order. After each pass, the largest element moves to the end, just like bubbles rising to the top.

Example (unsorted array: [5, 3, 8, 4, 2])

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

Pass 2:
[3, 5, 4] → swap → [3, 4, 5, 2, 8]
[5, 2] → swap → [3, 4, 2, 5, 8]

…and so on until sorted.

func bubbleSort(_ array: inout [Int]) {
    for i in 0..<array.count {
        for j in 0..<array.count - i - 1 {
            if array[j] > array[j+1] {
                array.swapAt(j, j+1)
            }
        }
    }
}

var numbers = [5, 3, 8, 4, 2]
bubbleSort(&numbers)
print(numbers) // [2, 3, 4, 5, 8]

🔹 Selection Sort

Selection Sort finds the smallest element and moves it to the front. It keeps repeating this process for each position in the array.

Example (unsorted array: [5, 3, 8, 4, 2])

Pass 1: Smallest is 2 → swap with first → [2, 3, 8, 4, 5]
Pass 2: Smallest from index 1 is 3 → already in place
Pass 3: Smallest from index 2 is 4 → swap → [2, 3, 4, 8, 5]
Pass 4: Smallest from index 3 is 5 → swap → [2, 3, 4, 5, 8]

func selectionSort(_ array: inout [Int]) {
    for i in 0..<array.count {
        var minIndex = i
        for j in i+1..<array.count {
            if array[j] < array[minIndex] {
                minIndex = j
            }
        }
        array.swapAt(i, minIndex)
    }
}

var numbers = [5, 3, 8, 4, 2]
selectionSort(&numbers)
print(numbers) // [2, 3, 4, 5, 8]

🔹 Insertion Sort

Insertion Sort works like sorting playing cards in your hand. Each new card (element) is compared and inserted in its proper place.

Example (unsorted array: [5, 3, 8, 4, 2])

Start with [5]
Insert 3 → [3, 5]
Insert 8 → [3, 5, 8]
Insert 4 → [3, 4, 5, 8]
Insert 2 → [2, 3, 4, 5, 8]

func insertionSort(_ array: inout [Int]) {
    for i in 1..<array.count {
        var j = i
        let key = array[i]
        while j > 0 && array[j-1] > key {
            array[j] = array[j-1]
            j -= 1
        }
        array[j] = key
    }
}

var numbers = [5, 3, 8, 4, 2]
insertionSort(&numbers)
print(numbers) // [2, 3, 4, 5, 8]

Final Thoughts

  • Bubble Sort is best for learning, but rarely used in production.
  • Selection Sort is simple but inefficient.
  • Insertion Sort is practical for small datasets.
  • For larger datasets, you’ll want to learn Merge Sort and Quick Sort (coming soon).

I'm a passionate iOS Developer with over 8 years of experience building high-quality iOS apps using Objective-C, Swift, and SwiftUI. I created iostutor.com to share practical tips, tutorials, and insights for developers of all levels.

When I’m not coding, I enjoy exploring new technologies and writing content — from technical guides to stories and poems — with the hope that it might help or inspire someone, somewhere.

Leave a Reply

Your email address will not be published. Required fields are marked *