This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 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 top-down 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 Bottom-Up 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...
View Full Document
- Spring '09