SUNY Geneseo Department of Computer Science


Correctness of Loops

{Date}

CSci 240, Spring 2007
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture

Questions?

Loop Invariants

Note on the sorting algorithm you invented Monday:

    for ( i = 1; i < A.length; i = i + 1 ) {
        for ( j = i; j > 0; j-- ) {
            if ( A[j] < A[j-1] ) {
                k = A[j-1];
                A[j-1] = A[j];
                A[j] = k;
            }
            else {
                break;
            }
        }
    }

Correctness of iterative algorithms

Example: prove that insertion sort (in a more concise form) really leaves A in ascending order

    for ( i = 1; i < A.length; i++ ) {
        for ( j = i; j > 0 && A[j] < A[j-1]; j-- ) {
            k = A[j-1];
            A[j-1] = A[j];
            A[j] = k;
        }
    }

Hand out Problem Set 9

Next

Simple iterative sorting algorithms (no reading)


Next Lecture