next up previous
Up: APS105F Home

APS 105F - Computer Fundamentals

Assignment #7:  Sorting Arrays and Complexity

Fall 1997

(To be completed on Oct. 22/23)

 

Objective:   To experiment with several sorting algorithms, and measure their running time on different inputs.  
 

1 Input to Sorting Program

In this assignment you will be running various sorting algorithms on the same sets of data, and measuring how much work they do. All programs should behave in the following way:

  1. Read in a set of integers from a file, into a 1-dimensional array.
  2. Sort the array using the algorithm (specified below) into descending order.

Use the sample data that are contained in the directory /usr/copy/aps105 under the file names nums.10, nums.20, nums.50 (and so on) up to nums.5000. The file nums.X contains X data items to be sorted.

To measure the amount of work each program does, you should ``instrument" the program so that it counts the number of times it makes a comparison (i.e. with an IF statement) with any number in the array. That is, the output of each program, besides the sorted array, is the number of comparisons made in the program.

For each program, you should create one (or more) tables that give the number of comparisons for each of the input files nums.10 .. nums.5000 listed above.

2 Bubble Sort

Implement the Bubble Sort algorithm, and create the comparison table for each input file.

3 Quick Sort

Implement the Quick Sort algorithm, and create the comparison table for each input file.

How does the number of comparisons for Quick Sort and Bubble Sort compare as the size of the input grows?

4 Combined Quick and Bubble Sort

Create a combined Quick Sort/Bubble Sort algorithm that works in the following way. This program should read an integer, S, as input. The recursive call in Quick Sort should be changed so that if the size of the sub-range of the array to be sorted is greater than S, then the Quick Sort algorithm is to be used, but if it is less than or equal to S, then the Bubble Sort algorithm should be used.

Create a set of tables that show how the number of comparisons changes as the parameter S changes.

You may wish to using a plotting package, such as xgraph, to graphically show your results. To see how to use xgraph, type ``man xgraph" in a Unix window.

How do your results compare with the original two algorithms? Is there a value of S that consistently provides a better number of comparisons?

Optional-Better Quick Sort

Improve the Quick Sort algorithm so that it uses fewer comparisons for all of the input files. Send mail to pc@eecg.toronto.edu reporting the smallest number of comparisons you can achieve on the file nums.5000. (You are not allowed to do any data-specific optimization such as using knowledge of what the specific data items are in that file).


next up previous
Up: APS105F Home

Paul Chow
Wed Oct 15 23:18:06 EDT 1997