Hello World! An Invitation to Computer Science
Lecture slides on algorithms applied to lists
Thursday, January 31, 2008
- pseudocode: what and why
- sequential operations: "straight-line" instructions
computation: variable assignment, primitive
arithmetic operations
  input/output (with details abstracted away)
- conditional operations for decision making
- iterative operations for specifying lots of work in small
space
  with power comes responsibility - algorithms may not halt!
- bottom-up reductionism: complex algorithms constructed from
simpler pieces
Division via repeated subtraction
input dividend, divisor
quotient ← 0
remainder ← dividend
while remainder &ge divisor:
remainder ← remainder - divisor
quotient ← quotient + 1
output quotient, remainder
Algorithms that operate on lists
- "real" algorithms rarely operate on just one or two pieces of data
- analogy: consecutively numbered mailboxes
| McNulty | Kema | Lester | Bunk |
| 1 |
2 |
3 |
4 |
- analogy: playing cards
Adding numbers on a list
input n; L1, L2, ... Ln
sum ← 0
i ← 1
while i ≤ n:
sum ← sum + Li
i ← i + 1
output sum
Sequential search
input n; L1, L2, ... Ln; x
found ← false
i ← 1
while not found and i ≤ n:
if Li = x:
found ← true
else:
i ← i + 1
output found
Observe: Boolean variables and conjunction
Finding largest item on a list
input n; L1, L2, ... Ln
max ← L1
i ← 2
while i ≤ n:
if Li > max:
max ← Li
i ← i + 1
output max
Why do we assume n > 0 and start i at 2?
Where analogies break down
- solving problems on computers differs from solving in physical-world
- throwing away v. overwriting
- moving v. copying
- digital copies are perfect and close to free
Pattern matching
- important concept throughout computer science
- can be used on numbers, DNA, text, images, and more
- algorithm in 2.3.4 is simple (not very efficient)
- "nested" loops
- algorithmic development employs abstraction and top-down
design
- eventually need to get very specific
"cannot assume common sense in a computer system"