Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package nl.ru.ai.exercise1;
- //
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Exercise1
- {
- private static final String DATABASE_FILENAME="songs.txt";
- public static int counter=0;
- /*
- * Here we go
- */
- public static void main(String[] args)
- {
- try
- {
- ArrayList<Track> list=new ArrayList<Track>();
- System.out.println(list);
- /*
- * Read database
- */
- readDatabase(list);
- System.out.printf("%d songs read from datatabase '%s'\n",list.size(),DATABASE_FILENAME);
- /*
- * Ask for sorting method
- */
- Scanner input=new Scanner(System.in);
- SortingMethod method=askForSortingMethod(input);
- /*
- * Sort
- */
- System.out.printf("Sorting with %s\n",method);
- switch(method)
- {
- case BUBBLE_SORT:
- bubbleSort(list);
- break;
- case INSERTION_SORT:
- insertionSort(list);
- break;
- case SELECTION_SORT:
- selectionSort(list);
- break;
- }
- System.out.println("Sorted!");
- /*
- * Show result
- */
- dumpDatabase(list, counter);
- }
- catch(FileNotFoundException exception)
- {
- System.out.printf("Error opening database file '%s': file not found\n",DATABASE_FILENAME);
- }
- }
- private static void dumpDatabase(ArrayList<Track> list, int counter)
- {
- for(int i=0;i<list.size();i++)
- {
- System.out.printf("%-26s %-32s %4d %s\n",list.get(i).artist,list.get(i).cd,list.get(i).track, list.get(i).time);
- System.out.println(counter);
- }
- }
- /**
- * Ask the user for a sorting method
- * @param input scanner used for asking
- * @return sorting method
- */
- private static SortingMethod askForSortingMethod(Scanner input)
- {
- /*
- * Show possible sorting methods
- */
- for(SortingMethod method : SortingMethod.values())
- System.out.printf("%d : %s\n",method.ordinal(),method);
- /*
- * Loop until valid choice
- */
- SortingMethod choice=null;
- while(choice==null)
- {
- System.out.println("Enter choice: ");
- int selection=input.nextInt();
- if(selection>=0&&selection<SortingMethod.values().length)
- choice=SortingMethod.values()[selection];
- else
- System.out.println("Invalid choice, try again!");
- }
- return choice;
- }
- /**
- * Reads the cd database from the file 'songs.txt' into the specified track array
- * @param database this is the database that will be filled with the input.
- * @return number of tracks read
- * @throws FileNotFoundException
- */
- static int readDatabase(ArrayList<Track> list) throws FileNotFoundException
- {
- FileInputStream inputStream=new FileInputStream(DATABASE_FILENAME);
- Scanner scanner=new Scanner(inputStream);
- int numberOfTracks=0;
- while(scanner.hasNext())
- {
- Track track=new Track();
- track.artist=scanner.nextLine();
- track.cd=scanner.nextLine();
- track.year=scanner.nextInt();
- scanner.nextLine();
- track.track=scanner.nextInt();
- scanner.nextLine();
- track.title=scanner.nextLine();
- track.tags=scanner.nextLine();
- track.time=new Length(scanner.nextLine());
- track.country=scanner.nextLine();
- list.add(track);
- numberOfTracks++;
- }
- scanner.close();
- return numberOfTracks;
- }
- /*************** Auxiliary array routines from lecture ***************/
- /**
- * Checks if the slice of the specified array is sorted
- * @param array
- * @param slice
- * @return true if the slice of the array is in ascending order, false otherwise
- */
- static <T extends Comparable<T>> boolean isSorted(ArrayList<T> list, Slice slice)
- {
- assert list!=null : "ArrayList should be initialized";
- assert slice.isValid() : "Slice should be valid";
- for(int i=slice.from;i<slice.upto-1;i++)
- if(list.get(i).compareTo(list.get(i+1))>0)
- return false;
- return true;
- }
- /**
- * Find position in array slice where to insert new element
- * @param array
- * @param slice
- * @param y element for which the position should be returned
- * @return position where to insert
- */
- static <T extends Comparable<T>> int findInsertPosition(ArrayList<T> list, Slice slice, T y)
- {
- assert list!=null : "ArrayList should be initialized";
- assert slice.isValid() : "Slice should be valid";
- assert isSorted(list,slice);
- for(int i=slice.from;i<slice.upto;i++)
- if(list.get(i).compareTo(y)>=0)
- return i;
- return slice.upto;
- }
- /**
- * Shifts all elements in the slice one position to the right
- * @param array
- * @param slice
- */
- static <T extends Comparable<T>> void shiftRight(ArrayList<T> list, Slice slice)
- {
- assert list!=null : "ArrayList should be initialized";
- assert slice.isValid()&&slice.from<list.size() : "Slice should be valid";
- for(int i=slice.upto;i>slice.from;i--)
- list.set(i,list.get(i-1));
- }
- /**
- * Insert an element to a sorted array and keep it sorted
- * @param array
- * @param length length
- * @param y element to be added
- * @return new length
- */
- static <T extends Comparable<T>> int insert(ArrayList<T> list, int i, T y)
- {
- assert list!=null : "ArrayList should be initialized";
- assert list.size()>=0 : "Length cannot be negative";
- assert isSorted(list,new Slice(0,i)) : "Array should be sorted";
- int position=findInsertPosition(list,new Slice(0,i),y);
- shiftRight(list,new Slice(position,i));
- list.set(position,y);
- return i+1;
- }
- /**
- * Swap two elements in an array
- * @param array
- * @param i
- * @param j
- */
- private static <T extends Comparable<T>> void swap(ArrayList<T> list, int i, int j)
- {
- assert list!=null : "ArrayList should be initialized";
- assert i>=0&&i<list.size() : "First index is invalid";
- assert j>=0&&j<list.size() : "Second index is invalid";
- T help=list.get(i);
- list.set(i,list.get(j));
- list.set(j,help);
- }
- /*************** Array based Sorting routines from lecture ***************/
- /**
- * Sorts an array in situ in ascending order using selection sort
- * @param array
- * @oaram length
- */
- static <T extends Comparable<T>> void selectionSort(ArrayList<T> list)
- {
- assert list!=null : "arrayList should be initialized";
- for(int i=0;i<list.size();i++)
- {
- int j=indexOfSmallestValue(list,new Slice(i,list.size()));
- swap(list,i,j);
- }
- }
- /**
- * Finds index of smallest value in array slice
- * @param array
- * @param slice
- * @return index of smallest value
- */
- static <T extends Comparable<T>> int indexOfSmallestValue(ArrayList<T> list, Slice slice)
- {
- assert list!=null : "ArrayList should be initialized";
- assert slice.isValid()&&slice.upto<=list.size() : "Slice should be valid";
- assert slice.upto-slice.from>0 : "Slice should be non-empty";
- int index=slice.from;
- for(int i=slice.from+1;i<slice.upto;i++)
- if(list.get(i).compareTo(list.get(index))<0)
- index=i;
- return index;
- }
- /**
- * Sorts an array in situ in ascending order using bubble sort
- * @param array
- * @param length
- */
- static <T extends Comparable<T>> void bubbleSort(ArrayList<T> list)
- {
- assert list!=null : "arrayList should be initialized";
- int length=list.size();
- while(!bubble(list,new Slice(0,length)))
- length--;
- }
- /**
- * Swap all adjacent pairs in the array slice that are not in the right order
- * @param list
- * @param slice
- * @return array slice is sorted
- */
- static <T extends Comparable<T>> boolean bubble(ArrayList<T> list, Slice slice)
- {
- assert list!=null : "Array should be initialized";
- assert slice.isValid()&&slice.upto<=list.size() : "Slice should be valid";
- boolean isSorted=true;
- for(int i=slice.from;i<slice.upto-1;i++)
- if(list.get(i).compareTo(list.get(i+1))>0)
- {
- swap(list,i,i+1);
- isSorted=false;
- }
- return isSorted;
- }
- /**
- * Sorts an array in situ in ascending order using insertion sort
- * @param array
- * @param length
- *
- */
- static <T extends Comparable<T>> void insertionSort(ArrayList<T> list)
- {
- assert list!=null : "arrayList should be initialized";
- for(int i=0;i<list.size();i++)
- insert(list,i,list.get(i));
- }
- }
Add Comment
Please, Sign In to add comment