1.
Why Learn Data Structures & Algorithms?
Written by Kelvin Lau
The study of data structures is one of efficiency. Given a particular amount of data, what is the best way to store it to achieve a particular goal?
As a programmer, you regularly use a variety of collection types, such as arrays, dictionaries and sets. These are data structures that hold a collection of data, each structure having its own performance characteristics.
As an example, consider the difference between an array and a set. Both are meant to hold a collection of elements, but searching for an element in an array takes far longer than searching for an element in a set. On the other hand, you can order the elements of an array but you can’t order the elements of a set.
Data structures are a well-studied discipline, and the concepts are language agnostic; A data structure from C is functionally and conceptually identical to the same data structure in any other language, such as Swift. At the same time, the high-level expressiveness of Swift makes it an ideal choice for learning these core concepts without sacrificing too much performance.
Algorithms on the other hand are a set of operations that complete a task. This can be a sorting algorithm that moves data around to put it in order. Or it can be an algorithm that compresses an 8K picture to a manageable size. Algorithms are essential to software and many have been created to act as building blocks for useful programs.
So why should you learn data structures and algorithms?
Interviews
An important reason to keep your algorithmic skills up to par is to prepare for interviews. Most companies have at least one or two algorithmic questions to test your abilities as an engineer. Having a strong foundation in data structures and algorithms is the “bar” for many software engineering positions.
Work
Using an appropriate data structure is crucial when working with lots of data. Using the right algorithm plays a significant role in the performance and scalability of your software. Your mobile apps will be more responsive and have better battery life. Your server apps will be able to handle more concurrent requests and use less energy. Algorithms often include proofs of correctness that you can leverage to build better software.
Using the correct data structure also helps to provide context to the reader. As an example, you might come across a Set
in your code base. Immediately, you can deduce:
- Consumers of the
Set
don’t care about the order of the elements, sinceSet
is an unordered collection -
Set
also ensures that there are no duplicate values. You can assume consumers are working with unique data -
Set
is great for checking for value membership, so it’s likely the engineer introduced aSet
for this purpose
Once you are familiar with various data structures, you can extract additional context from code using data structures as “cues”. This is a powerful skill that will help you understand how a piece of software works.
Self-Improvement
Knowing the strategies used by algorithms to solve tricky problems gives you ideas for improvements that you can make to your own code. The Swift standard library has a small set of general-purpose collection types; they definitely don’t cover every case. And, yet, as you will see, these primitives can be used as a great starting point for building more complex and special-purpose abstractions. Knowing more data structures than just the standard array and dictionary gives you a bigger collection of tools that you can use to build your own apps with.
A wise man once said: The practice of algorithms is akin to how musicians practice their scales. The more polished your foundations are, the better you will become in working with more complex pieces of software.
The goal of this book
This book is meant to be both a reference and an exercise book. If you’re familiar with other books from raywenderlich.com, you’ll feel right at home. Each chapter is followed by a short chapter with some challenges. The solutions to these challenges appear at the end of each of these chapters. Do yourself a favor and make a serious attempt at solving each challenge before peeking at the solution.
This book is divided into five sections, each covering a specific theme:
- Introduction
- Elementary data structures
- Trees
- Sorting
- Graphs
This book is best read in chronological order, but it also works well as a reference if you want to skip around.
If you’re new to the study of algorithms and data structures, you may find some of the material challenging. But, if you stick with it to the end, you will be well on the way to becoming a Swift data structures and algorithms master. Let’s get started!