Join the Swift Algorithm Club!
The Swift Algorithm Club is a popular open source project (with over 4,000 stars on GitHub) that implements popular algorithms and data structures in Swift. Now, we’re pleased to announce that the project is officially sponsored by raywenderlich.com – and we’re looking for contributors! By Kelvin Lau.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Join the Swift Algorithm Club!
10 mins
About six months ago, the author of the iOS Apprentice, Matthijs Hollemans, started an open source project to implement popular algorithms and data structures in Swift: the Swift Algorithm Club.
At first, it was only Matthijs at the helm. But the popularity of the repository grew, and currently it boasts 37 unique contributors and over 11,500 stars, proving to be one of the most popular Swift repositories on GitHub.
Today marks a new chapter of the club’s life. Matthijs and I are pleased to announce that the Swift Algorithm Club is now an official project sponsored by raywenderlich.com!
Specifically, raywenderlich.com team members Chris Pilcher and Kelvin Lau (yours truly!) will be leading the effort to maintain the repository, expand the Swift Algorithm Club even further, and post about Swift algorithm topics regularly here on the site.
Keep reading to find out more what the Swift Algorithm Club is, how you can use it to learn, and how you can contribute!
What is the Swift Algorithm Club?
As a Swift developer, you have access to a plethora of methods in the Swift standard library that implement handy algorithms, like sort()
for example.
But sometimes you need a custom algorithm that goes beyond what the standard library provides. A good developer not only recognizes what a piece of software does, but also how the software accomplishes the task.
That’s where the Swift Algorithm Club comes in. It’s a free, open source collection of implementations of popular algorithms and data structures in Swift, with detailed explanations of how they work.
If you’re a computer science student who needs to learn this stuff for exams — or if you’re a self-taught programmer who wants to brush up on the theory behind your craft — this is for you!
The goal of this project is to explain how algorithms work. The focus is on clarity and readability of the code, not on making a reusable library that you can drop into your own projects. That said, most of the code should be ready for production use but you may need to tweak it to fit into your own codebase.
Now that the Swift Algorithm club is an official part of raywenderlich.com, Chris and I will be publishing monthly articles about the Swift Algorithm Club right here on this site.
As a sneak peek of what’s coming and to get an idea of what the Swift Algorithm Club is all about, here’s an example algorithm that most coders should be familiar with: Linear Search.
Example: Implementing Linear Search
The goal of the linear search algorithm is to find a particular value in an array.
Given an array of generic objects, you iterate through the array and compare each object to the object you’re looking for. If the two objects are equal, stop and return the current index. If not, continue to look for the next object as long as there are objects left to compare.
Take the example of an array of numbers: [5, 2, 4, 7]
. You’d like to check if the array contains the number 2
.
You start by comparing the first number in the array —5
— to the number you’re looking for — 2
. They aren’t equivalent, so you continue to the next array element.
You compare the number 2
from the array to our number 2
and — hey! — they’re equal. You can stop iterating and return 1
, which is the index of the number 2
in the array.
The Code
Here’s a simple implementation of linear search in Swift:
func linearSearch<T: Equatable>(_ array: [T], _ object: T) -> Int? {
for (index, obj) in array.enumerated() where obj == object {
return index
}
return nil
}
Credit: Patrick Balestra.
Put this code in a playground and test it like so:
let array = [5, 2, 4, 7]
linearSearch(array, 2) // This will return 1
Linear Search Performance
Linear search runs at O(n); it compares the object you’re looking for with each object in the array. Therefore, the time it takes is directly proportional to the length of the array. In the worst case, you’ll need to look at all elements in the array.
The best-case performance is O(1), but this is pretty rare; in a randomly assigned array, the odds of this are 1:n since the object you’re looking for has to be positioned in the first array index. On average, linear search needs to look at half the objects in the array.
If you’re not familiar with Big O notation, check out our short guide to it here. For more information on this algorithm, check out Linear search on Wikipedia.
Back to the Swift Algorithm Club
That concludes a quick example of the type of thing that’s in the Swift Algorithm Club. Now, let’s take a quick look at how the repository is structured.
The resources in the Swift Algorithm Club repository generally follow a particular pattern, which we’ve found to be essential for capturing the essential information of an algorithm:
- Theory
- Example
- Code
- Performance
The combination of theory, example, code, and performance gives the reader the best opportunity to learn about the algorithm — and how to best apply it to their situation.
Now let’s take a look at how you can enjoy the Swift Algorithm club – both as a learner and as a contributor.
Getting Started as a Learner
If learning about algorithms like this sounds fun, just check out the official Swift Algorithm Club repository.
It’s a great learning resource for both beginning students and algorithm veterans. Beyond just algorithms, there’s also a new section on popular interview questions — great for priming yourself for your next interview!
Here’s a recommended list of data structures and algorithms to get started with:
- Stack
- Queue
- Insertion Sort
- Binary Search and Binary Search Tree
- Merge Sort
- Boyer-Moore string search
Once you’re done with those, feel free to explore the rest of the repository. One thing is for certain — the repository will only continue to grow!