I am using a Heap sort algorithm to sort an Object List and that is working fine. The problem is when I am trying to get the top k results of the list using this code:
HeapSort.sort(songs);
List<Song> highk = songs.subList(songs.size() - k, songs.size());
System.out.println(highk);
This prints the last k elements of the list. The k is fine because even if I change it with a number I have the same problem.
If you want top k elements, then your method call should be like below:
List<Song> highk = songs.subList(0, k);
When you see the java doc comments of the subList method, it clearly says, first argument is fromIndex and second argument is toIndex (here).