| CSC 161 | Grinnell College | Spring, 2012 |
| Imperative Problem Solving and Data Structures | ||
The goal of this lab is to gain familiarity with the method of data sorting known as insertion sort, culminating in writing a simple insertion sort program.
In today's reading, you learned about insertion sort. Write one to three sentences to answer each of the following questions.
For each of the following lists, draw the result of each insertion of the insertion sorting routine. You do not need to show the result of each comparison, just the final insertion of the element.
In this exercise, you will examine two sorting programs to determine if
either is insertion sort. Download and save the
programs insertion-sort-proc1.c
and insertion-sort-proc2.c
in your directory for this lab.
insertion-sort-proc1.c. Is this an example of
insertion sort?
Hint: What exactly are the nested for loops doing?
insertion-sort-proc2.c. Is this an example of
insertion sort?
Hint: What exactly is the while loop doing?
Download and save the
program insertion-sort-proc3.c
in your directory for this lab.
Hint: try making some elements in the list negative.
Hint: the error is caused by one line in the program.
Download the
file insertion-sort-picture.c
and save it in your current directory. Read over the program and be sure
you understand it. Using what you have learned in the previous exercises,
write the insertion sort function that sorts the pixels in the picture,
compile, and view the result. The program may take a couple minutes to
run fully.
You will use this program in a future module, so you may wish to save this program.
In Module 2, you learned about arrays. In this module, you learned about multidimensional arrays. Below is an example of initializing a two-dimensional array:
int array[2][5] = { {4, 3, 8, 2, 5}, {2, 1} };
In this example, the array of integers array has two rows,
with the first row initialized with the values 4, 3, 8, 2, and 5, and the
second row with the first two values initialized (2 and 1), and the
remaining values are implicitly initialized to 0. So, a human-readable
version of this two-dimensional array looks like the following:
However, when you initialize the array in C, the program reserves a contiguous amount of memory for the array and assigns the values that have been specified. So, the above array is represented in memory as the following:
As you notice, the C program puts the array in memory a row at a time, beginning from the first row. This is called row-major order, and is the C standard. Other programming languages, such as Fortran, use column-major order, in which each column is stored in memory contiguously. So, in column-major order, the array looks like the following:
When sorting a single-dimensional array in C, the most common method is to sort the elements from smallest to largest. Though sorting a multidimensional array is much less frequent, one method is to perform essentially the same method by sorting each row, so the rows are in order, but the columns are not.
Note that for this exercise, you should not perform a normal insertion sort (row-major order) and simply paste the result in the "column-major sorted" array.
Development of laboratory exercises is an interative process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.