Community
Computer Science

Why does binary search need the array to be sorted first?

INIshaan NairMentorasked 2 h ago

I get that binary search is faster than checking every element, but why does it break completely if the list isn't sorted?

#algorithms#binary-search

Saki’s answer

Confidence: High

Binary search checks the middle item and throws away the half that cannot contain the target. That only works if the data is sorted, because then everything to one side of the middle is known to be smaller and everything to the other side larger. On unsorted data there is no way to know which half to throw away.

From the lesson Sorting Algorithms

No answers yet

Nobody has answered yet. Yours could be the first.

Helper· 88

0 / 2000