• Stars
    star
    28,471
  • Rank 613 (Top 0.02 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 8 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Algorithms and data structures in Swift, with explanations!

Swift Algorithm Club

Welcome to the Swift Algorithm Club!

Here you'll find implementations of popular algorithms and data structures in everyone's favorite new language 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 -- you've come to the right place!

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.

Code is compatible with Xcode 10 and Swift 4.2. We'll keep this updated with the latest version of Swift. If you're interested in a GitHub pages version of the repo, check out this.

😍 Suggestions and contributions are welcome! 😍

Important links

What are algorithms and data structures? Pancakes!

Why learn algorithms? Worried this isn't your cup of tea? Then read this.

Big-O notation. We often say things like, "This algorithm is O(n)." If you don't know what that means, read this first.

Algorithm design techniques. How do you create your own algorithms?

How to contribute. Report an issue to leave feedback, or submit a pull request.

Where to start?

If you're new to algorithms and data structures, here are a few good ones to start out with:

The algorithms

Searching

String Search

  • Brute-Force String Search. A naive method.
  • Boyer-Moore. A fast method to search for substrings. It skips ahead based on a look-up table, to avoid looking at every character in the text.
  • Knuth-Morris-Pratt. A linear-time string algorithm that returns indexes of all occurrencies of a given pattern.
  • Rabin-Karp Faster search by using hashing.
  • Longest Common Subsequence. Find the longest sequence of characters that appear in the same order in both strings.
  • Z-Algorithm. Finds all instances of a pattern in a String, and returns the indexes of where the pattern starts within the String.

Sorting

It's fun to see how sorting algorithms work, but in practice you'll almost never have to provide your own sorting routines. Swift's own sort() is more than up to the job. But if you're curious, read on...

Basic sorts:

Fast sorts:

Hybrid sorts:

Special-purpose sorts:

Bad sorting algorithms (don't use these!):

Compression

Miscellaneous

Mathematics

Machine learning

  • k-Means Clustering. Unsupervised classifier that partitions data into k clusters.
  • k-Nearest Neighbors
  • Linear Regression. A technique for creating a model of the relationship between two (or more) variable quantities.
  • Logistic Regression
  • Neural Networks
  • PageRank
  • Naive Bayes Classifier
  • Simulated annealing. Probabilistic technique for approximating the global maxima in a (often discrete) large search space.

Data structures

The choice of data structure for a particular task depends on a few things.

First, there is the shape of your data and the kinds of operations that you'll need to perform on it. If you want to look up objects by a key you need some kind of dictionary; if your data is hierarchical in nature you want a tree structure of some sort; if your data is sequential you want a stack or queue.

Second, it matters what particular operations you'll be performing most, as certain data structures are optimized for certain actions. For example, if you often need to find the most important object in a collection, then a heap or priority queue is more optimal than a plain array.

Most of the time using just the built-in Array, Dictionary, and Set types is sufficient, but sometimes you may want something more fancy...

Variations on arrays

  • Array2D. A two-dimensional array with fixed dimensions. Useful for board games.
  • Bit Set. A fixed-size sequence of n bits.
  • Fixed Size Array. When you know beforehand how large your data will be, it might be more efficient to use an old-fashioned array with a fixed size.
  • Ordered Array. An array that is always sorted.
  • Rootish Array Stack. A space and time efficient variation on Swift arrays.

Queues

  • Stack. Last-in, first-out!
  • Queue. First-in, first-out!
  • Deque. A double-ended queue.
  • Priority Queue. A queue where the most important element is always at the front.
  • Ring Buffer. Also known as a circular buffer. An array of a certain size that conceptually wraps around back to the beginning.

Lists

  • Linked List. A sequence of data items connected through links. Covers both singly and doubly linked lists.
  • Skip-List. Skip List is a probabilistic data-structure with same logarithmic time bound and efficiency as AVL/ or Red-Black tree and provides a clever compromise to efficiently support search and update operations.

Trees

  • Tree. A general-purpose tree structure.
  • Binary Tree. A tree where each node has at most two children.
  • Binary Search Tree (BST). A binary tree that orders its nodes in a way that allows for fast queries.
  • Red-Black Tree. A self balancing binary search tree.
  • Splay Tree. A self balancing binary search tree that enables fast retrieval of recently updated elements.
  • Threaded Binary Tree. A binary tree that maintains a few extra variables for cheap and fast in-order traversals.
  • Segment Tree. Can quickly compute a function over a portion of an array.
  • kd-Tree
  • Sparse Table. Another take on quickly computing a function over a portion of an array, but this time we'll make it even quicker!.
  • Heap. A binary tree stored in an array, so it doesn't use pointers. Makes a great priority queue.
  • Fibonacci Heap
  • Trie. A special type of tree used to store associative data structures.
  • B-Tree. A self-balancing search tree, in which nodes can have more than two children.
  • QuadTree. A tree with 4 children.
  • Octree. A tree with 8 children.

Hashing

  • Hash Table. Allows you to store and retrieve objects by a key. This is how the dictionary type is usually implemented.
  • Hash Functions

Sets

  • Bloom Filter. A constant-memory data structure that probabilistically tests whether an element is in a set.
  • Hash Set. A set implemented using a hash table.
  • Multiset. A set where the number of times an element is added matters. (Also known as a bag.)
  • Ordered Set. A set where the order of items matters.

Graphs

Puzzles

A lot of software developer interview questions consist of algorithmic puzzles. Here is a small selection of fun ones. For more puzzles (with answers), see here and here.

Learn more!

Like what you see? Check out Data Structures & Algorithms in Swift, the official book by the Swift Algorithm Club team!

Data Structures & Algorithms in Swift Book

You’ll start with the fundamental structures of linked lists, queues and stacks, and see how to implement them in a highly Swift-like way. Move on to working with various types of trees, including general purpose trees, binary trees, AVL trees, binary search trees, and tries.

Go beyond bubble and insertion sort with better-performing algorithms, including mergesort, radix sort, heap sort, and quicksort. Learn how to construct directed, non-directed and weighted graphs to represent many real-world models, and traverse graphs and trees efficiently with breadth-first, depth-first, Dijkstra’s and Prim’s algorithms to solve problems such as finding the shortest path or lowest cost in a network.

By the end of this book, you’ll have hands-on experience solving common issues with data structures and algorithms β€” and you’ll be well on your way to developing your own efficient and useful implementations!

You can find the book on the raywenderlich.com store.

Credits

The Swift Algorithm Club was originally created by Matthijs Hollemans.

It is now maintained by Vincent Ngo, Kelvin Lau, and Richard Ash.

The Swift Algorithm Club is a collaborative effort from the most algorithmic members of the raywenderlich.com community. We're always looking for help - why not join the club? :]

License

All content is licensed under the terms of the MIT open source license.

By posting here, or by submitting any pull request through this forum, you agree that all content you submit or create, both code and text, is subject to this license. Razeware, LLC, and others will have all the rights described in the license regarding this content. The precise terms of this license may be found here.

Build Status

More Repositories

1

swift-style-guide

The official Swift style guide for Kodeco.
12,820
star
2

objective-c-style-guide

A style guide that outlines the coding conventions for Kodeco
3,092
star
3

flta-materials

The projects and the materials that accompany the Flutter Apprentice book
Dart
2,344
star
4

SKTUtils

Sprite Kit helper classes and functions. From the book iOS Games by Tutorials.
Swift
661
star
5

c-sharp-style-guide

C# Style Guide for Game Tech tutorials
484
star
6

ios-interview

The resources and instructions for the iOS Sample Project interview question.
423
star
7

kotlin-style-guide

Kodeco's Kotlin style guide
387
star
8

comb-materials

The projects and the materials that accompany the Combine: Asynchronous Programming With Swift Book
Swift
290
star
9

met-materials

The projects and the materials that accompany the Metal by Tutorials book.
Swift
283
star
10

deprecated-books

Kodeco Deprecated Books
258
star
11

sui-materials

The projects and the materials that accompany the SwiftUI by Tutorials book
Swift
215
star
12

adva-materials

The projects and materials that accompany the Real-World Android by Tutorials book
Kotlin
213
star
13

mcon-materials

The projects and materials that accompany the Modern Concurrency in Swift book
Swift
201
star
14

mvc-modern-approach

Sample project for an article at raywenderlich.com.
Swift
170
star
15

english-style-guide

Style guide for writing in English for tutorials and articles at Kodeco.
157
star
16

alg-materials

The projects and the materials that accompany the Data Structures & Algorithms in Swift book
Swift
152
star
17

rxs-materials

The projects and the materials that accompany the RxSwift: Reactive Programming with Swift Book
Swift
147
star
18

java-style-guide

The official Java style guide for raywenderlich.com
Shell
146
star
19

vapor-til

The TIL Application for the Vapor book
Swift
145
star
20

da-materials

The projects and the materials that accompany the Dart Apprentice book
Dart
139
star
21

dsk-materials

The projects and the materials that accompany the Data Structures & Algorithms in Kotlin book
Kotlin
137
star
22

jet-materials

The projects and the materials that accompany the Jetpack Compose book
Kotlin
134
star
23

ia-materials

The projects and materials that accompany the UIKit Apprentice book
HTML
131
star
24

sa-materials

The projects and the materials that accompany the Swift Apprentice book.
Swift
124
star
25

dsad-materials

The projects and the materials that accompany the Data Structures & Algorithms in Dart book
Dart
116
star
26

vpr-materials

The projects and materials that accompany the Server-Side Swift with Vapor book
Swift
110
star
27

arch-materials

The projects and the materials that accompany the Advanced iOS App Architecture book
Swift
97
star
28

atdd-materials

The projects and the materials that accompany the Android Test-Driven Development by Tutorials book
Kotlin
95
star
29

rwf-materials

The projects and materials that accompany the Real-World Flutter by Tutorials book
Dart
83
star
30

learning-roadmaps

Learning roadmaps for students at raywenderlich.com.
82
star
31

apr-materials

The projects and materials that accompany the Apple Augmented Reality by Tutorials book
Swift
81
star
32

suia-materials

The projects and the materials that accompany the SwiftUI Apprentice book
Swift
80
star
33

itdd-materials

The projects and the materials that accompany the iOS Test-Driven Development by Tutorials book
Swift
72
star
34

rwi-materials

The projects and materials that accompany the Real-World iOS by Tutorials book.
Swift
69
star
35

ka-materials

The projects and the materials that accompany the Kotlin Apprentice book.
Kotlin
65
star
36

ana-materials

The projects and materials that accompany the Advanced Android App Architecture book
Kotlin
60
star
37

kco-materials

The projects and materials that accompany the Kotlin Coroutines by Tutorials book
Shell
59
star
38

aa-materials

The projects and the materials that accompany the Android Apprentice book.
Kotlin
59
star
39

mad-materials

The projects and materials that accompany the App Design Apprentice book
Shell
58
star
40

des-materials

The projects and materials that accompany the Design Patterns by Tutorials book
Shell
58
star
41

swiftui-example-app-koober

Porting the example app from our Advanced iOS App Architecture book from UIKit to SwiftUI.
Swift
57
star
42

cdt-materials

The projects and the materials that accompany the Core Data by Tutorials book
Swift
51
star
43

video-yfsa1-materials

Student Materials for Your First iOS and SwiftUI App: An App from Scratch.
Swift
49
star
44

kmpf-materials

The projects and materials that accompany the Kotlin Multiplatform by Tutorials book
Kotlin
48
star
45

iat-materials

The projects and the materials that accompany the iOS Animations by Tutorials book
Shell
44
star
46

dbg-materials

The projects and the materials that accompany the Advanced Apple Debugging & Reverse Engineering book
C
43
star
47

mos-materials

The projects and materials that accompany the macOS by Tutorials book
Swift
42
star
48

advs-materials

The projects and materials that accompany the Expert Swift book
Swift
41
star
49

rxa-materials

The projects and the materials that accompany the Reactive Programming with Kotlin Book
Shell
35
star
50

MyRWTutorial

A reusable starter project for raywenderlich.com tutorials
Swift
34
star
51

video-jcomp-materials

The projects and the materials that accompany the Jetpack Compose course
Kotlin
34
star
52

dag-materials

The projects and the materials that accompany the Dagger by Tutorials book
Kotlin
31
star
53

sat-materials

The projects and materials that accompany the SwiftUI Animations by Tutorials book
Swift
30
star
54

daf-materials

The projects and the materials that accompany the Dart Apprentice: Fundamentals book
Dart
30
star
55

bkk-materials

Kodeco Book Materials Template Repo
Shell
30
star
56

sda-materials

The projects and the materials that accompany the Saving Data on Android book
Kotlin
29
star
57

con-materials

About The projects and the materials that accompany the Concurrency by Tutorials book
Shell
28
star
58

RWDevCon-App

The RWDevCon app
Swift
28
star
59

wos-materials

The projects and the materials that accompany the watchOS With SwiftUI by Tutorials book
Swift
27
star
60

recipes

27
star
61

fpk-materials

The projects and materials that accompany the Functional Programming in Kotlin by Tutorials book
Kotlin
26
star
62

pasi-materials

The projects and the materials that accompany the iOS App Distribution & Best Practices book.
Swift
26
star
63

agit-materials

The projects and the materials that accompany the Advanced Git Book
Shell
25
star
64

SC_SusmitaHorrow

Swift
24
star
65

gita-materials

The projects and the materials that accompany the Git Apprentice book
Shell
24
star
66

not-materials

The projects and the materials that accompany the Push Notifications by Tutorials book
Swift
22
star
67

RWAndroidTutorial

A reusable starter project for raywenderlich.com Android tutorials.
Kotlin
20
star
68

programmer-jokes

Sample repository for the Git Apprentice book
19
star
69

video-ps1-materials

The projects and the materials that accompany the Programming in Swift: Fundamentals course
Swift
19
star
70

alt-materials

The projects and materials that accompany the Auto Layout by Tutorials book
Swift
19
star
71

aat-materials

The projects and materials that accompany the Android Animations by Tutorials book
Kotlin
19
star
72

dabb-materials

The projects and the materials that accompany the Dart Apprentice: Beyond the Basics book
Dart
19
star
73

mlt-materials

The projects and the materials that accompany the Machine Learning by Tutorials book
Shell
18
star
74

video-insta-materials

The projects and the materials that accompany the How to Make an App Like Instagram in iOS video course
Swift
17
star
75

video-yfsa2-materials

Student Materials for Your First iOS and SwiftUI App: Polishing the App.
Swift
17
star
76

universal-links

HTML
17
star
77

video-comb-materials

The projects and the materials that accompany the Reactive Programming in iOS with Combine course
Swift
16
star
78

cat-materials

The projects and the materials that accompany the Catalyst by Tutorials Book
Swift
16
star
79

video-suif-materials

The projects and the materials that accompany the SwiftUI Fundamentals course
Swift
15
star
80

video-sdios-materials

The projects and the materials that accompany the Saving Data in iOS course
Swift
15
star
81

maca-materials

The projects and materials that accompany the macOS Apprentice book
Swift
15
star
82

uapp-materials

The projects and the materials that accompany the Unity Apprentice book
C#
15
star
83

android-bootcamp-summer-2020

Homework solutions for the Android Bootcamp, class of Summer 2020.
Kotlin
15
star
84

video-nurls-materials

The projects and the materials that accompany the Networking with URLSession video course
Swift
15
star
85

ideas

The "ideas" repository for the raywenderlich.com book Mastering Git
14
star
86

kodeco-android-cookiecutter-template

A cookiecutter πŸͺ template for bootstrapping new Android Tutorial projects for Kodeco!
Kotlin
14
star
87

video-ukf-materials

The projects and the materials that accompany the UIKit Fundamentals video course
Swift
13
star
88

git-materials

The projects and the materials that accompany the Mastering Git Book
Shell
13
star
89

video-tv-materials

The projects and the materials that accompany the Table Views video course
Swift
12
star
90

video-iosa-materials

The projects and the materials that accompany the UIKit Animation course
Swift
12
star
91

video-jca-materials

The projects and materials that accompany the Jetpack Compose Animations course
Kotlin
12
star
92

rwdevcon-materials

Required materials for RWDevCon attendees.
12
star
93

video-kf1-materials

The projects and the materials that accompany the Kotlin Flow: Getting Started video course
Kotlin
11
star
94

DadJokes

A way to improve the programmer's day.
Swift
11
star
95

video-aidp-materials

The projects and materials that accompany the Advanced iOS Design Patterns course
Swift
11
star
96

video-abp-materials

The projects and the materials that accompany the Android Background Processing video course
Kotlin
10
star
97

adf-materials

The projects and the materials that accompany the Android Debugging by Tutorials book
Kotlin
10
star
98

video-sli-materials

The projects and the materials that accompany the SwiftUI: Layout & Interfaces video course
Swift
10
star
99

video-swa-materials

The projects and the materials that accompany the SwiftUI: Animation course
Swift
9
star
100

video-yfkaa-materials

The projects and the materials that accompany the Your First Kotlin Android App video course
Kotlin
9
star