Assignment #3

Due: Thursday, December 19 (revised)
Complete the following program:

import java.util.*;
class Assignment3
{
   /* These methods should each do the appropriate
   sort on the String array list,  which contains listSize
   Strings.  The method should return the time the sort takes
   in seconds, accurate to the nearest millisecond. 
   Convert all the letters to uppercase before sorting.*/
   public static double insertSort(String[] list, int listSize)
   public static double selectSort(String[] list, int listSize)
   public static double shellsort(String[] list, int listSize)
   /* This method prints up to max items from the list.
   Print each word separated by a space, with as many
   words on each line as possible (up to 80 characters per line)*/
   public static void printList(String[] list, int listSize, int max)
   /* Read all the words from a file and store them in
   list.  Return the number of words read.  Strip all
   punctuation from the words (other than the
   apostrophe).  Return 0 if the file doesn't exist*/
   public static int readList(String[] list, String fileName)
   
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);
      String[] words = new String[1500000];
      String[] original = new String[1500000];
      System.out.println("Enter file name");
      String name = input.nextLine();
      String choice;
      int numWords = readList(words, name);
      double insertTime = 0, selectTime = 0, shellTime = 0;

      if (numWords !=0)
      {
         System.arraycopy(words,0, original, 0, words.length);
         System.out.println("Enter 1 for Insertion Sort");
         System.out.println("Enter 2 for Selection Sort");
         System.out.println("Enter 3 for Shellsort");
         System.out.println("Enter 4 to do all 3 sorts");
         choice = input.nextLine();
         if (choice.charAt(0) == '1' || choice.charAt(0) == '4')
         {  
            System.out.println("Here is the list (max 2000 words) after Insertion sort:");
            insertTime = insertSort(words, numWords);
            printList(words, numWords, 2000);
            System.arraycopy(original,0, words, 0, words.length);
            System.out.println("***************************");
         }
         if (choice.charAt(0) == '2' || choice.charAt(0) == '4')
         {
            System.out.println("Here is the list (max 3000 words) after Selection sort:");
            selectTime = selectSort(words, numWords);
            printList(words, numWords, 3000);
            System.arraycopy(original,0, words, 0, words.length);
            System.out.println("***************************");
         }
         if (choice.charAt(0) == '3' || choice.charAt(0) == '4')
         {
            System.out.println("Here is the list (max 4000 words) after Shellsort:");
            shellTime = shellsort(words, numWords);
            printList(words, numWords, 4000);
            System.out.println("***************************");
         }

         System.out.println("Read " + numWords + " words in " + name + ".");
         if (choice.charAt(0) == '1' || choice.charAt(0) == '4')
            System.out.println("It took " + insertTime + " s to sort the words using insertion sort");
         if (choice.charAt(0) == '2' || choice.charAt(0) == '4')
            System.out.println("It took " + selectTime + " s to sort the words using selection sort");
         if (choice.charAt(0) == '3' || choice.charAt(0) == '4')
            System.out.println("It took " + shellTime + " s to sort the words using Shellsort");
      }
      else
         System.out.println("No such file exists.");
   }
      
} 

Suggestions:

Marking Scheme

____
 23
Insertion sort (2) 
Selection sort (2) 
shellsort (2) 
printList (3) 
readList (3) 
results table (3) 
predictions table (3) 
comments (opening, variable, inline) (3) 
indentation (2) 

Summarizing Results:

You need to complete the following table listing the times required to sort various size files using different sorts. You must use the school computers to get these times. Give yourself a couple of classes to get these times.

  File Source     Range of WordsActual # of wordsInsertion SortSelection SortShellsort
 200-1000    
 50,000-100,000    
 100,000-300,000    
 300,000-500,000  
 > 1 million  

Note from the table you do not need to do insertion or selection sort on files larger than 300,000 words (and I would recommend you choose something close to 100,000 and not much more than 150,000 for the one in the 100,000 to 300,000 range.

From the results that you have, predict how long insertion and selection would take for the two larger files.

Range of WordsActual # of wordsPrediction for InsertionPrediction for Selection
300,000-500,000   
> 1 million   
Show your work here (attach a sheet if necessary):