Computer Science 210 Lab 3: Complexity and Sorting
Due: September 23, 1997

Objectives and Overview: This lab provides a chance to become familiar with basic ideas of "computational complexity," which is a way of measuring the efficiency of programs. What makes a particular program the most efficient solution to a given problem, and how do we know how efficient it is? The sorting problem provides a good vehicle for exploring the complexity of alternative solutions. That is, find a program for rearranging the numbers in a list into ascending order, and then measure (estimate) the running time for your program. Is there a faster program, using a different approach to sorting? Chapter 4 in your text introduces tools for measuring the complexity of programs, while Chapter 5 presents several different sorting programs and discusses their complexity.

Part 1 - Answer Some Questions about Complexity and Recursion

To be sure that you understand the basic ideas of computational complexity and recursion, answer the following questions in Chapter 4 of your text.

  1. Question 4.1 on page 65
  2. Question 4.5 on page 66
  3. Question 4.12
  4. Question 4.14
  5. Question 4.15

Part 2 - Exercise the sortDriver Program

Now start CodeWarrior and create a new project called Lab3.µ on the desktop. Add the sortDriver.java program file to your project, along with the two additional files shown below. These files can be found in the Courses -> CS210 (Tucker) -> examples folder; copies of all these files should be dragged to the desktop before adding them to your project.

The sortDriver program is a utility program to exercise any of the sorting algorithms that are presented in Chapter 5 of your text. As it stands, it is set up to exercise the bubbleSort method (see p 68 of your text). The program calls for an integer n and then generates n random integers in the range 0-999. After calling bubbleSort, the program displays the sorted list and the elapsed time in milliseconds to accomplish this sort.

A sample of the output for this program is shown below:

To run this program, recall that the following actions need to be taken with your project Lab3.µ. Select TrivialApplication Settings under the Edit menu, and a settings window will appear. Now make three changes in this window:

  • Select Java Target and type "sortDriver" as the Main Class. This is the class that will execute when your program is run.
  • Change Target Settings -> Target Name to "sortDriver".
  • Change Linker Output -> Type to "Droplet" from its current setting.
  • Now close this window and Make and Run your project. The output should appear in the Java Output window after you type a number in the System.in window. For example, if you type the input

    10

    the program will give output similar to that shown above (the millisecond timing number may be different). Now run this program and check that it is working properly.

    Part 3- Measure the Running Time for Two Sorting Algorithms

    Now run this program several (12) times, recording the elapsed time in milliseconds for each run.

  • First run the bubbleSort method for each of the following input sizes: 100, 500, 600, 800, 1000, 2000.
  • Next run the mergeSort method for each of these six different input sizes.
  • This exercise provides a way to empirically measure the complexity of different sorting algorithms.

    1. What are the running times for each of the 12 runs you made?
    2. How does the running time of the bubble sort seem to grow as the size of the input grows?
    3. How does the running time of the merge sort seem to grow?
    4. Which seems to be the more efficient sort for large values of n? How does this conclusion compare with that drawn from examining the theoretical complexities of the bubble sort and merge sort algorithms, as discussed in the text.
    5. How would you design an experiment that measures the "best case" performance of the bubble sort?
    6. Answer question 5.10 on page 85 of your text.

    Lab 3 Deliverables:

    Hand in your answers to the questions in Parts 1 and 3 of this lab. Also, hand in a hard copy of the output from a single run of the sortDriver program, for input size n = 100.