SUNY Geneseo Department of Computer Science


Performance of List Algorithms

{Date}

CSci 141, Fall 2003
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture


Questions?

Server for "Course Materials" is really perotine.cs.geneseo.edu

From Windows, try

Lists as Data Sets

    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 ) )
                }
            }
        }
    }

Efficiency -- how many primitive List messages do these send? (correlated with execution time)

  Best Case Worst Case
Delete 3n + 1 5n + 1
Search 2 3n + 1
Insert 1 1

Mini-assignment: work out number of primitive messages for delete

Observation:

Best case in terms of length of list

Search

Insert:

What can you say about time?

Asymptotic Notation


Next Lecture