跳转至

1 Why Learn Data Structures & Algorithms?

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 lists, maps and sets. These are data structures that hold a collection of data, each structure having its own performance characteristics.

For example, consider the difference between a list and a set. Both are meant to hold a collection of elements, but searching for an element in a list takes far longer than searching for an element in a set. On the other hand, you can order the elements of a list 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 Dart. At the same time, the high-level expressiveness of Dart 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. 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, and 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, since Set 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 it for this purpose.

Being familiar with a data structures allows you to extract additional context from the code. 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 code. The Dart core library has a small set of general-purpose collection types; they don’t cover every case. And, yet, as you’ll 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 common ones gives you a wider range of tools that you can use to build your apps.

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. Most chapters will include some challenges at the end. The solutions to these challenges appear in the Challenge Solutions section at the end of the book.

Do yourself a favor and make a serious attempt at solving each challenge before peeking at the solution.

There are five content sections in this book, each covering a specific theme:

  1. Introduction
  2. Elementary Data Structures
  3. Trees
  4. Sorting Algorithms
  5. Graphs

It’s best to read this book in chronological order, but it also works well as a reference if you have some prior knowledge.

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’ll be well on the way to becoming a Dart data structures and algorithms expert. It’s time to get started!