CMPU 102: Lab 6

CMPU 102 Lab 6: Testing efficiency of sorting algorithms experimentally

This lab gives you some experience measuring the running time of algorithms.

Getting started

Download http://www.cs.vassar.edu/~cs102/assign/lab6.zip and import it into your Eclipse workspace.

Part 1:

Implement the method genRanArray in the Sorts class so that that it takes 2 integers, n and m, as input and returns an unsorted array of m (pseudo) random integers between 1 and n. Make sure there are no duplicate numbers in the container (therefore, make sure that n > m).

Part 2

In the same class as the genRanArray method you wrote for part 1, write a main method to do the following:

  1. Read integers for size and range from the command line.
  2. Call the genRanArray method to generate an array of size unique random numbers between 0 and range (i.e., your method from part 1). Warning: make sure the size of the array is less than the range or your method will never terminate. Use timing statements (described below) around the call to the genRanArray method to measure the amount of time your program takes to generate the array of unique numbers. Make sure this part of the program works before you continue.
  3. Run each of the four sorting algorithms mentioned in Part 2 on the same unsorted array generated in step 1.
  4. Use timing statements to determine the system time used by each sorting algorithm on the array generated in step 1 and print the running time for each algorithm.


Timing in Java

Like most high-level languages, Java provides a method to measure the running time of a program. The call to this method is as follows:

               System.currentTimeMillis();

The currentTimeMillis method returns an integer of type long, representing the current system time in milliseconds.

Time a particular statement by calling it in between two calls to System.currentTimeMillis() and then subtracting the difference between the times returned by those two calls.


Running the program

Prepare a terminal window for running the program by executing the following commands (typing return after each one):

     cd
     cd eclipse-workspace/lab6/bin
     setenv CLASSPATH .:$CLASSPATH

Run the program from the terminal window as follows:

      java edu.vassar.cs.cs102.sorts.Sorts 20000 1000000
Warning: make sure the size of the array, m, is less than the range n, or your program will never terminate.

Your output should be as follows (user input bolded):

      Generating 20000 unique random numbers took 12600 ms
      Insertion sort took 4293 ms
      Shell sort took 1826 ms
      Merge sort took 33 ms
      Quick sort took 29 ms
Test your program on large enough data sets to give you an idea of how the running time of each program relates to the size of the data set (warning: generating too large a data set will take a very long time). Recommended maximum size = 20000.

Run your program for your lab instructor when you get it working.

Submit your lab6 directory when you are finished and have tested your program: from a terminal window

     cd
     cd eclipse-workspace
     submit102 lab6