Computer Science

Using Linear Search When Binary Search Needs Sorted Data

Students run binary search on an unsorted array and expect it to work like a faster linear search.

Quick check

Why students make it

Binary search feels like it is just skipping ahead to save time. The missing idea is that each skip is only valid when the data is sorted in the same order the search uses.

The rule done right

Use binary search only when the collection is sorted by the search key. If the data is not sorted, use linear search, or sort it first and then use binary search.