CS 163 Lecture-Module #09:
Basic Sorting


general task: given values (probably not ordered)
e.g.: K E R A H Z U C . . .
and a function specifying
    for any two values which should precede the other
e.g. here "<" for  char
task of sorting is to finish with the given values
totally ordered according to the specified precedence.

there are various algorithms for sorting
you should know several different ones because each has advantages, disadvantages in different circumstances:
* one fast sorting algorithm (presented later) requires twice as much space
* another generally fast sorting algorithm can be slow sometimes, requires messy implementation — rem. time to develop, maintain costs...
* one basic sorting algorithm

two basic ones you should know are as follows:

selection-sort

English description of this algorithm:
repeatedly search unsorted elements for smallest one,
then next smallest one, then next next smallest, and so on;
as find these, place them in order starting at front of array
e.g.:
    K   E   R   A   H   Z   U   C
    p           |
                |
                smallest 
                |
    A   E   R   K   H   Z   U   C
        p                       |
        |                       |
        |                       next_smallest
        |                       |
    A   C   R   K   H   Z   U   E
            p                   |
    A   C   E   K   H   Z   U   R
                p   |
                      .
                        .
                          .
    A   C   E   H   K   R   U   Z
implementation:
    public static void selectionSort(Comparable[] a) {
                // "p": "pass"
        for ( int p = 0; p < a.length - 1 ; p++ ) {
            int next_smallest_i = p;

            for ( j = p + 1; j < a.length; j++ )
                if ( !(a[next_smallest_i] < a[j]) )
                    next_smallest_i = j;

            Comparable tmp = a[p];
            a[p] = a[next_smallest_i];
            a[next_smallest_i] = tmp;
            }
        }
Θ(N²)



insertion-sort

English description of this algorithm:
process unsorted items as they are given,
inserting them one at a time in sorted sequence at front of array
e.g.:
    K   E   R   A   H   Z   U   C
        |
        |
        next 'unsorted' element (a[p])
     / \ 
    E   K   R   A   H   Z   U   C
            |
            |
            next (a[p])
            |
    E   K   R   A   H   Z   U   C
     / \   \   \p
    A   E   K   R   H   Z   U   C
                    p
showing sub-steps at this point:
            tmp = a[p] == "H"
    A   E   K   R   *   Z   U   C       
                   j=p
    A   E   K   *   R   Z   U   C       
                j
    A   E   *   K   R   Z   U   C       
            j
    A   E   H   K   R   Z   U   C       
                        p
                          .
                            .
                              .
    A   C   E   H   K   R   U   Z
implementation:
    public static void insertionSort(Comparable[] a) {
        int  j;
        for ( int p = 1; p < a.length; p++ ) {
            Comparable tmp = a[p];

            for ( j = p;  j >= 1  &&  tmp.compareTo(a[j-1]) < 0;  j-- )
                a[j] = a[j - 1];

            a[j] = tmp;
            }
        }
Θ(N²)


(Copyright © 2008 by Hugh McGuire   — for thoughts about this, see   http://www.cis.gvsu.edu/~mcguire/teaching/copyright_thoughts.html .)