4

Binary search algorithm has a big O value of O(log n) and a sequential search has a big O value of O(n). But we need sorting algorithm before a binary search and best big O value for a sorting algotithm is O(n.log n). So, effectively, the big O value for binary search is O(n.log n), which is greater than that of the sequential search. So, which one is preferred as searching algo?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Victor Mukherjee
  • 10,487
  • 16
  • 54
  • 97

2 Answers2

3

In practice it depends on how often you search. If you have to search millions of times, you want binary search, even if you have to pay the upfront cost of sorting. It depends on your use case. With binary search, you also ensure that your inserts keep the list sorted, so they become slower as well.

If you need to do a lot of inserts, and very few searches, sequential search might be faster.

Keep in mind that a lot of this won't even be noticeable until you are working with a lot of data.

Oleksi
  • 12,947
  • 4
  • 56
  • 80
2

Sequential search is practically rarely used in optimised applications. Because it is usually much better to find an appropriate data structure then using the one that provides a frequently used search in O(n).

For example, red-black tree is a special kind of balanced binary tree which provides insert/delete/search all in O(log n). So it is fast to create, fill it in and search.

oleksii
  • 35,458
  • 16
  • 93
  • 163