This article has been published at Function Space.
(This is just my back up copy of the thing, do check this out on FS for better experience.)
Suppose you are a teacher of a class of 1000 students. There is a scholarship offer for the top 50 students of your class. Now, you as a teacher, want to find those top 50 students. How would you go about doing this?
A) There is an easy approach you could follow. You just sort the students according to their ranks and pick up the top 50 from this sorted list. But you will realize that you have wasted a lot of effort in sorting the other 950 students of your class. The best known sorting algorithm takes O(n∗log(n)), where n is the number of students in the class.
B) Another approach which is slightly clever is as follows. Pick up the student with highest rank from the list by going through all of them. Then, remove him from the list and find out the highest rank from the remaining students, which turns out to be the student with second highest rank. Repeat the process 50 times and you have the top 50 students with you. Upon trying to analyze the time complexity of this approach, it would take O(k∗n) time, where k is 50 in this case.
So, is this really better than the first? Continue reading →