Which search algorithm is preferable?

The binary search algorithm has a large value of OO (log n), and sequential search has a large value of OO (n). But we need a sorting algorithm before binary search, and the best value for a large O value for sorting algotithm is O (n.log n). Thus, the large value of O for binary search is O (n.log n), which is greater than sequential search. So which one is preferable to a search algorithm?

+4
source share
2 answers

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

If you need to make a lot of investments and very few searches, a sequential search can be faster.

Keep in mind that a lot of this will not be noticeable until you work with a lot of data.

+3
source

Sequential search is almost never used in optimized applications. Since it is usually better to find the appropriate data structure and then use the one that provides the frequently used search in O (n).

For example, red-black tree is a special kind of balanced binary tree, which provides the insertion / deletion / search of all in O (log n). Therefore, quickly create, fill and search.

+2
source

All Articles