Lab4 - Sorting Arrays Activity Sheet
Visual Sort Mode
The xSortLab applet can display three different panels: a panel for "Visual Sort," a panel for "Timed Sort," and a "Log" panel. There is a pop-up menu at the top of the applet that can be used to switch among the three panels. (Note: Changing panels while a sort is in progress will abort the sort.)
The applet starts in "Visual Sort" mode, in which 16 bars are sorted step-by-step using one of the sorting algorithms Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, or QuickSort. Below the area where the bars are displayed are two message areas, which display a running commentary when a sort is in progress. The lower message area displays a detailed comment on each step in the sort. The upper area displays more general messages about major phases in the sort. (The lower message area is not used when the sort is being run at "Fast" speed.)
To the right of the bars is a column of controls. The first of these is a pop-up menu, that can be used to select the sorting method. (Again, doing this in the middle of a sort will abort the current sort.) Next comes a checkbox that can be used to determine whether or not the sort is done at "Fast" speed. When this box is not checked, you get to see a cute animation of moving bars; also, a longer delay is inserted between steps when you run the sort with the "Go" button. The "Go" and "Step" buttons are used for executing a sort. The "Start Again" button gives you a new, randomized list of 16 bars.
Two basic operations are used in sorting: comparing two items to see which is bigger and copying an item from one place to another. The number of comparisons and the number of copies used in the current sort are displayed below the controls.
Timed Sort Mode
If you switch to the "Timed Sort" panel, you'll see a large message area, with some instructions. This panel is used to obtain statistics about the running times of various running algorithms. The interesting question is how the running time depends on the number of items being sorted. There is a text-input box at the top of the panel where you can specify the size of the array that is to be sorted. You can also specify the number of arrays to be sorted. The point is that a small array takes so little time to sort that the time cannot be measured accurately. So, you should sort a number of arrays, all of the same size. You can measure the total time it takes to sort them all. The time required to sort one array can be obtained by dividing the total time by the number of arrays. You probably want to adjust the number of arrays so that the total time is at least a couple of seconds.
Note: Your computer must have enough memory to store all the numbers you want to sort. (If you are running the applet at all, you probably have enough memory to work with at least one million items.) If you ask for more numbers than you have room for, you should just a message telling you that you don't have enough memory. However, most systems that I have tried this on have crashed. This is a bug. You are warned. Stick to a reasonable number of items.
At the bottom of the panel are a pop-up menu that you can use to choose the sort method and a "Go" button that you can click to start the sort. (This changes to "Abort" while a sort is in progress.)
When you begin a sort, the first thing the computer does is to fill up the arrays with random numbers. If there are a lot of numbers, this will take a noticeable amount of time. Then, the computer begins to sort. As the sorting operation proceeds, statistics are displayed about twice per second. The statistics include the number of comparison and copy operations that have been performed, the number of arrays that have been sorted so far, the elapsed time since the computer began sorting, and the approximate compute time that the computer has devoted to sorting. The compute time is not the same as the elapsed time, since the applet is doing other things besides sorting (such as redrawing the screen). The applet tries to use 80% of the time for sorting, and to leave 20% for other tasks. The applet can measure the 20% of its time that it gives away voluntarily, but if other things are going on in your computer, it might lose some other time that it can't measure. This is why the measured compute time is approximate. So, you should not try to run a timed sort in the background! Just sit and watch -- or go get a coffee.
The Log
Every time a sort completes successfully, statistics about that sort are written to a log. For a visual sort, the number of copies and the number of comparisons are recorded. For a timed sort, all the statistics that are displayed in the "Timed Sort" panel are written to the log. You can view this log by selecting the "Log" option from the pop-up menu at the top of the applet.
The Log panel has buttons for clearing the log and for saving it to a file. However, it is likely that your browser will not permit you to save the log. Unfortunately, the applet has no provision for printing the log at this time. You might try using copy-and-paste to copy the data from the log to another program from which you can copy or print it.
Watching Bubble Sort
The xSortLab applet has two operating modes: "Visual Sort" and "Timed Sort." There is a pop-up menu at the very top of the applet that can be used to select one of these modes. The pop-up menu also contains an entry for a "Log" which records statistics about sorts that have been performed by the applet. When the applet first starts up, it is in Visual Sort mode, and you will use this mode for the first part of the lab. In this mode, you can watch as the applet sorts sixteen bars into order of increasing height. Later in the lab, you'll be using the Timed Sort mode.
The applet can perform five different sorting algorithms: Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, and QuickSort. When it fist starts up, it is set to use Bubble Sort. There is a pop-up menu near the upper right corner of the applet that you can use to select the sorting method that you want to work with. Below the pop-up menu are three buttons and a checkbox that can be used to control the applet. Click on the "Go" button to let the applet perform the sort automatically, without further intervention from you. Click on the "Step" button to perform just one step in the sort. The "Start Again" button lets you start a new sort, with a randomly arranged set of bars. (The applet will also start a new sort if you select a new sorting method from the pop-up menu.)
All the sorting algorithms that you will look at use two basic operations: compare two items to see which is largest, and copy an item from one place to another. Sorting consists of these two operations performed over and over (plus some "bookkeeping," such as keeping track of which step in the sort the computer is currently performing). Sometimes, the program has to exchange, or swap, two items. It takes three copy operations to perform a swap: First, copy the first item to a special location called "Temp." Second, copy the second item into the first location. And third, copy the item from "Temp" into the first location. As the xSortLab applet performs a sort, it will tell you how may comparison and copy operations it has done so far. This information is displayed in the lower right section of the applet.
Your first task in the lab is to understand Bubble Sort, using the xSortLab applet that you launched above. The basic idea of Bubble Sort is to compare two neighboring items and, if they are in the wrong order, swap them. The computer moves through the list comparing and swapping. At the end of one pass, the largest item will have "bubbled up" to the last position in the list. On the second pass, the next-to-largest item moves into next-to-last position, and so forth. Here is a picture of what the applet will look like when it is partway through the sort. I've added some comments to help you understand what you see:
Use the "Step" and "Go" buttons in the xSortLab Applet to execute Bubble Sort. Check the "Fast" checkbox when you want to speed things up. (This option might also help you to get the broader picture of how the algorithm operates.) You should try stepping slowly through the algorithm with the "Step" button, reading each message in detail. It will also be useful for you to watch the algorithm run itself at "Fast" speed. At this speed, the applet only displays the upper message, which tells you what it is trying to accomplish in each major phase of the algorithm. In Bubble Sort, each major phase moves one item into its final position at the end of the list.
The learn how the Bubble Sort algorithm works, you will have to pay attention to what you see and think about it. You have control over the applet. Use your best judgement about how to proceed. In the exercises at the end of the lab, you will be asked to apply Bubble Sort by hand. You should make sure you understand it well enough to do this.
Selection Sort
Selection Sort is probably the easiest sorting method to understand. In each major phase of the algorithm, the next largest item is found and moved into position at the end of the list. In the picture below, the four black bars have already been moved into position, and the algorithm is in the middle of the next phase. It is important to remember that the program can't just "look at all the bars and pick the biggest one" in one step, as you can. It is restricted to comparing two items at a time. To find the largest item in a list, the computer moves through the list one item at a time, keeping track of the largest item it has seen so far. After looking at all the available items, it knows which is the largest item overall. It moves the largest item into the next available spot at the end of the list by swapping it with the item that is currently occupying that spot. In the picture, during the current phase of the sort, the computer has looked at items number 1 through 7. An arrow labeled "Max" is pointing to item number 2, since that is the largest item that the computer has seen so far during this phase of the sort. The magenta-colored boxes indicate that the computer has just compared items 2 and 7. The next step will be for it to compare items 2 and 8.

