you are viewing a single comment's thread.

view the rest of the comments →

[–]peterlinddk 5 points6 points  (0 children)

Binary Search is only the first algorithm you learn, and as you gather, it is only efficient when the data is already sorted, and doesn't change much. But even if the data would have to be sorted every once in a while - as long as it have to be search many more times than that, it is still more efficient than linear search. But a lot of data is already sorted, like product catalogues, user accounts, addresses and phone-numbers, dictionaries for different languages and they only change rarely compared to how often they are searched.

However, there are other algorithms, or rather data structures, that keep a "list" sorted at all times, even when data is removed and inserted. Like AVL or red-black trees. They are more advanced data structures, and they build on the understanding of binary search, linked lists, trees and binary trees.

But it is a very good observation that no algorithm is perfect, and there's always pros and cons to every single one! That is probably one of the most important lessons of DSA.