SUNY Geneseo Department of Computer Science


Performance of Insertion Sort

{Date}

CSci 240, Spring 2007
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture

Misc

Return hour exams

Lab 7 due tomorrow

Questions?

Analyzing Loops' Performance

Basic idea is to calculate the number of steps executed as loop runs

This always means summing the number of steps executed in each iteration over all the iterations

Best and worst cases are often different

Example: Insertion Sort

    insertionSort( array A )
        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;
            }
        }

[Sum for i=1 to n-1 of 3i = 3/2(n^2 - n) = Theta(n^2)]

Hand out Problem Set 10

Next

Quicksort, an algorithm that improves on the Θ(n2) performance of insertion sort and selection sort

Read sections 10.1 and 10.2

Hand out Lab 8


Next Lecture