Vikram
Vikram

Reputation: 4096

Which searching technique can be used if the array is unsorted?

Binary search performs search in O(log n). But, it can be used only if the array is sorted.

Which is the best searching technique if the array is unsorted?

Upvotes: 2

Views: 3658

Answers (3)

J. Michael Wuerth
J. Michael Wuerth

Reputation: 292

If your data is unsorted, you can use a hash table to access your data in O(1) time.

Upvotes: 1

Ankur Chauhan
Ankur Chauhan

Reputation: 41

You can do a linear search. But the issue with linear search is that it has performance issues, it takes a lot of time. So I'd suggest to sort the array if possible and then use binary search. If you want even better latency, then try interpolation search, which is a bit optimized version of typical binary Search.

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490018

If you're only doing a few searches, then a basic linear search is about the best you can do.

If you're going to search very often, it's usually better to sort, then use a binary search (or, if the distribution of the contents if fairly predictable, an interpolation search).

Upvotes: 6

Related Questions