Join the Swift Algorithm Club!

Kelvin Lau
Introducing the Swift Algorithm Club!

Join the Swift Algorithm Club!

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 5,000 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!

Specifically, 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.

Screen Shot 2016-05-31 at 8.24.09 PM

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, 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.enumerate() 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:

  1. Theory
  2. Example
  3. Code
  4. 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:

  1. Stack
  2. Queue
  3. Insertion Sort
  4. Binary Search and Binary Search Tree
  5. Merge Sort
  6. 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!

Screen Shot 2016-05-31 at 8.27.12 PM

Getting Started as a Contributor

Contributing to open source projects is a great learning experience and a great way to give back to the community. And we could definitely use a lot of help on the Swift Algorithm Club, so why not join us? :]

We’re currently looking for contributors to the club for the following tasks:

  • Implement missing algorithms
  • Improve existing algorithms or explanations
  • Help us port everything to Swift 3 (not yet – once it’s closer to release)
  • Unit tests, fixes for typos – no contribution is too small! :]

For more details, please check out our comprehensive guide for contributors and see how you can help.

Where to Go From Here?

Introducing the Swift Algorithm Club!

No matter how long you’ve been writing code, it’s likely there’s something you can learn from digging into the deeper details of structures and algorithms. We’re excited to help provide this information to Swift community.

Our focus is to lay the groundwork for the standard data structures and algorithms you’d encounter in computer science courses. We’d like to expand our interview questions section to encompass as many brain twisting complex algorithms as we can.

Whether you’re a beginning learner or a seasoned algorithm wrangler, there are lots of ways you can contribute:

  • Learner: Ask lots of questions! Developers are generally very nice people, and we learn from each other by investigating problems as a team. Simply open an issue in the repository to get started!
  • Contributor: Help others learn by contributing to the club! Every month, we’ll choose an algorithm to feature on Perhaps we’ll even choose one that you contributed! We’re always happy to recognize outstanding work from the community.

Chris and I are excited to see how the Swift Algorithm Club evolves. Your first step (Yes! Go do it right now!) is to check out the Swift Algorithm Club repository and start reading.

Any questions or comments? Join the discussion on the forum — or come see us on GitHub!

Kelvin Lau

Kelvin is a physicist turned Swift iOS Developer. He's also one of the two maintainers for the Swift Algorithm Club. While he's currently entrenched with iOS development, he often reminisces of his aspirations to be part of the efforts in space exploration.

Outside of programming work, he's an aspiring entrepreneur and musician. You can find him on his twitter at @KelvinlauKL

Other Items of Interest Weekly

Sign up to receive the latest tutorials from each week, and receive a free epic-length tutorial as a bonus!

Advertise with Us!

PragmaConf 2016 Come check out Alt U

Our Books

Our Team

Video Team

... 19 total!

Swift Team

... 16 total!

iOS Team

... 30 total!

Android Team

... 15 total!

macOS Team

... 11 total!

Apple Game Frameworks Team

... 10 total!

Unity Team

... 11 total!

Articles Team

... 11 total!

Resident Authors Team

... 11 total!