tutorial2b

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

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

Tail Recursion Speaker : MARK

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

View Full Document
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 !!
Quicksort In-place ! In-place ?

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

View Full Document
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)
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

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

View Full Document
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 ?
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

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

View Full Document
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

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

View Full Document
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
Ask a homework question - tutors are online