1 introduction to algorithms

 

In this chapter

  • You get a foundation for the rest of the book.
  • You write your first search algorithm (binary search).
  • You learn how to talk about the running time of an algorithm (Big O notation).

1.1 Introduction

An algorithm is a set of instructions for accomplishing a task. Every piece of code could be called an algorithm, but this book covers the more interesting bits. I chose the algorithms in this book for inclusion because they’re fast, or they solve interesting problems, or both. Here are some highlights:

  • Chapter 1 talks about binary search and shows how an algorithm can speed up your code. In one example, the number of steps needed goes from 4 billion down to 32!
  • A GPS device uses graph algorithms (as you’ll learn in chapters 6, 7, and 8) to calculate the shortest route to your destination.
  • You can use dynamic programming (discussed in chapter 9) to write an AI algorithm that plays checkers.

In each case, I’ll describe the algorithm and give you an example. Then I’ll talk about the running time of the algorithm in Big O notation. Finally, I’ll explore what other types of problems could be solved by the same algorithm.

1.1.1 What you’ll learn about performance

1.1.2 What you’ll learn about solving problems

1.2 Binary search

1.2.1 A better way to search

1.2.2 Exercises

1.2.3 Running time

1.3 Big O notation

1.3.1 Algorithm running times grow at different rates

1.3.2 Visualizing different Big O run times

1.3.3 Big O establishes a worst-case run time

1.3.4 Some common Big O run times

1.3.5 Exercises

1.3.6 The traveling salesperson

1.4 Recap