Searching Algorithms: Summary
Explore the fundamentals of searching algorithms, focusing on linear and binary search techniques. Understand how data structure impacts algorithm choice, and learn practical decision guidelines for using each search method effectively in JavaScript.
Having studied both search algorithms, the table below summarizes their key differences.
Feature | Linear Search | Binary Search |
Data requirement | Works on ordered or unordered collections (e.g., arrays, lists) | Requires sorted data in an indexed structure (e.g., arrays, lists) |
Time complexity | O(n) in the worst case | O(log n) in the worst case |
Space complexity | O(1) |
|
How it works | Checks elements one by one from start to end | Repeatedly divides the search space in half |
Best for | Small collections, unsorted data, single lookups | Large sorted collections, repeated lookups |
Implementation | Simpler often has a single loop | Slightly more complex, uses variables such as low, high, and mid |