CSc 150

Homework 1 -- Analyzing Time Complexity
Due Wednesday, May 5, 2010

Part 1: Source Code and Algorithm Analysis

For each of the following code fragments or algorithms, determine the worst-case time complexity and express it in Big-O notation in terms of the number of input. You must explain each of your answers. You will only get partial credit if you don't have explanations.
  1. public void one(int n)
    {   
       int a;   
       for (int i=n; i>0; i-=2)
          a=i;
    }
    
  2. public void two(int n, int p)
    {
       int a;
       if (p>0)
       {
         for (int i=0; i<n; i++)
           for (int j=1; j<=n; j++)
              a=i*j;
       }
       else
       {
         for (int i=0; i<n; i++)
           a=i;
       }
    }
    
  3. public void three(int[] myArray)
    {
       int n=myArray.length;
       int count=0;
       do
       {
          count++;
          n=n/2;
       } while (n>1);
       n=myArray.length;
       for (int i=1; i<=count; i++)
         for (int j=0; j<n; j++)
           System.out.print(i*myArray[j]);
    }
    

    Unfamiliar with Java's do-while loop? Check out this tutorial or any number of others just by Googling it.

  4. public void four(int n, int p)
    {
       int a;
       for (int k=0; k<p; k++)
       {
          System.out.println(k);
       }
       for (int i=1; i<=6; i++)
          this.one(n);   // call to method "one" above
    }
    
  5. public void five(int[] A, int[] B)
    {
       int n=A.length;
       int m=B.length;
       for (int i=0; i<m; i++) {
         for (int j=0; j<=i; j++) {
           B[i]=j;
         }
       }
       for (int p=n-1; p>=0; p--) {
         for (int q=p-1; q>=0; q--) {
           A[p]=q;
         }
       }
    }
    

Part 2: Run-time Analysis

For this part, you will be analyzing the time complexity of various sorting routines. However, instead of analyzing the source code, you will be conducting experiments at runtime by measuring how long each routine takes in terms of time and data structure accesses.

Go to our BlackBoard web page and download two files from the Course Material section. The first is the compiled code for six different sorting methods, called Sort.jar. It has the following public interface:

public class Sort
{
   public static int[] method1(int[] A)
   {
      // returns a new sorted array. The array A is not changed.
   }

   public static int[] method2(int[] A)
   {
      // returns a new sorted array. The array A is not changed.
   }

   public static int[] method3(int[] A)
   {
      // returns a new sorted array. The array A is not changed.
   }

   public static int[] method4(int[] A)
   {
      // returns a new sorted array. The array A is not changed.
   }

   public static int[] method5(int[] A)
   {
      // returns a new sorted array. The array A is not changed.
   }

   public static int[] method6(int[] A)
   {
      // returns a new sorted array. The array A is not changed.
   }

   // getMethod<X>Count: returns the number of array accesses
   // that occurred in the last execution of the method.

   public static long getMethod1Count()
   public static long getMethod2Count()
   public static long getMethod3Count()
   public static long getMethod4Count()
   public static long getMethod5Count()
   public static long getMethod6Count()
} // end of class Sort
Note that these methods are all static, which means that you can execute a method just by saying Sort.getMethod1Count().

The second file you need to download is Client.java. This is the source code containing the main() method that runs each of the above sorting methods.

Setup

To use these two files in Eclipse, start a new project. Add in Client.java by dragging the file into the lefthand pane in Eclipse and making sure it ends up in the src folder. To add the Sort.jar file to your project, go to the Project menu and select Properties. Make sure Java Build Path is selected in the lefthand pane. Click on the Libraries tab, and then use the Add External JARs... button to select Sort.jar from your local system. Once included, the main() method should be runnable.

Once your project is runnable, be sure to study the Client.java file to understand what's going on. Notice that there are two ways in which the time complexity of the sorting methods is being measured:

  1. The code is measuring the time in milliseconds of execution of each method (using the currentTimeMillis() method of System)
  2. The code is counting the number of array accesses the method makes.

Your Mission

Your task is to deduce the worst-case time complexity of each of the six methods and express it in Big-O notation. To accomplish this, you need to run the main() method many times using many different arrays of various lengths and containing many different types of numbers. For example, the main() method currently fills the array with random numbers. You could also try identical numbers, a sorted list, and a reverse-sorted list, to name a few. For each type of contents, you should run many experiments with a variety of different array lengths. For each run, keep track of the time complexity measurements that the program outputs.

Using this data, I want you to make two plots for each method:

  1. A plot of the time each of the array content types (random, sorted, etc.) takes (on the vertical axis) against the array length (on the horizontal axis)
  2. A plot of the number of array accesses each of the array content types makes (on the vertical axis) against the array length (on the horizontal axis)
Based on the plots, I want you to conjecture on the worst-case time complexity of each of the six methods and express it in Big-O notation. Be sure to include a brief explanation supporting each of your conjectures. Also, for each method, discuss possible reasons for any differences you find between the two plots for the method. You must do your plots electronically (not by hand.) Microsoft Excel did a pretty good job when I tried it.

Notes:

Gentle Reminder

Homeworks, like programming assignments, are individual projects. I encourage you to talk to others about the general nature of the homework and ideas about how to pursue it. However, the technical work, the writing, and the inspiration behind these must be substantially your own. If any person besides you contributes in any way to the project, you must credit their work on your homework. Similarly, if you include information that you have gleaned from other published sources, you must cite them as references. Looking at, and/or copying, other people's programs or written work is inappropriate, and will be considered cheating.
Return to Homework Index