import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; // Bucket sort public class FantanoSort { static class Album { String name; int score; Album(String n, int s) { name = n; score = s; } } static Album[] readAlbums() { Album[] albums = new Album[3123]; String filename = "scores.txt"; try { Scanner sc = new Scanner(new File(filename)); int i = 0; while (sc.hasNext()) { String[] line = sc.nextLine().split("\t"); albums[i] = new Album(line[1], Integer.parseInt(line[0])); i++; } sc.close(); } catch (IOException e) { System.exit(1); } return albums; } @SuppressWarnings("unchecked") static Album[] fantanoSort(Album[] albums) { ArrayList[] buckets = new ArrayList[11]; for (int i = 0; i < 11; i++) { buckets[i] = new ArrayList<>(); } // Legger til alle album i riktig b?tte (score svarer til indeks) for (Album a : albums) { int score = a.score; // N?kkelen til et album buckets[score].add(a); // Legger til i riktig b?tte } // G?r gjennom alle b?ttene og stapper albumene tebake i arrayet i rekkef?lgen de itereres over int j = 0; for (int i = 0; i < buckets.length; i++) { for (Album a : buckets[i]) { albums[j] = a; j++; } } return albums; } public static void main(String[] args) { Album[] albums = readAlbums(); albums = fantanoSort(albums); for (Album a : albums) { System.out.println(a.name + " (" + a.score + "/10)"); } } }