I have to make a program that sorts a collection of songs by runtime. I have to analyse a selection of songs, each with a “Title” string, a “Composer” string, and a “Running Time” integer. Input will be piped through stdin, and output will be in stdout.
Here’s an example input:
3
&
Pink Frost&Phillipps, Martin&234933
Se quel guerrier io fossi&Puccini, Giacomo&297539
Non piu andrai&Mozart&234933
M'appari tutt'amor&Flotow, F&252905
And output:
Se quel guerrier io fossi&Puccini, Giacomo&297539
M'appari tutt'amor&Flotow, F&252905
Non piu andrai&Mozart&234933
I know I have to sort these by Running Time, but I’m not sure which sorting algorithm to use. By general knowledge, the two sorting algo’s that come to mind are Merge Sort and Quicksort, because they seem to be the quickest on average. I also have the idea of using a Comparator to compare two “Running time” elements in a Collection.
Could someone please point me in the right direction?
Just stick to
compareTo()method for String or int (running tittle) and use them in yourComparators. Nextly – useCollections.sort()which uses merge sort that is quite good 🙂Ah and during runtime you should add those songs to list of songs –
ArrayListorLinkedList. And sort them byCollections.sort(yourListName, new yourComparatorName());