Lab 7: Complexity and Simple Sorts
CS 241: Data Structures, Fall 2007


Due Date: class on Wednesday Oct 24 (part 1) and Monday Oct 29 (part 2)

Goals for this assignment are to:

Reading

Read Chapter 7 and 8.

Part 1: Written Assignment

We will go over the following in class on Wed, Oct 24. They will not be collected but please be ready to discuss them. Try to complete as much as you can.

  1. Projects 7.19 and 7.20 on page 204.
  2. Complete the table here (pdf).

Part 2: Sorting

  1. Download and unzip the project SortAnimThreaded.

  2. This project contains a program that can animate a number of different sorts. The version you have only implements insertion sort. Run the code to see what it does. In this lab, you are to implement bubble and selection sorts. You will implement the other sorts in later labs. The program also computes the number of comparisons and swaps that each sort uses.

  3. The items to be sorted are integers from zero up to size. The integers are stored in the variable called list which is of type ArrayList<Integer>. Take a look at how insertion sort is implemented. Your task is to implement the run() method contained in the BubbleSort and SelectionSort classes. The run() method is where the sorting is done.

  4. The animation is done via the pause() method which redraws the screen and pauses the process so that you can see what is happening. The pause() method should be called each time items in the list are compared (note, you get different delays if you pause each time items are swapped - which one makes more sense to use?).

  5. The code contains variables called swaps and comparisons. Modify your code so that these are incremented when either a swap or a comparison is performed. The last lines given in sort() prints out the sorted list and the values of swaps and comparisons.

  6. Questions

    No need to turn these in. We will discuss them in class.

    1. What do you expect the worse case, average case, and best case complexity (wrt to the number of comparisons) of each of the sorts in this program?
    2. For the size of the lists in this program (10, 20, 50, 100), do the number of comparisons make sense? Explain.
    3. What happens in your program to the number of comparisons if you sort a list after it is already sorted? Is this what you would expect?

Evaluation

By no later than class on Wednesday, Oct 24, you should do the following:

  1. Be prepared to discuss the tables from the text (7.19 and 7.20) and the pdf table in class.

By no later than class on Monday, Oct 29, you should do the following:

  1. Be prepared to discuss the above Questions in class.
  2. Zip together the entire SortAnimThreaded project and email as an attachment to gorr@willamette.edu. Place CS241 Lab 7 in the subject line.

Demonstrate the SortAnimThreaded code no later than Thursday,Nov 1.


[top]  [Schedule]  [Home]