SUNY Geneseo Department of Computer Science


Correctness of Insertion Sort

{Date}

CSci 240, Spring 2007
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture

Misc

Problem Set 9 extended 'til Monday Mar. 6

Questions?

Proofs about loop invariants always induction

Insertion Sort

Prove that insertion sort 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;
        }
    }

[Inserting Value into Sorted Array Section at Position j]

Next

How fast are these sorting algorithms?

Read section 9.1 through 9.1.3 (pages 275 through 281)


Next Lecture