Join the Swift Algorithm Club!
About six months ago, the author of theiOS 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 raywenderlich.com!
Specifically, raywenderlich.com team membersChris Pilcher andKelvin 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.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.
For more information, 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!
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?
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 raywenderlich.com. 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!
转载本站任何文章请注明：转载至神刀安全网，谢谢神刀安全网 » Join the Swift Algorithm Club!