CS 415, Section 001 Sonoma State University Spring, 2024
 
Algorithm Analysis
Instructor: Henry M. Walker

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

Although much of this course has been well developed in recent semesters, the SSU CS faculty recently have approved a partially-updated course description. Currenly, the Web site is reasonably stable, but modest refinements are possbile.


Contract Negotiations Have Yielded a Tentative Agreement

Since May, 2024, the California Faculty Association (CFA) – the labor union of professors, lecturers, librarians, counselors, and coaches across the 23 California State University campuses – has been in negotiations with the management of the California State University System. After a one-day strike on Monday, January 22, the two sides have reached a tentative agreement, and the strike has been called off. Effective Tuesday, January 23, SSU classes (including CS 415) will be held as scheduled.

Insertion Sort

Abstract

Many applications require that data be ordered and maintained.

In C and many other languages, a simple way to structure ordered data is in an array (or vector), such as the following ordered array A of integers. Array A:  2 3 5 7 9 10 13 18 24 27 33 35 37


The Insertion Sort

One common sorting approach is based on code that assumes that the first part of an array is ordered and then adds successive items to this array segment until the entire array is sorted.

Some examples of humans manually using insertion sort include sorting a deck of cards, or sorting books on a bookshelf.

To understand this approach within an array, we first consider how to add one item to an ordered array segment. We then apply this work to each array element in turn to yield an ordered array.


Maintaining An Ordered Array Segment

Suppose items A[0], ..., A[k-1] are ordered in array A:

A:  3 7 9 10 18 27 33 37

To insert an item into the first part of this array and maintain the ordering, we proceed from right to left with elements in the array.

For example, the following figure shows the insertion of the number 15 into the array A above.

Inserting 15 into A:  3 7 9 10 18 27 33 37

The following code translates this idea into C. When this code terminates, item is placed into the array, so that items A[0], ..., A[k] become ordered:

   int i = k-1;
   while ((i >= 0) && a[i] > item){
      a[i+1] = a[i];
      i--;
   }
   a[i+1] = item;

Using this basic insertion step, an array A can be sorted iteratively according to the following outline:

Insertion Sort Example

Animation borrowed from Wikipedia.org under a Creative Commons license.
Image showing an array being sorted


This outline gives rise the the following code, called an insertion sort.


void insertionSort (int a [], int length) {
// method to sort using the insertion sort
// parameters:  a, the array to be sorted
//              length, the size of the a array
   for (int k = 1; k < length; k++) {
      int item = a[k];
      int i = k-1;
      while ((i >= 0) && a[i] > item){
         a[i+1] = a[i];
         i--;
      }
      a[i+1] = item;
   }
}

It is important to notice that the item being compared is not swapped with each item as the loop progresses. The items that are compared against are shifted; after the comparisons, the element being compared is inserted after any large items.

Programming Note

The array parameter for insertionSort may be specified in either of two, interchangeable ways:

As already noted, the two parameter declarations are equivalent and may be used interchangeably.


Insertion Sort Characteristics

This sorting algorithm has several helpful properties.
The insertion sort is

The amount of time this algorithm takes to sort a set is dependent upon the set itself.

Overall, for a set with n elements, if the set is sorted (or nearly so), the algorithm takes only a single time unit for each element, so it takes n time units. On the other hand, if the set is in reverse order (that is, largest to smallest), the time approaches n-squared. You will see the previous two statements proven in a later course.



created 1 August 2011 by April O'Neill
revised 5 August 2011 by April O'Neill
minor editing 7 November 2011 by Henry M. Walker
altered sort header 5 December 2012 by Henry M. Walker
updated and reformatted August-November 2016 by Henry M. Walker
revised January 2023 by Henry M. Walker
revised Summer 2023 by Henry M. Walker
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.