Algorithm Correctness
•
We need a mathematical justification for how we
think about recursion.
•
Proving correctness gives us confidence that the
algorithm is correct.
•
The process often reveals subtleties and/or errors.
•
Knowing how to prove correctness helps us to
design better algorithms.
•
Techniques fit well with the topdown approach.
1
Algorithm Analysis
•
We will use mathematical techniques to analyze
resource usage (e.g., time, space).
•
For example, the stack usage for
InsertSort
is
linear in the size of the array.
•
Thus, for moderately large array sizes, the runtime
stack will be exhausted.
•
In order for the algorithm to be useful on larger
inputs, the stack usage must be much lower.
2
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
BottomUp Implementation
InsertSort
(
A
[1
..n
]
)
if
n >
1
InsertSort
(
A
[1
..n
−
1]
)
Insert
(
A
[1
..n
]
)
•
Note that the recursive call is essentially the first
thing done.
•
We can therefore do the same computation by
iteratively applying
Insert
to
A
[1
..
2]
,A
[1
..
3]
,...,A
[1
..n
]
.
3
Tail Recursion
RecursiveInsert
(
A
[1
..n
]
)
if
n >
1
and
A
[
n
]
< A
[
n
−
1]
A
[
n
]
↔
A
[
n
−
1]
RecursiveInsert
(
A
[1
..n
−
1]
)
•
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '09
 Howell
 Recursion, Control flow, Postcondition, maximum subsequence sum, maximum suffix sum

Click to edit the document details