Lab1: Sorting

This lab has a variety of goals: To refresh your skills in programming generally, to introduce you to C (including the C compiler), to introduce the topic of sorting, and to start you thinking about how to measure and compare the performance of programs.

In this lab you will write two sorting algorithms and run an experiment to measure one aspect of their performance.

This lab description assumes that you know and remember the essentials of three sorting algorithms: insertion sort, selection sort, and bubble sort. If you need a refresher on any of these, you can find descriptions of them at this old CSCI 235 project description.

1. Set up

Make a directory for this lab and move into it.

mkdir lab1
cd lab1

As in most labs and projects, I am giving you some code base to work on. Copy the following files from the course directory for this lab.

cp /homes/tvandrun/Public/cs245/lab1/* .

This gives you seven files:

The last two of these are the only files that you will need to modify.

Inspect the makefile. I'm not having you write your own makefile today, but you will have to write one in the future, so review its format and the logic of the dependencies.

Open sorts.c in xemacs or gedit.

3. Insertion sort

The first sorting algorithm, insertion sort, is given to you. Read through it carefully to make sure you understand how it works. One part is missing, however. Even though a variable compars is declared and its value returned, we never actually increment it based on the comparisons.

Modify this so that compars is incremented whenever we compare two data from the array. Expressions like i < n don't count because they don't compare items in the array.

Compile using make and test using ./sDriver. Comparisons will range from around 15 to about 40. If you're not getting in the high 30s some of the time, then you're missing some.

2. Selection sort

Now implement selection sort in the function selectionSort(). As the algorithm progresses, keep track of comparisons in the variable compars. Then compile and test the revised selection sort. Obviously you first need to make sure the sorting is correct. Then make sure you're testing for comparisons correctly. If you are not getting between 40 and 50 comparisons, then you're missing some.

4. Bubble sort

You have probably seen another sorting algorithm called bubble sort. While not a very good sort in terms of efficiency, it is easy to program and understand. This algorithm's strategy is to iterate through the array, swapping adjacent values that are out of order.

Clearly one pass through the array of this sort of swapping won't sort the array. Many passes are necessary to put all the elements in the right order. There are two ways to monitor repeated passes: First, one could keep track of whether any changes were made to the array (whether any actual swaps happened) on the current pass; if a pass completes without any swaps, then the array is sorted and we can quit. Second, we can observe the fact that after the first pass through the array, the largest element has made it all the way to the end, and so the next pass can stop one element short; the second pass will put the second largest element in the right place, and so the third pass doesn't need to examine the last two positions; an outer loop, therefore, can count down the ending point of the potentially unsorted portion of the array until that portion is empty.

Implement a version of bubble sort and make sure it sorts correctly.

4. Experiment

Now we want to run an experiment to determine how these algorithms vary in terms of the comparisons they require. For this you will write a program that runs the experiment.

Open the file experiment.c. Write opening documentation like what we have seen, giving your name and the occasion (lab 1).

In this experiment, we ask two questions:

We can easily address both of these questions in the same experiment. Write a program that for each of 5 sizes (already given in the array sizes; notice you can get size i by sizes[i]) generates 5 arrays (using randomArray()) and sorts each using insertion, selection, and bubble and displays the number of comparisons.

Make sure that you don't do something like this:

       int* array = randomArray(sizes[i]);
       int compars = insertionSort(array);
       printf("Insertion sort tooked %d comparison's\n", compars);
       compars = selectionSort(array);
       printf("Selectino sort tooked %d comparsions\n", compars);

(Spelling, grammar, and punctuation mistakes are deliberate to remind you that this is bad code. :) )

What's the problem with that? When we used selection sort, the array was already sorted! Instead, you should make a copy of the original, random array using copyArray() for each sorting algorithm.

Your program should generate readable output, something like

	Insertion 	Selection 	Bubble
Size 5000:
	6233199		12497500	24420115
	6242791		12497500	24430113
...

What do you observe?

5. To turn in

Leave your code in a gradable state in the directory. The TAs will log in and grade it there.


Thomas VanDrunen
Last modified: Thu Jan 12 10:01:10 CST 2012