Rushikesh
Rushikesh

Reputation: 1

Sorting Arrays Before binary search will Take big O (n)?

When i was learning about Big O Notations , while getting to know the binary search algorithm as it requires sorting the array before searching . I had a question that isn't sorting going to take the same amount of time as linear search as it will look at each and every memory locations there is ?

Upvotes: 0

Views: 457

Answers (1)

Bill the Lizard
Bill the Lizard

Reputation: 405675

Sorting an array is typically O(n * log(n)), so it is slower than simply doing one linear search on an unsorted array.

The time savings come in when you need to do more than log(n) searches on the same array. In that case, it's worthwhile to sort it once, then each search only takes an additional log(n) steps.

Upvotes: 1

Related Questions