Now that a lot about sorting algorithms has been discussed, you must try to solve the second query of the leaderboard problem discussed at the beginning of the module. In the next video, industry expert Ankit Maheshwari will discuss the approach one needs to take to crack that problem. Let’s see what he has to say before we move on to the code.
Now that you have understood different sorting techniques, we can move on to the second part of the leaderboard example which we took at the beginning of the module. In the leaderboard of the game which I created in my previous company, the second most frequent query which we received in the leaderboard was to fetch top k rankers. Now let's see how we can solve this problem. We could achieve that easily by doing passes on our array and picking the next highest every time. This obviously will take order NK time. Or we can simply sort the players by their scores using an order and login based sorting algorithm and then return the top k players from the sorted list of players. So which method is better? Should we make k passes over the list of players? Or should we sort the list of players once by their scores and then return the top k players from the sorted list? Let's think this through. What if our k is slightly higher, let's say of the order of 51 hundred? In that case, login will be significantly less than k. Even if we take n, that is a total number of players as large as 1 million. Even then, login will still remain less than 20. In that case, order unlock n would be much better than order NK. Most of the queries which we received in our leaderboard were to fetch top 50 players. This means order n login would be faster than order NK because remember, log n of a million is 20 which is lesser than 50. So we decided to use an efficient sorting algorithm which takes at most order n login time, as speed was our biggest concern. Now let's take a moment to recall what sorting algorithm Professor Mulli has taught us and see which we can leverage over here. So we have learned five sorting algorithms so far bubble sort, selection sort, insertion sort, merge sort and Quicksort. Since we need to sort our list of players as quickly as possible, bubble selection and insertion sort wouldn't work for us because they have order n square time complexity. This leaves us with Merge and Quicksort since they are faster at order n login time complexity. But among those, Quicksort has an average of order n login, but a worst case of order n square. But we want our algorithm to take order n login time at most, even if it takes more memory. Since speed was our biggest concern, this leaves us no choice but to use merge sort. But we don't have to implement the merge sort algorithm all over again. We have a standard library function of collections class in Java which uses an enhanced version of merge sort. You can see that mentioned in its Java doc. Now, this method, collections sort takes in two argument. One is the list which needs to be sorted and the other one is a comparator object. The purpose of the comparator object is to provide rules for sorting. In our case, a player with higher score should come first. In case two players have the same score, then the older player should come first. Let's see how we can use comparator object to incorporate these rules. You can infer from the Java dot that compare method takes in two player arguments p one and p two. And it should return a negative value if p one is to appear before p two. Now let's say p one score is greater than p two score, then we would want p one to appear before. Therefore, we shall return a negative value. On the contrary, if p two score is greater than p one score, we shall return a positive value. We can achieve this by simply returning p two score minus p one score in the compare method. But wait, what if the scores are equal? In that case, we will use created at timestamp as a tiebreaker and we shall return p one created at timestamp minus p two created at timestamp I'll leave it up to you guys to figure out why did we choose this return statement for timestamp comparison. Now let's look at the pseudo code for compare function. Compare function takes in two argument player one p one and player two p two. First we calculate the difference that is p two dot score minus p one dot score. And if difference is not zero, that means it's either negative or positive. In that case we simply return the difference. But if difference is zero, that means the score of both the players are the same. In that case, we have to compare the timestamp and hence we will return p one dot created at timestamp minus p two dot created at timestamp after we have created the comparator with the write compare function, the sorting the list is just calling a single library method. Now let's look at the Java code.
One way to achieve this is by doing passes on the array and picking the next highest every time, which takes order (N*K) time.
Alternatively, the list of players can be sorted by their scores using an order and login-based sorting algorithm and then return the top K players from the sorted list of players.
For large values of k, the order N(log(N)) method is better than the order (N*K) method.
Merge sort and Quicksort are two sorting algorithms with order (N) login time complexity, but Quicksort has a worst-case scenario of order (N) square time complexity.
The standard library function of the collections class in Java uses an enhanced version of merge sort and takes in two arguments: the list to be sorted and a comparator object that provides rules for sorting.
The comparator object in this case compares the score of the players and uses the timestamp as a tiebreaker if the scores are equal.
The pseudo code for the compare function is to calculate the difference between the scores of the two players, and if the difference is not zero, return the difference. If the difference is zero, compare the timestamps and return the result.
Sorting the list is just calling a single library method after creating the comparator with the right compare function.
Great! You saw that you can use the ‘Collections.sort’ function to implement merge sort in our program. This program takes a comparator as an input, which basically sets the rules for this sorting. In case of a tie, you shall use timestamps of the players to assign them to the ranks. Let’s see the Java code for this in the video below.
As you saw in the video, Ankit used timestamps in the occurrence of a tie. Also, if the email ID does not exist in the list, the program prints a message saying that the email ID does not exist.