[Current] [News] [Glance] [Discussions] [Instructions] [Search] [Links] [Handouts] [Outlines] [Readings] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [Fall2000.01] [Spring2000]

Sorting a collection of values -- arranging them in a fixed order, usually alphabetical or numerical -- is one of the commonest computing applications. When the number of values is even moderately large, sorting is such a tiresome, error-prone, and time-consuming process for human beings that the programmer should automate it whenever possible. For this reason, computer scientists have studied this application with extreme care and thoroughness.

One of the clear results of their investigations is that no one algorithm
for sorting is best in all cases. Which approach is best depends on
whether one is sorting a small collection or a large one, on whether
the individual elements occupy a lot of storage (so that moving them around
in memory is time-consuming), on how easy it is to compare two elements to
figure out which one should precede the other, and so on. In this course
we'll be looking at two of the most generally useful algorithms for
sorting: *insertion sort*, which is the subject of today's
lab, and *merge sort*, which we'll talk about in the next lab.

Imagine first that we're given a collection of values and a rule for
arranging them. The values might actually be stored either in a list or in
a vector; let's assume first that they are in a list. The rule typically
takes the form of a predicate of arity 2 that can be applied to any two
values in the set to determine whether the first of them could precede the
second when the values have been sorted. (For example, if one wants to
sort a set of real numbers into ascending numerical order, the rule should
be the predicate `<=`

; if one wants to sort a set of strings
into alphabetical order, ignoring case, the rule should be
`string-ci<=?`

, and so on.)

The insertion sort works by taking the values one by one and inserting each
one into a new list that it constructs, constantly maintaining the
condition that the elements of the new list are in the desired order with
respect to one another. Clearly, this condition will not be maintained if
each element is added to the new list at the beginning, using
`cons`

; instead, the insertion sort adds each element at a
carefully selected position within the new list, placing the new element
*after* each previously placed element that precedes it according
to the given precedence rule, but *before* every such element that
it precedes. The following procedure, `insert`

, adds a new
element to a list in exactly this way. For the moment, we'll assume that
the elements of the list are real numbers and than we want to sort them
into ascending order; `<=`

is therefore used as the ordering
predicate.

(define insert-number (lambda (new-element ls) (cond ((null? ls) (list new-element)) ((<= new-element (car ls)) (cons new-element ls)) (else (cons (car ls) (insert-number new-element (cdr ls)))))))

In English: If the list into which the new element is to be inserted is
empty, return a list containing only the new element. If the new element
can precede the first element of the existing list, then, since the
existing list is assumed to be sorted already, it must also be able to
precede *every* element of the existing list, so attach the new
element onto the front of the existing list and return the result.
Otherwise, we haven't yet found the place, so issue a recursive call to
insert the new element into the cdr of the current list, then reattach its
car at the beginning of the result.

The preceding version of the `insert-number`

procedure is not
tail-recursive. When dealing with long lists, you may want to use the
following tail-recursive version, which uses space more economically:

(define insert-number2 (lambda (new-element ls) (let kernel ((rest ls) (bypassed '())) (cond ((null? rest) (revappend bypassed (list new-element))) ((<= new-element (car rest)) (revappend bypassed (cons new-element rest))) (else (kernel (cdr rest) (cons (car rest) bypassed))))))) (define revappend (lambda (left right) (if (null? left) right (revappend (cdr left) (cons (car left) right)))))

(The `revappend`

procedure takes two lists and returns the
result of concatenating the *reverse* of the first one to the front
of the second one.)

Now let's return to the overall process of sorting an entire list. The insertion sort algorithm simply takes up the elements of the list to be sorted one by one and inserts each one into a new list, initially empty:

(define insertion-sort-numbers (lambda (lst) (let helper ((unsorted lst) ; The remaining unsorted values (sorted null)) ; The sorted values (if (null? unsorted) sorted (helper (cdr unsorted) (insert-number (car unsorted) sorted))))))

Finally, let's consider the rather different case in which the values that
we want to arrange are presented as a vector and the goal of the sorting
algorithm is to *overwrite* the old arrangement of those values with
a new, sorted arrangement of the same values.

Instead of constructing a new vector, we partition the original vector into two subvectors: a sorted subvector, in which all of the elements are in the correct order relative to one another, and an unsorted subvector in which the elements are still in their original positions. The two subvectors are not actually separated; instead, we just keep track of a boundary between them inside the original vector. Items to the left of the boundary are in the sorted subvector; items to its right, in the unsorted one. Initially the boundary is at the left end of the vector. The plan is to shift it, one position at a time, to the right end. When it arrives, the entire vector has been sorted.

Here's the plan for the main algorithm.

(define insertion-sort! (lambda (less-than-or-equal? vec) (let ((len (vector-length vec))) (let helper ((boundary 0)) ; The index of the first unsorted value (if (< boundary len) ; If we have elements left to sort (begin (insert! (vector-ref vec boundary) vec boundary less-than-or-equal?) (helper (+ boundary 1))))))))

The `insert!`

procedure takes four arguments: an element to be
inserted into the sorted part of the vector, the vector itself, the
current boundary position, and the comparison procedure. The new element
can be inserted at any position
up to and including the current boundary position, but it must be placed in
the correct order relative to elements to the left of that boundary. This
means that any elements that should follow the new one should be shifted
one position to the right in order to make room for the new one. (Elements
that precede the new one can keep their current positions.)

You will write this `insert!`

procedure in the lab.

November 21, 1997 (John Stone and/or Henry Walker)

- Created

April 15, 2000 (John Stone)

- Last revised.

Friday, 20 November 2000 (Sam Rebelsky)

- Copied to CSC152 course web from
`http://www.cs.grinnell.edu/~stone/courses/scheme/sorting-methods.xhtml`

. - Rewrote sorting routines to eliminate use of
`do`

(not yet covered in my section). - Excised section.
- Excised exercises.

[Current] [News] [Glance] [Discussions] [Instructions] [Search] [Links] [Handouts] [Outlines] [Readings] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [Fall2000.01] [Spring2000]

**Disclaimer** Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2000F/Readings/sorting.html

Source text last modified Fri Nov 17 22:05:59 2000.

This page generated on Fri Nov 17 22:07:45 2000 by Siteweaver. Validate this page's HTML.

Contact our webmaster at rebelsky@grinnell.edu