Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concepts of recursive algorithms, specifically insertion sort and tower of hanoi, in the context of the cs 173: discrete structures course. Students will learn how to compute the time complexity of these functions and create their own recursive algorithm. The document also discusses the efficiency of recursive sorting algorithms like mergesort.
Typology: Study notes
1 / 15
This page cannot be seen from the preview
Don't miss anything!
Review
Insertion sort… Time for an animation: http://www.cs.oswego.edu/~mohammad/classes/csc241/samples/sort/Sort2-E.html
Create your own recursive algorithm!
2
2
2
Tower of Hanoi… The Tower of Hanoi invented by French mathematician, Edouard Lucas, in 1883. TH (A, B, C,n)
Towers of Hanoi efficiency (or lack thereof) Now let’s analyze the running time. T(n) = 2 T(n-1) + C Unroll a bit… = 2 (2 T(n-2) + C) + C = 4 T(n-2) + 3C = 4 (2 T(n-3) + C) + 3C = 8 T(n-3) + 7C … = 2 k T(n-k) + ( k -1)C What is k when this bottoms out at 1?
n- T(1) + ( n- -1)C = ( n -1) C = O( n ) More general techniques investigated in a couple weeks
Recursive sorting…. MergeSort (list)
Recursive sorting…. MergeSort (list)
Merge sort Write down the recurrence relation: T(n) = 2 T(n/2) + cn We’ll talk about techniques for solving it later.
When is recursion inefficient? Recursive function for computing the nth Fibonacci number: Iterative function for computing the nth Fibonacci number: Which of these is better?