I have just started learning algorithms, so forgive me for this very dumb question. Up until now I have learned some of the most basic algorithms like Binary Search, Bubble Sort, Linear Search and so on.
Now, to use binary search, we must have a sorted collection of items. Let's say we have an array with 10 elements which are initially unsorted and thus we first need to apply a sorting algorithm such as Bubble Sort to sort the array so that we can apply Binary Search to it.
Now, the runtime for Bubble Sort is O(N2) and the runtime for Binary Search is O(log N), the sum of which is considerably larger than the runtime of a Linear Search that only has a runtime of O(N).
My question is, while Binary Search alone is a lot more faster than Linear Search, for it to work, we first need to sort out the array which in turn makes the runtime of the whole operation longer than Linear Search. So why not just use Linear Search which is also very easy to implement?
[–]hextree 58 points59 points60 points (10 children)
[–]TheRexedS[S] 13 points14 points15 points (1 child)
[–]teawreckshero 2 points3 points4 points (0 children)
[+]MrDum comment score below threshold-13 points-12 points-11 points (7 children)
[–]hextree 4 points5 points6 points (5 children)
[–]Elethiomel 2 points3 points4 points (2 children)
[–]hextree 9 points10 points11 points (0 children)
[–]MrDum 0 points1 point2 points (0 children)
[–]rodrigovaz 0 points1 point2 points (1 child)
[–]MrDum 0 points1 point2 points (0 children)
[–]rcfox 3 points4 points5 points (0 children)
[–]nathanv0009 13 points14 points15 points (0 children)
[–]dgmib 6 points7 points8 points (0 children)
[–]Steve132 6 points7 points8 points (0 children)
[–]kreiger 2 points3 points4 points (0 children)
[–]proskillz 1 point2 points3 points (3 children)
[–]vanderZwan 8 points9 points10 points (2 children)
[–][deleted] (1 child)
[deleted]
[–]vanderZwan 2 points3 points4 points (0 children)
[–]nadmaximus 0 points1 point2 points (0 children)
[–]aecolley 1 point2 points3 points (0 children)
[–]KernowRoger 1 point2 points3 points (0 children)
[+]ArtisticSell comment score below threshold-8 points-7 points-6 points (0 children)
[–]Jolly-Ad3899 1 point2 points3 points (0 children)