Your next career begins with
Save 50% off your seat in our next iOS Bootcamp. Limited time only. Sessions start April 3.
Data Structures & Algorithms in Swift
Version history
Fourth Edition · iOS 15, Swift 5.5, Xcode 13 (Selected)
Sep 15 2021Third Edition · iOS 13, Swift 5.1, Xcode 11
Oct 2 2019The most popular and comprehensive book on Swift algorithms & data structures! Covers search, sort, trees, stacks, and more. By Vincent Ngo & 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
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
Who is this for?
This book is for developers who are comfortable with Swift and want to ace whiteboard interviews, improve the performance of their code, and ensure their apps will perform well at scale.
Covered concepts
- Basic data structures and algorithms
- Protocols for generalizing algorithms
- Building your own data structures using the Swift standard library
- Trees, tries and graphs
- Building algorithms on top of other primitives
- Sorting algorithms
- Algorithmic complexity
- Finding shortest paths, traversals, and subgraphs
Understanding how data structures and algorithms work in code is crucial for creating efficient and scalable apps and acing job interviews. Swift’s standard library and, more recently, the Swift Collections and Algorithms packages contain a robust set of general-purpose collection types and algorithms, yet they don’t cover every case!
In...
moreUnderstanding how data structures and algorithms work in code is crucial for creating efficient and scalable apps and acing job interviews. Swift’s standard library and, more recently, the Swift Collections and Algorithms packages contain a robust set of general-purpose collection types and algorithms, yet they don’t cover every case!
In Data Structures and Algorithms in Swift, you’ll learn how to implement the most popular and useful data structures and when and why you should use one particular data structure or algorithm over another. This set of basic data structures and algorithms will serve as an excellent foundation for building more complex and special-purpose constructs. The high-level expressiveness of Swift makes it an ideal choice for learning these core concepts without sacrificing performance.
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. Traverse those 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!
Before You Begin
This section tells you a few things you need to know before starting, such as what you’ll need for hardware and software, where to find the project files for this book and more.
Section I: Introduction
The chapters in this short but essential section will motivate the study of data structures and algorithms and give you a quick rundown of the Swift standard library, whose facilities you can use as a basis for creating your own data structures and algorithms.
Section II: Elementary Data Structures
This section looks at a few important data structures that are not found in the Swift standard library but form the basis of more advanced algorithms covered in future sections. All of them are collections optimized for (and enforce) a particular access pattern. You will also get a glimpse of how protocols in Swift can be used to build up these useful primitives.
Each concept chapter is followed by a Challenge chapter where you will be asked to answer something about the data structure, write a utility function, or use it directly to solve a common problem. Worked solutions to the Challenge chapters are located at the end of the book. We encourage you not to peek at our solution until you have given the challenge a shot yourself.
Section III: Trees
Trees are another way to organize information, introducing the concept of children and parents. You‘ll take a look at the most common tree types and see how they readily solve specific computational problems. Just like the last section, this section will introduce you to a concept with a chapter, followed by a Challenge chapter to help you hone the skills you are learning.
Trees are a handy way to organize information when performance is critical. Adding them as a tool to your toolbelt will undoubtedly prove to be useful throughout your career.
Section IV: Sorting Algorithms
Putting lists in order is a classical computational problem. Sorting has been studied since the days of vacuum tubes and perhaps even before that. Although you may never need to write your own sorting algorithm using the highly optimized standard library, studying sorting has many benefits. You’ll learn, for example, about the all-important technique of divide-and-conquer, stability, and best and worst case timings.
This section will follow the same structure of introducing you to a concept with a chapter, followed by a Challenge chapter to practice the skills you are acquiring.
Studying sorting may seem a bit academic and disconnected from the “real world” of app development, but understanding the tradeoffs for these simple cases will lead you to a better understanding and let you analyze any algorithm.
Section V: Graphs
Graphs are an instrumental data structure that can model a wide range of things: webpages on the internet, the migration patterns of birds, protons in the nucleus of an atom. This section gets you thinking deeply (and broadly) about using graphs and graph algorithms to solve real-world problems.
The chapters that follow will give the foundation you need to understand graph data structures. Like previous sections, every other chapter will serve as a Challenge chapter so you can practice what you’ve learned.
After completing this section, you will have powerful tools at your disposal to model and solve important real-life problems using graphs. Let’s get started!
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development — plans start at just $19.99/month! Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.
Learn more