SUNY Geneseo Department of Computer Science


Execution Time of Bubble Sort, Part 2

{Date}

CSci 141, Spring 2005
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture


Misc

Second hour exam is next Friday (April 1)

Questions?

Algorithm Design and Analysis Example

Deriving worst-case execution time for bubble sort

Had two recurrence relations

Mini-assignment: look for pattern, and phrase in English

n s(n)
0 1
1 10*1 - 5 + s(0) = 5 + 1 = 6
2 10*2 - 5 + 10*1 - 5 + 1
3 10*3 - 5 + 10*2 - 5 + 10*1 - 5 + 1 = 10(3+2+1) - 5*3 + 1

Patttern:

[Pattern for s(n) with "Sigma" Notation]

Prove s(n) = 5n2 + 1 by induction on n

= Θ(n2)

Next

Stacks, a list-like structure

Motivation: a model for how recursion works (and how it's really implemented in computers)

    1:  fib( n )
    2:      if n <= 1
    3:          return 1
    4:      else
    5:          prev1 = fib( n - 1 )
    6:          prev2 = fib( n - 2 )
    7:          return prev1 + prev2

Read Section 12.2


Next Lecture