Selection sort is one of those classic sorting algorithms that every beginner meets early in their programming path. It is simple, clear, and easy to understand, which makes it perfect for learners. The idea behind selection sort is to move through the array step by step, pick the smallest value each time, and place it where it belongs. This makes the algorithm feel calm and steady, almost like choosing the best item from a group and arranging things one by one.
with hands-on learning.
get the skills and confidence to land your next move.
Even though selection sort is not the fastest algorithm in the world, it is still important. It is used in teaching, coding interviews, and small programs where performance is not a big concern. More importantly, it helps new programmers understand how searching, swapping, and iteration work. Once you learn selection sort well, other sorting techniques feel easier to understand because you have a strong foundation.
Program 1: Basic Selection Sort Using Loops
This first program shows the classic way to perform selection sort in Swift using simple loops. It takes a predefined array, finds the smallest value during each pass, and places it in the correct position. This approach helps beginners understand the heart of the selection sort process.
import Foundation
var numbers = [64, 25, 12, 22, 11]
for i in 0..<numbers.count {
var minIndex = i
for j in i + 1..<numbers.count {
if numbers[j] < numbers[minIndex] {
minIndex = j
}
}
if minIndex != i {
let temp = numbers[i]
numbers[i] = numbers[minIndex]
numbers[minIndex] = temp
}
}
print("Sorted Array:", numbers)This version works by scanning the unsorted portion of the array, locating the smallest number, and swapping it into place. It does this again and again until the whole array is sorted. Beginners find this method helpful because it shows clearly how selection takes place, and the structure is easy to trace on paper or in the mind.
Program 2: Selection Sort Using a Function
This program organizes the sorting logic into a Swift function so the code becomes cleaner and easier to reuse. It takes an array as input and returns a fully sorted array. This is helpful for beginners who want to understand how to separate logic into different parts of a program.
import Foundation
func selectionSort(_ arr: [Int]) -> [Int] {
var nums = arr
for i in 0..<nums.count {
var minIndex = i
for j in i + 1..<nums.count {
if nums[j] < nums[minIndex] {
minIndex = j
}
}
if minIndex != i {
let temp = nums[i]
nums[i] = nums[minIndex]
nums[minIndex] = temp
}
}
return nums
}
let data = [50, 10, 2, 88, 33]
let sorted = selectionSort(data)
print("Sorted Array:", sorted)This approach keeps the main code simple because the sorting happens inside a dedicated function. It teaches beginners how to design neat code and how to send data into functions and get processed results back. It also prepares them for writing bigger programs later on.
Program 3: Selection Sort in Descending Order
Here, the program performs selection sort but arranges the numbers from largest to smallest instead of smallest to largest. The logic is almost the same, except the comparison changes to search for the maximum value. This shows how flexible the algorithm can be with only small adjustments.
import Foundation
var items = [14, 3, 67, 25, 9]
for i in 0..<items.count {
var maxIndex = i
for j in i + 1..<items.count {
if items[j] > items[maxIndex] {
maxIndex = j
}
}
if maxIndex != i {
let temp = items[i]
items[i] = items[maxIndex]
items[maxIndex] = temp
}
}
print("Sorted Descending:", items)This version is useful when you want higher values first, such as ranking scores or organizing results. It also teaches beginners that algorithms are not fixed; they can be shaped to serve different purposes by adjusting small parts of the logic.
Program 4: Recursive Selection Sort
This program takes a different path by using recursion. Instead of loops, the function calls itself again and again until the whole array is sorted. The logic still finds the smallest value in each step, but the sorting moves forward through recursive calls.
import Foundation
func recursiveSelection(_ arr: inout [Int], _ start: Int) {
if start >= arr.count - 1 {
return
}
var minIndex = start
for i in start + 1..<arr.count {
if arr[i] < arr[minIndex] {
minIndex = i
}
}
if minIndex != start {
let temp = arr[start]
arr[start] = arr[minIndex]
arr[minIndex] = temp
}
recursiveSelection(&arr, start + 1)
}
var nums = [29, 10, 14, 37, 13]
recursiveSelection(&nums, 0)
print("Sorted Array:", nums)This recursive version helps beginners explore another style of programming. Even though recursion may look unusual at first, it teaches the idea of solving smaller and smaller parts of a problem until nothing is left. While recursion is not the fastest choice for sorting, it is an excellent learning exercise.
Program 5: Selection Sort with a Helper Function to Find the Minimum
In this version, the logic for finding the smallest number is moved into a helper function. This divides the task into smaller parts, making the code easier to read. It demonstrates how breaking down a problem into sub-tasks can lead to cleaner design.
import Foundation
func findMinIndex(_ arr: [Int], _ start: Int) -> Int {
var minIndex = start
for i in start + 1..<arr.count {
if arr[i] < arr[minIndex] {
minIndex = i
}
}
return minIndex
}
var dataSet = [40, 20, 90, 5, 16]
for i in 0..<dataSet.count {
let minIndex = findMinIndex(dataSet, i)
if minIndex != i {
let temp = dataSet[i]
dataSet[i] = dataSet[minIndex]
dataSet[minIndex] = temp
}
}
print("Sorted Array:", dataSet)This method shows another way to structure selection sort. By moving the search into a helper function, the main loop becomes easier to understand. This approach is good for beginners who want code that looks clean and teaches them how to organize programs into smaller reusable pieces.
Frequently Asked Questions (FAQ)
This section helps clear common doubts that beginners often have when learning selection sort.
Q1: Why do programmers still learn selection sort?
It is simple, predictable, and great for teaching how sorting works. Even if it is not the fastest, it explains core ideas clearly.
Q2: Does selection sort work well for large data?
It is not efficient for huge lists, but it works well for small tasks and learning purposes.
Q3: Can selection sort handle negative numbers?
Yes, it works with any numbers as long as the comparison is correct.
Q4: How is selection sort different from bubble sort?
Selection sort searches for the smallest element and places it in the correct spot, while bubble sort repeatedly swaps nearby values.
Q5: Is recursion a better option for selection sort?
Recursion is good for learning and understanding concepts, but loops are usually faster and easier to manage in real programs.
Conclusion
Selection sort may be one of the simpler algorithms, but it opens the door to understanding much deeper ideas in programming. It teaches searching, swapping, iteration, recursion, and algorithmic thinking in a gentle way. With the different Swift programs shown above, you can explore the algorithm from many angles and see how flexible it can be. Keep practicing, try new arrays, and experiment with your own variations. With time, sorting will feel natural, and your Swift skills will continue to grow.




