SUNY Geneseo Department of Computer Science


Algorithm Design and Analysis with Lists, Part 1

{Date}

CSci 141, Spring 2005
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture


Misc

Hour exams are graded

Questions?

Concatenation exercise

    List list1
        ... initialized somehow....
    List list2
        ... initialized somehow ...
    List list3 = new List()
    list3.concat( list1.copy() )
    list3.concat( list2 )

[Concatenation Leaves Lists Sharing Structure]

Solution: explicitly make copies (as the code above does -- it started without the "list1.copy()")

Algorithm Design and Analysis Example

Problem: sort a list

(Precondition: all items in the list are comparable using "<", ">")

Solution idea: "bubble sort"

[Moving Small Elements to Front in List of Numbers]

An algorithm based on this idea....

    // In a SortableList subclass of List...
    sort()
        if ( ! this.isEmpty() )
            this.findSmallestAndPutAtFront()
                //postcondition: smallest item in list is at head
            this.getRest().sort()
			
    findSmallestAndPutAtFront()
        ...

Is it correct?


Next Lecture