tutorial2b - Is it good enough No Space of an entry may be...

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

View Full Document Right Arrow Icon
Tail Recursion Speaker : MARK
Background image of page 1

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

View Full DocumentRight Arrow Icon
What is in-place algorithm? Algorithm that uses O(1) extra space in addition to the original input How about Quicksort ? Quicksort has in-place partition Then, Quicksort is in-place algorithm ? NO !!
Background image of page 2
Quicksort In-place ! In-place ?
Background image of page 3

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

View Full DocumentRight Arrow Icon
Quicksort needs stack 1 3 7 8 2 6 4 5 1 3 7 8 2 6 4 5 after partition Qsort(A,1,4) Qsort(A,6,8) STACK Q(1,8) Q(1,4),Q(6,8)
Background image of page 4
Quicksort needs stack (cont.) 1 3 7 8 2 6 4 5 after partition Qsort(A,1,3) Qsort(A,6,7) STACK Q(1,8) Q(1,4),Q(6,8) Q(1,3),Q(6,7) 1 3 7 8 2 6 4 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
Worst Case Space 1 2 3 4 5 6 7 8 STACK Q(1,8) Q(1,7) Q(1,6) Q(1,1) STACK size = O(n) entries Can we use less space ?
Background image of page 6
Method I P while (length(X i ) > n/2) partition again X 1 X 2 STACK Q(X 3 ),Q(X 4 ),Q(X 2 ) X 3 X 2 P X 4 until all length(X i ) < n/2
Background image of page 7

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

View Full DocumentRight Arrow Icon
Method I (cont.) STACK Q(X 3 ),Q(X 4 ),Q(X 2 ) Q(X 5 ),Q(X 6 ),Q(X 7 ) STACK size = O(log n) entries
Background image of page 8
Background image of page 9

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

View Full DocumentRight Arrow Icon
Background image of page 10
Background image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Is it good enough ? No! Space of an entry may be as large as O(n) Method II P if (there is X with length(X) < n/2) call Qsort(X) else partition X into X ’ and X ’’ X 1 X 2 STACK Q(X 2 ) Q(X 3 ) Q(X 4 ) X 3 X 2 P X 4 until all X are processed Method II (cont.) STACK size = O(log n) entries Space of every entry is only O(1) STACK Q(X 2 ) Q(X 3 ) Q(X 4 ) Conclusion The idea of Method II is tail recursion First solves sub-problem with smaller size Call recursion only when sub-problem is small enough Even with the improvement, Method II ’ s space complexity = input + O(log n) Still not in-place algorithm !!...
View Full Document

This note was uploaded on 05/07/2008 for the course CS cs3333 taught by Professor Ancd during the Spring '08 term at Academy of Design Tampa.

Page1 / 11

tutorial2b - Is it good enough No Space of an entry may be...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online