CSC 115.005/006 Sonoma State University Spring 2022
Scribbler 2
CSC 115.005/006:
Programming I
Scribbler 2
Instructor: Henry M. Walker

Lecturer, Sonoma State University
Professor Emeritus of Computer Science and Mathematics, Grinnell College


Course Home References Course Details: Syllabus, Schedule, Deadlines, Topic organization MyroC Documentation Project Scope/
Acknowledgments

Notes:

Laboratory Exercise on Insertion Sort

Overview

The purpose of this lab is to gain familiarity with the method of data sorting known as insertion sort. To illustrate this approach, the insertion sort algorithm is applied both to numbers and to pixels within an image.


Work Started in Class

Reading Review

  1. Today's reading discusses the insertion sort algorithm for ordering data within an array. Write one to three sentences to answer each of the following questions.

    1. The reading claims that insertion sort is better for nearly-sorted lists than reverse-ordered lists. Why?
    2. How much extra memory is required for each sort?
    3. If the list is composed of four identical elements, how many elements change position when insertion sort is used? Why?
    4. Why might some people use insertion sort in real life?

Manual Use of Insertion Sort

  1. 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.

    1. [ 5 | 4 | 1 ]
    2. [ 3 | 1 | 3 ]
    3. [ 2 | 5 | 4 | 0 ]
    4. [ 6 | 8 | 3 | 5 | 1 | 9 | 2 | 2 ]

Identifying Elements of an Insertion Sort

  1. 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.
    1. Compile and run both programs. Do both sort the given list appropriately?
    2. Look at insertion-sort-proc1.c. Is this an example of insertion sort?
      Hint: What exactly are the nested for loops doing?
    3. Look at insertion-sort-proc2.c. Is this an example of insertion sort?
      Hint: What exactly is the while loop doing?

Error Checking in Insertion Sort

  1. Download and save the program insertion-sort-proc3.c in your directory for this lab.
    1. Compile and run the program with the values 1, 7, 3, 5, 4, 2, 9, 8, 2, 6. Does the program produce the correct output
    2. Now run the program with a few of your own values. Does the program still produce correct output?
      Hint: try making some elements in the list negative.
    3. Read through the program to locate the source of the error and fix it.
      Hint: the error is caused by one line in the program.
    4. Write a paragraph explaining why this error caused the output you saw.

Insertion Sort with Pictures

Although the description of the insertion sort in the reading considered the ordering of integers, the algorithm applies to any data which can be ordered.


  1. This problem considers applying the insertion sort to the pixels in a picture.

    1. Program insertion-sort-picture.c provides a shell for applying an insertion sort to an array of pixels.

    2. Read over the program and be sure you understand it.

    3. Compile and run the program, and describe what happens.

    4. Using the the insertion sort in the reading as starting point, complete the pixelInsertionSort procedure to sort the pixels in the local, 1-dimensional array picArray

      • Much of the algorithm will run without change.
      • Adjust the comparison of the size of array values in sorting, so that the sum of R, G, and B values are compared for two pixels.
      • Adjust the type of material being sorted, so that pixels are manipulated rather than integers.

      Be sure to compile and run the program to be sure it works.

insertion-sort-picture.c has the following high-level outline.


  1. Assuming the previous step was successful, you observed an original picture and a final picture after the pixels had been sorted.

    1. Add calls to pix1DArrayToPicrDisplayPicture within the main loop of the insertion sort, so that a picture is displayed every 50th iteration of the for loop. This should allow you to watch sorting progress periodically as the algorithm runs.
    2. Change the picture display, so that it updates more or less often than every 50 iterations. Do you find one frequency of updates more insightful or more fun to watch than another?
    3. Write 1-2 paragraphs to describe how the picture changes as sorting progresses.

Homework

Insertion Sort with Column-Major Order

In Module 010, you learned about arrays. In this module, you learned about multi-dimensional 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:

Human-readable array of two rows and five columns

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:

Memory representation of 2D array in C

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. Some 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:

Memory representation of column-major order

When sorting a single-dimensional array in C, the most common method is to sort the elements from smallest to largest. Though sorting a multi-dimensional 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.

  1. Write a program that, using insertion sort, sorts a two-dimensional array in row-major order such that the elements in each row go from smaller to larger.

  2. Write a program that takes a two-dimensional array and, using insertion sort, sorts it in column-major order, so the values in the top of each column are the smallest in the column, with the largest value in each column in the bottom.



created 1 August 2011 by April O'Neill
last revised 8 August 2011 by April O'Neill
minor editing 7 November 2011 by Henry M. Walker
reformatting lab, editing programs 2 February 2014 by Henry M. Walker
readings added 19 September 2014 by Henry M. Walker
discussion of 2D arrays rethought November/December 2014 by Anita DeWitt
minor editing (mostly formatting) 25 December 2014 by Henry M. Walker
new insertion-sort-picture.c January 2015 by Henry M. Walker
reformatted and updated for varying Picture sizes 19 October 2016 by Henry M. Walker
typos correct 26 November 2014 by Henry M. Walker
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.