Ch 6 · Searching — Finding the Needle · Data Structures & Algorithms
Topic 6 of 10 in Data Structures & Algorithms — Foundations — 2 lessons.
Linear Search
The simplest search: look at each item until you find the target. It always works, on any list, but it is O(n) — on average you scan half the list.
Binary Search
If the data is sorted, you can do far better. Look at the middle: if the target is smaller, throw away the right half; if larger, throw away the left. Each step removes half the remaining items — that is O(log n). A million items take only ~20 steps.
All topics in Data Structures & Algorithms Beginner
- Thinking Like a Computer Scientist
- Arrays & Lists — The Workhorse
- Stacks — Last In, First Out
- Queues — First In, First Out
- Linked Lists — Nodes on a Chain
- Searching — Finding the Needle
- Sorting — Putting Things in Order
- Recursion — Functions That Call Themselves
- Hash Tables — Instant Lookups
- Mini-Projects & Practice