Mastering Sorting Algorithms in Swift for iOS Development
Written on
Chapter 1: Introduction to Sorting Algorithms
As a professional iOS developer, I initially didn’t find a need to utilize sorting algorithms in my daily work. After completing a comprehensive full-stack coding bootcamp, I followed a common path that many junior developers take when pursuing their first entry-level positions: I dedicated time to problem-solving on platforms like LeetCode.
Studying for Software Engineering Interviews
I structured my weekdays, adhering to a Monday to Friday schedule, committing at least two hours daily to studying data structures and algorithms. My go-to resources included:
- Udemy's course by Colt Steele: JavaScript Algorithms and Data Structures Masterclass
- The Grind 75 (previously known as Blind 75)
After three weeks of submitting applications, I landed an interview with the company where I currently work. Although there weren't any positions available for full-stack web engineers, I had the option to pursue an opportunity in either the Android or iOS development teams.
At that time, I had been sharpening my skills in data structures and algorithms using JavaScript. While I was also learning cross-platform development with React Native as a side project, I lacked experience in native mobile development. The thought of transitioning from full-stack web development to iOS development was daunting, but I recognized it as a crucial aspect of software engineering that I wanted to explore.
Fortunately, during my interviews for the iOS software engineering role, the company allowed me to showcase my technical skills using JavaScript. I recall being asked several questions related to sorting arrays and thoroughly enjoyed discussing time complexity with my interviewers.
Chapter 2: Implementing Sorting Algorithms in Swift
Now that about eight months have passed since those interviews, I believe it's the right moment to delve into writing sorting algorithms in Swift. I found an excellent resource that effectively illustrates the differences among three popular sorting algorithms.
Before we dive into implementations, let’s check out this informative video on sorting algorithms in Swift:
Bubble Sort
To illustrate bubble sort, envision a scenario where you are sorting books alphabetically. The process involves comparing and swapping adjacent books, progressively moving through the array until everything is in order.
public func bubbleSort(_ elements: [T]) -> [T] where T: Comparable {
return bubbleSort(elements, <)
}
public func bubbleSort(_ elements: [T], _ comparison: (T, T) -> Bool) -> [T] {
var array = elements
for i in 0..<array.count {
for j in 0..<array.count - 1 - i {
if comparison(array[j + 1], array[j]) {
array.swapAt(j, j + 1)}
}
}
return array
}
Insertion Sort
In insertion sort, you start with the first two books sorted and then integrate each subsequent book into the correct position, one by one.
func insertionSort(_ array: [T]) -> [T] {
var sortedArray = array
for index in 1..<sortedArray.count {
let temp = sortedArray[index]
var currentIndex = index
while currentIndex > 0 && temp < sortedArray[currentIndex - 1] {
sortedArray[currentIndex] = sortedArray[currentIndex - 1]
currentIndex -= 1
}
sortedArray[currentIndex] = temp
}
return sortedArray
}
Quick Sort
In quick sort, you select a pivot and partition the array. Elements that are less than the pivot go to the left, while those greater go to the right. This process is repeated for the sub-arrays until everything is sorted.
func quicksort(_ a: [T]) -> [T] {
guard a.count > 1 else { return a }
let pivot = a[a.count / 2]
let less = a.filter { $0 < pivot }
let equal = a.filter { $0 == pivot }
let greater = a.filter { $0 > pivot }
return quicksort(less) + equal + quicksort(greater)
}
To enhance your understanding, watch this insightful video on Quick Sort in Swift:
If you found this article helpful or have alternative approaches to sorting algorithms, please feel free to share your thoughts in the comments!