Reputation: 4096
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
Reputation: 292
If your data is unsorted, you can use a hash table to access your data in O(1) time.
Upvotes: 1
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
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