Skip to main content

Searching

2 lessonsAbout 12 minutesIntermediate

This part of the DSA Tutorial runs from Binary Search Explained through to Linear Search and When to Use It. There are 2 lessons here, and working through them takes about 12 minutes at a steady pace.

It follows on from Sorting, so finish that first if you have not already — the examples below assume you are comfortable with it.

Each lesson below says what it covers before you open it. Read them in order the first time; afterwards this page works as a index you can jump back into when you need to check one thing.

  1. 1Binary Search ExplainedBinary search finds an item in a sorted array in O(log n) by halving the range each step. Learn the correct JavaScript implementation, the overflow and off-by-one traps, and when it is worth sorting first.
  2. 2Linear Search and When to Use ItLinear search is O(n) and often the right answer. Learn when scanning beats sorting or hashing, and how JavaScript's find, some, includes and indexOf differ.