# Data Structures & Algorithms in Swift

The most popular and comprehensive book on Swift algorithms & data structures! Covers search, sort, trees, stacks, and more. By Vincent Ngo & Kelvin Lau.

## 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...

more## 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!