A Common-Sense Guide to
Data Structures and
Algorithms
Level Up Your Core Programming Skills
by Jay Wengrow
Version: P1.0 (August 2017)
Copyright © 2017 The Pragmatic Programmers, LLC. This book is licensed to the
individual who purchased it. We don't copy-protect it because that would limit your ability
to use it for your own purposes. Please don't break this trust—you can use this across all of
your devices but please do not share this copy with other members of your team, with
friends, or via file sharing services. Thanks.
Many of the designations used by manufacturers and sellers to distinguish their products
are claimed as trademarks. Where those designations appear in this book, and The
Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been
printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic
Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are
trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher
assumes no responsibility for errors or omissions, or for damages that may result from the
use of information (including program listings) contained herein.
About the Pragmatic Bookshelf
The Pragmatic Bookshelf is an agile publishing company. We’re here because we want to
improve the lives of developers. We do this by creating timely, practical titles, written by
programmers for programmers.
Our Pragmatic courses, workshops, and other products can help you and your team create
better software and have more fun. For more information, as well as the latest Pragmatic
titles, please visit us at http://pragprog.com.
Our ebooks do not contain any Digital Restrictions Management, and have always been
DRM-free. We pioneered the beta book concept, where you can purchase and read a book
while it’s still being written, and provide feedback to the author to help make a better book
for everyone. Free resources for all purchasers include source code downloads (if
applicable), errata and discussion forums, all available on the book's home page at
pragprog.com. We’re here to make your life easier.
New Book Announcements
Want to keep up on our latest titles and announcements, and occasional special offers? Just
create an account on pragprog.com (an email address and a password is all it takes) and
select the checkbox to receive newsletters. You can also follow us on twitter as
@pragprog.
About Ebook Formats
If you buy directly from pragprog.com, you get ebooks in all available formats for one
price. You can synch your ebooks amongst all your devices (including iPhone/iPad,
Android, laptops, etc.) via Dropbox. You get free updates for the life of the edition. And,
of course, you can always come back and re-download your books when needed. Ebooks
bought from the Amazon Kindle store are subject to Amazon's polices. Limitations in
Amazon's file format may cause ebooks to display differently on different devices. For
more information, please see our FAQ at pragprog.com/frequently-asked-
questions/ebooks. To learn more about this book and access the free resources, go to
https://pragprog.com/book/jwdsal, the book's homepage.
Thanks for your continued support,
Andy Hunt
The Pragmatic Programmers
The team that produced this book includes: Andy Hunt (Publisher),
Janet Furlow (VP of Operations), Susannah Davidson Pfalzer (Executive Editor),
Brian MacDonald (Development Editor), Potomac Indexing, LLC (Indexing),
Nicole Abramowtiz (Copy Editor), Gilson Graphics (Layout)
For customer support, please contact support@pragprog.com.
For international rights, please contact rights@pragprog.com.
Table of Contents
Preface
Who Is This Book For?
What’s in This Book?
How to Read This Book
Online Resources
Acknowledgments
1. Why Data Structures Matter
The Array: The Foundational Data Structure
Reading
Searching
Insertion
Deletion
Sets: How a Single Rule Can Affect Efficiency
Wrapping Up
2. Why Algorithms Matter
Ordered Arrays
Searching an Ordered Array
Binary Search
Binary Search vs. Linear Search
Wrapping Up
3. Oh Yes! Big O Notation
Big O: Count the Steps
Constant Time vs. Linear Time
Same Algorithm, Different Scenarios
An Algorithm of the Third Kind
Logarithms
O(log N) Explained
Practical Examples
Wrapping Up
4.
Speeding Up Your Code with Big O
Bubble Sort
Bubble Sort in Action
Bubble Sort Implemented
The Efficiency of Bubble Sort
A Quadratic Problem
A Linear Solution
Wrapping Up
5. Optimizing Code with and Without Big O
Selection Sort
Selection Sort in Action
Selection Sort Implemented
The Efficiency of Selection Sort
Ignoring Constants
The Role of Big O
A Practical Example
Wrapping Up
6. Optimizing for Optimistic Scenarios
Insertion Sort
Insertion Sort in Action
Insertion Sort Implemented
The Efficiency of Insertion Sort
The Average Case
A Practical Example
Wrapping Up
7. Blazing Fast Lookup with Hash Tables
Enter the Hash Table
Hashing with Hash Functions
Building a Thesaurus for Fun and Profit, but Mainly Profit
Dealing with Collisions
The Great Balancing Act
Practical Examples
Wrapping Up
8. Crafting Elegant Code with Stacks and Queues
Stacks
Stacks in Action
Queues
Queues in Action
Wrapping Up
9. Recursively Recurse with Recursion
Recurse Instead of Loop
The Base Case
Reading Recursive Code
Recursion in the Eyes of the Computer
Recursion in Action
Wrapping Up
10. Recursive Algorithms for Speed
Partitioning
Quicksort
The Efficiency of Quicksort
Worst-Case Scenario
Quickselect
Wrapping Up
11. Node-Based Data Structures
Linked Lists
Implementing a Linked List
Reading
Searching
Insertion
Deletion
Linked Lists in Action
Doubly Linked Lists
Wrapping Up
12. Speeding Up All the Things with Binary Trees
Binary Trees
Searching
Insertion
Deletion
Binary Trees in Action
Wrapping Up
13. Connecting Everything with Graphs
Graphs