Insertion Sort
In Insertion Sort, the basic idea is to take a sorted list and to insert a new item into its proper position in the list. The length of the sorted list grows by one every time a new item is inserted. You can start with a list containing just one item. Then you can insert the remaining items, one at a time, into the list. At the end of this process, all the items have been sorted. In the picture below, the items that are enclosed in a green box have been sorted into increasing size. Each major phase of the sort inserts one new item into this list and increases the size of the box by one. The problem is to determine where in the list the new item should be inserted. You can use the applet to see how it all works.

Exercises
Exercise 1: Suppose that Bubble Sort is applied to the following list of numbers. Show what the list will look like after each phase in the sort:
73 21 15 83 66 7 19 18
Exercise 2: Suppose that Selection Sort is applied to the list of numbers given in Exercise 1. Show what the list will look like after each phase in the sort.
Exercise 3: Suppose that Insertion Sort is applied to the list of numbers given in Exercise 1. Show what the list will look like after each phase in the sort.
Exercise 4: Bubble sort, Insertion sort, and Selection sort are order N2 .You should be able to use data you collected in this lab to compute the value of K in the equation T = K*N2 for each of these sorts. (Recall that N is the array size and that T is the time it takes to sort one array of size N. Also remember that the equation is only approximately true.) Make a table showing the following information for each array size, N, for which you collected data: The array size (N); the number of arrays you sorted; the total computation time to sort the arrays; the computation time for one array (T); and the computed value T/(N*N) which is an approximation for K.
Based on the data in your table, what is your best guess for the actual value of K? Explain your reasoning.
Array size N (Bubble sort) |
Number of Arrays to sort |
Compute time to sort all arrays |
T = Average compute time to sort one array |
K = T/N2 |
| 1000 |
10 |
|
|
|
| 5000 |
10 |
|
|
|
| 10,000 |
10 |
|
|
|
| 15,000 |
10 |
|
|
|
| 20,000 |
10 |
|
|
|
| 25,000 |
10 |
|
|
|
Array size N (Selection sort) |
Number of Arrays to sort |
Compute time to sort all arrays |
T = Average compute time to sort one array |
K = T/N2 |
| 1000 |
10 |
|
|
|
| 5000 |
10 |
|
|
|
| 10,000 |
10 |
|
|
|
| 15,000 |
10 |
|
|
|
| 20,000 |
10 |
|
|
|
| 25,000 |
10 |
|
|
|
Array size N (Insertion sort) |
Number of Arrays to sort |
Compute time to sort all arrays |
T = Average compute time to sort one array |
K = T/N2 |
| 1000 |
10 |
|
|
|
| 5000 |
10 |
|
|
|
| 10,000 |
10 |
|
|
|
| 15,000 |
10 |
|
|
|
| 20,000 |
10 |
|
|
|
| 25,000 |
10 |
|
|
|
Exercise 5: Consider the data that you collected on the compute time of the five sorting algorithms. For arrays of size 10000, how do the algorithms rank according to speed? How about for arrays of size 50000?
|