News for hackers. Stuff that matters.
Re:Optimizations on Linear Search (2016) by kleiba on Saturday October 03, 2020 @06:47PM (#24675241)
Quite a few comments here already remark on the "computer scientists leaving the room" joke at the beginning of the article.

I'd like to add another computer sciency aspect that hasn't been covered yet. If the original question is "what is the fastest algorithm to find the largest number in an unsorted array" you might just answer: "it depends on what you want to do with it afterwards."

Sure, in a job interview or in a computer science class you might want to just look at any one problem independent of anything else. In the real world, context matters though.

For example, we know that in an unsorted list (which we're dealing with here), an element can be removed by overwriting it with the last element of the list and decrementing the length of the list. So perhaps someone is actually looking at solving a more complex problem, say, processing a list of tasks from highest importance to lowest and removing each task once it has been processed. But they are new to the team, and there is a huge and complicated code base that has been created over years and that they now have to work with. And it just so happens that the actual algorithm is already implemented in the code base, except for one function:

    while list is not empty:
        task = find_most_important_task(list) # your TODO
        process(task)
        list.remove(task)
Now, this is the same question as in the job interview... or is it? Assuming that find_most_important_task is implemented as a linear search, the above algorithm is O(n^2).

However, knowing which task you're optimizing for -- that is, the algorithm as a whole -- you can obviously do better. Let's say, your list data structure gets a new flag that is true iff the list is sorted. Then you could use the following implementation for the max search:

    find_most_important_task(list):
        if list is not sorted:
            list.sort()
        return list[list.length - 1]
With this implementation, a single call to find_most_important_task(list) would be much worse in term of O-notation than linear search: O(nlogn) vs. O(n). However, amortized over the whole algorithm, the complexity of find_most_important_task is actually only O(logn)!

So, yeah, I don't really know why I wrote all this up. Probably because it is late. Certainly not because it contained anything new. It's also contrived. Oh well. Sorry for wasting anyone's time...

There are no comments on this comment yet.