{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

1a-twoup - Algorithm Correctness We need a mathematical...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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 ..n 1] )
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}