SHOW:
|
|
- or go back to the newest paste.
| 1 | package nl.ru.ai.exercise1; | |
| 2 | // | |
| 3 | import java.io.FileInputStream; | |
| 4 | import java.io.FileNotFoundException; | |
| 5 | import java.util.ArrayList; | |
| 6 | import java.util.Scanner; | |
| 7 | ||
| 8 | ||
| 9 | public class Exercise1 | |
| 10 | {
| |
| 11 | private static final String DATABASE_FILENAME="songs.txt"; | |
| 12 | public static int counter=0; | |
| 13 | ||
| 14 | /* | |
| 15 | * Here we go | |
| 16 | */ | |
| 17 | public static void main(String[] args) | |
| 18 | {
| |
| 19 | try | |
| 20 | {
| |
| 21 | ||
| 22 | ArrayList<Track> list=new ArrayList<Track>(); | |
| 23 | ||
| 24 | System.out.println(list); | |
| 25 | /* | |
| 26 | * Read database | |
| 27 | */ | |
| 28 | readDatabase(list); | |
| 29 | System.out.printf("%d songs read from datatabase '%s'\n",list.size(),DATABASE_FILENAME);
| |
| 30 | /* | |
| 31 | * Ask for sorting method | |
| 32 | */ | |
| 33 | Scanner input=new Scanner(System.in); | |
| 34 | SortingMethod method=askForSortingMethod(input); | |
| 35 | /* | |
| 36 | * Sort | |
| 37 | */ | |
| 38 | System.out.printf("Sorting with %s\n",method);
| |
| 39 | switch(method) | |
| 40 | {
| |
| 41 | case BUBBLE_SORT: | |
| 42 | bubbleSort(list); | |
| 43 | break; | |
| 44 | case INSERTION_SORT: | |
| 45 | insertionSort(list); | |
| 46 | break; | |
| 47 | case SELECTION_SORT: | |
| 48 | selectionSort(list); | |
| 49 | break; | |
| 50 | } | |
| 51 | System.out.println("Sorted!");
| |
| 52 | /* | |
| 53 | * Show result | |
| 54 | */ | |
| 55 | dumpDatabase(list, counter); | |
| 56 | } | |
| 57 | catch(FileNotFoundException exception) | |
| 58 | {
| |
| 59 | System.out.printf("Error opening database file '%s': file not found\n",DATABASE_FILENAME);
| |
| 60 | } | |
| 61 | } | |
| 62 | private static void dumpDatabase(ArrayList<Track> list, int counter) | |
| 63 | {
| |
| 64 | for(int i=0;i<list.size();i++) | |
| 65 | {
| |
| 66 | System.out.printf("%-26s %-32s %4d %s\n",list.get(i).artist,list.get(i).cd,list.get(i).track, list.get(i).time);
| |
| 67 | System.out.println(counter); | |
| 68 | } | |
| 69 | } | |
| 70 | /** | |
| 71 | * Ask the user for a sorting method | |
| 72 | * @param input scanner used for asking | |
| 73 | * @return sorting method | |
| 74 | */ | |
| 75 | private static SortingMethod askForSortingMethod(Scanner input) | |
| 76 | {
| |
| 77 | /* | |
| 78 | * Show possible sorting methods | |
| 79 | */ | |
| 80 | for(SortingMethod method : SortingMethod.values()) | |
| 81 | System.out.printf("%d : %s\n",method.ordinal(),method);
| |
| 82 | /* | |
| 83 | * Loop until valid choice | |
| 84 | */ | |
| 85 | SortingMethod choice=null; | |
| 86 | while(choice==null) | |
| 87 | {
| |
| 88 | System.out.println("Enter choice: ");
| |
| 89 | int selection=input.nextInt(); | |
| 90 | if(selection>=0&&selection<SortingMethod.values().length) | |
| 91 | choice=SortingMethod.values()[selection]; | |
| 92 | else | |
| 93 | System.out.println("Invalid choice, try again!");
| |
| 94 | } | |
| 95 | return choice; | |
| 96 | } | |
| 97 | /** | |
| 98 | * Reads the cd database from the file 'songs.txt' into the specified track array | |
| 99 | * @param database this is the database that will be filled with the input. | |
| 100 | * @return number of tracks read | |
| 101 | * @throws FileNotFoundException | |
| 102 | */ | |
| 103 | static int readDatabase(ArrayList<Track> list) throws FileNotFoundException | |
| 104 | {
| |
| 105 | FileInputStream inputStream=new FileInputStream(DATABASE_FILENAME); | |
| 106 | Scanner scanner=new Scanner(inputStream); | |
| 107 | int numberOfTracks=0; | |
| 108 | while(scanner.hasNext()) | |
| 109 | {
| |
| 110 | Track track=new Track(); | |
| 111 | track.artist=scanner.nextLine(); | |
| 112 | track.cd=scanner.nextLine(); | |
| 113 | track.year=scanner.nextInt(); | |
| 114 | scanner.nextLine(); | |
| 115 | track.track=scanner.nextInt(); | |
| 116 | scanner.nextLine(); | |
| 117 | track.title=scanner.nextLine(); | |
| 118 | track.tags=scanner.nextLine(); | |
| 119 | track.time=new Length(scanner.nextLine()); | |
| 120 | track.country=scanner.nextLine(); | |
| 121 | list.add(track); | |
| 122 | numberOfTracks++; | |
| 123 | } | |
| 124 | scanner.close(); | |
| 125 | return numberOfTracks; | |
| 126 | } | |
| 127 | /*************** Auxiliary array routines from lecture ***************/ | |
| 128 | /** | |
| 129 | * Checks if the slice of the specified array is sorted | |
| 130 | * @param array | |
| 131 | * @param slice | |
| 132 | * @return true if the slice of the array is in ascending order, false otherwise | |
| 133 | */ | |
| 134 | static <T extends Comparable<T>> boolean isSorted(ArrayList<T> list, Slice slice) | |
| 135 | {
| |
| 136 | assert list!=null : "ArrayList should be initialized"; | |
| 137 | assert slice.isValid() : "Slice should be valid"; | |
| 138 | for(int i=slice.from;i<slice.upto-1;i++) | |
| 139 | if(list.get(i).compareTo(list.get(i+1))>0) | |
| 140 | return false; | |
| 141 | return true; | |
| 142 | } | |
| 143 | /** | |
| 144 | * Find position in array slice where to insert new element | |
| 145 | * @param array | |
| 146 | * @param slice | |
| 147 | * @param y element for which the position should be returned | |
| 148 | * @return position where to insert | |
| 149 | */ | |
| 150 | static <T extends Comparable<T>> int findInsertPosition(ArrayList<T> list, Slice slice, T y) | |
| 151 | {
| |
| 152 | assert list!=null : "ArrayList should be initialized"; | |
| 153 | assert slice.isValid() : "Slice should be valid"; | |
| 154 | assert isSorted(list,slice); | |
| 155 | for(int i=slice.from;i<slice.upto;i++) | |
| 156 | if(list.get(i).compareTo(y)>=0) | |
| 157 | return i; | |
| 158 | return slice.upto; | |
| 159 | } | |
| 160 | /** | |
| 161 | * Shifts all elements in the slice one position to the right | |
| 162 | * @param array | |
| 163 | * @param slice | |
| 164 | */ | |
| 165 | static <T extends Comparable<T>> void shiftRight(ArrayList<T> list, Slice slice) | |
| 166 | {
| |
| 167 | assert list!=null : "ArrayList should be initialized"; | |
| 168 | assert slice.isValid()&&slice.from<list.size() : "Slice should be valid"; | |
| 169 | for(int i=slice.upto;i>slice.from;i--) | |
| 170 | list.set(i,list.get(i-1)); | |
| 171 | } | |
| 172 | /** | |
| 173 | * Insert an element to a sorted array and keep it sorted | |
| 174 | * @param array | |
| 175 | * @param length length | |
| 176 | * @param y element to be added | |
| 177 | * @return new length | |
| 178 | */ | |
| 179 | static <T extends Comparable<T>> int insert(ArrayList<T> list, int i, T y) | |
| 180 | {
| |
| 181 | assert list!=null : "ArrayList should be initialized"; | |
| 182 | assert list.size()>=0 : "Length cannot be negative"; | |
| 183 | assert isSorted(list,new Slice(0,i)) : "Array should be sorted"; | |
| 184 | int position=findInsertPosition(list,new Slice(0,i),y); | |
| 185 | shiftRight(list,new Slice(position,i)); | |
| 186 | list.set(position,y); | |
| 187 | return i+1; | |
| 188 | } | |
| 189 | /** | |
| 190 | * Swap two elements in an array | |
| 191 | * @param array | |
| 192 | * @param i | |
| 193 | * @param j | |
| 194 | */ | |
| 195 | private static <T extends Comparable<T>> void swap(ArrayList<T> list, int i, int j) | |
| 196 | {
| |
| 197 | assert list!=null : "ArrayList should be initialized"; | |
| 198 | assert i>=0&&i<list.size() : "First index is invalid"; | |
| 199 | assert j>=0&&j<list.size() : "Second index is invalid"; | |
| 200 | T help=list.get(i); | |
| 201 | list.set(i,list.get(j)); | |
| 202 | list.set(j,help); | |
| 203 | } | |
| 204 | /*************** Array based Sorting routines from lecture ***************/ | |
| 205 | /** | |
| 206 | * Sorts an array in situ in ascending order using selection sort | |
| 207 | * @param array | |
| 208 | * @oaram length | |
| 209 | */ | |
| 210 | static <T extends Comparable<T>> void selectionSort(ArrayList<T> list) | |
| 211 | {
| |
| 212 | assert list!=null : "arrayList should be initialized"; | |
| 213 | for(int i=0;i<list.size();i++) | |
| 214 | {
| |
| 215 | int j=indexOfSmallestValue(list,new Slice(i,list.size())); | |
| 216 | swap(list,i,j); | |
| 217 | } | |
| 218 | } | |
| 219 | /** | |
| 220 | * Finds index of smallest value in array slice | |
| 221 | * @param array | |
| 222 | * @param slice | |
| 223 | * @return index of smallest value | |
| 224 | */ | |
| 225 | static <T extends Comparable<T>> int indexOfSmallestValue(ArrayList<T> list, Slice slice) | |
| 226 | {
| |
| 227 | assert list!=null : "ArrayList should be initialized"; | |
| 228 | assert slice.isValid()&&slice.upto<=list.size() : "Slice should be valid"; | |
| 229 | assert slice.upto-slice.from>0 : "Slice should be non-empty"; | |
| 230 | int index=slice.from; | |
| 231 | for(int i=slice.from+1;i<slice.upto;i++) | |
| 232 | if(list.get(i).compareTo(list.get(index))<0) | |
| 233 | index=i; | |
| 234 | return index; | |
| 235 | } | |
| 236 | /** | |
| 237 | * Sorts an array in situ in ascending order using bubble sort | |
| 238 | * @param array | |
| 239 | * @param length | |
| 240 | */ | |
| 241 | static <T extends Comparable<T>> void bubbleSort(ArrayList<T> list) | |
| 242 | {
| |
| 243 | assert list!=null : "arrayList should be initialized"; | |
| 244 | int length=list.size(); | |
| 245 | while(!bubble(list,new Slice(0,length))) | |
| 246 | length--; | |
| 247 | ||
| 248 | } | |
| 249 | /** | |
| 250 | * Swap all adjacent pairs in the array slice that are not in the right order | |
| 251 | * @param list | |
| 252 | * @param slice | |
| 253 | * @return array slice is sorted | |
| 254 | */ | |
| 255 | static <T extends Comparable<T>> boolean bubble(ArrayList<T> list, Slice slice) | |
| 256 | {
| |
| 257 | assert list!=null : "Array should be initialized"; | |
| 258 | assert slice.isValid()&&slice.upto<=list.size() : "Slice should be valid"; | |
| 259 | boolean isSorted=true; | |
| 260 | for(int i=slice.from;i<slice.upto-1;i++) | |
| 261 | if(list.get(i).compareTo(list.get(i+1))>0) | |
| 262 | {
| |
| 263 | swap(list,i,i+1); | |
| 264 | isSorted=false; | |
| 265 | } | |
| 266 | return isSorted; | |
| 267 | } | |
| 268 | /** | |
| 269 | * Sorts an array in situ in ascending order using insertion sort | |
| 270 | * @param array | |
| 271 | * @param length | |
| 272 | * | |
| 273 | */ | |
| 274 | static <T extends Comparable<T>> void insertionSort(ArrayList<T> list) | |
| 275 | {
| |
| 276 | assert list!=null : "arrayList should be initialized"; | |
| 277 | for(int i=0;i<list.size();i++) | |
| 278 | insert(list,i,list.get(i)); | |
| 279 | } | |
| 280 | } |