SUNY Geneseo Department of Computer Science


Correctness of List Algorithms

{Date}

CSci 141, Fall 2003
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture


Misc

Hand out Lab 10

CS Dept. Halloween party

Questions?

Counting operations in an algorithm

Reading Summary

Sections 9.2.2, 9.2.3

Asymptotic notation = way to find simpler function than actual closed form for recurrence to show how time grows

Take fastest-growing term in equation,

Proportionality is all you can say about running time relative to theoretically derived times because of computer speeds

Testing asymptotic hypotheses ???

n Time n2 Time/n2
100 0.1 seconds 10000 .00001
200 0.33 40000

.000008

400 1 second 160000 .000006
1000 5 seconds 1000000 .000005
2000 22 seconds 4000000 .000006

Asymptotic List Performance

  Best Case Worst Case
Delete Theta(n) Theta(n)
Search Theta(1) Theta(n)
Insert Theta(1) Theta(1)

Correctness of List Algorithms

    class DataList extends List {

        DataList insert( Object x ) {
            return new DataList( x, this )
        }

        boolean search( Object x ) {
            if ( ! this.isEmpty() ) {
                if ( this.first().equals(x) ) {
                    return true
                }
                else {
                    return this.rest().search( x )
                }
            }
            else {
                return false;
            }
        }

        DataList delete ( Object x ) {
            // Postcondition: Result contains no
            // occurrences of x
            if ( this.isEmpty() ) {
                return this
            }
            else {
                if ( this.first().equals(x) )
                    return this.rest().delete( x )
                else {
                    return new DataList( this.first(), this.rest().delete( x ) )
                }
            }
        }
    }

Search: returns true if x is in the list, returns false if x is not in list


Next Lecture