SUNY Geneseo Department of Computer Science


Execution Time of Bubble Sort

{Date}

CSci 141, Spring 2005
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture


Questions?

Execution times for lab?

Algorithm Design and Analysis Example

Problem: sort a list

Bubble Sort

    // In a SortableList subclass of List...
    sort()
        if ( ! this.isEmpty() )
            this.findSmallestAndPutAtFront()
            this.getRest().sort()
			
    findSmallestAndPutAtFront()
        if ! this.isEmpty() && ! this.getRest().isEmpty()
            this.getRest().findSmallestAndPutAtFront()
            if this.getRest().first() < this.first()
                temp = this.getAndRemove()
                this.getRest().addItem( temp )

How fast is bubble sort, in terms of list length, n?


Next Lecture