Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion &Faster Sorting

Similar presentations


Presentation on theme: "Recursion &Faster Sorting"— Presentation transcript:

1 Recursion &Faster Sorting
Plan for Today: More fun with recursion Introduction to Quicksort "If the code and the comments disagree, then both are probably wrong." -Norm Schryer

2 Recursion Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21...
F0 = 0, F1 = 1 Fn = Fn-1 + Fn-2 for n >= 2 Exercise: Write a recursive function to compute Fn: // pre: n >= 0 long fibon(int n) {

3 Fibonacci long fibon(int n) { if(n == 0) return 0;
return fibon(n-1) + fibon(n-2); } This is quite inefficient. Consider the calls made in computing fibon(8) = 21: fibon(8) calls fibon(7) and fibon(6) fibon(7) calls fibon(6) and fibon(5) fibon(6) calls fibon(5) and fibon(4) fibon(5) calls fibon(4) and fibon(3) etc.

4 Fibonacci, version 2 The Fibonacci sequence is an example of an additive sequence: Specify t0, t1 For n >= 2, tn = tn-1 + tn-2 For sequence t0, t1, t2, t3, t4, t5, ... Computing the nth term is the same as computing the (n-1)st term in the sequence t1, t2, t3, ... that begins one term further along. int addSeq(int n, int t0, int t1) { if(n == 0) return t0; if(n == 1) return t1; return addSeq(n-1, t1, t0 + t1); } To compute fibon(5), call addSeq(5, 0, 1)

5 Fibonacci, version 2 int fibon(int n) { return addSeq(n, 0, 1); }
This prevents the explosion of terms we saw in the first version.

6 Towers of Hanoi 3 pegs (A, B and C) and n disks
Each disk is a different size Only one peg can be moved at a time A bigger disk can never be placed on top of a smaller disk To begin: all disks stacked on one peg Goal: Move all disks to one of the other pegs

7 Towers of Hanoi Move n disks from peg A to peg B Recursive solution:
Move top n-1 disks from peg A to peg C Move largest disk from peg A to peg B Move n-1 disks on peg C to peg B

8 Towers of Hanoi Move disks from peg source to peg dest
tower(numDisks, source, dest, spare): if numDisks == 0: move disk from source to dest else: tower(numDisks – 1, source, spare, dest) move disk from source to dest tower(numDisks – 1, spare, dest, source) To move 5 disks from peg A to peg B, we call: tower(5, A, B, C)

9 Towers of Hanoi T(N) = # of moves tower makes for N disks
Base case (n=1): Move the one disk directly. T(1) = 1. Otherwise, follow the 3 steps to get: T(N) = T(N-1) T(N-1) = 2T(n-1) + 1 Analysis: want a closed form (i.e., non-recursive) formula for T(N) Start by writing out T(N) for several values of N: T(1) = 1 T(2) = 2T(1) + 1 = = 3 T(3) = 2T(2) + 1 = 7 T(4) = 2T(3) + 1 = 2(7) + 1 = 15 T(5) = 2T(4) + 1 = 31 T(N) = 2N - 1

10 Recursion & Linked Lists
For these examples, assume the following node definition: struct node { int value; struct node *next; } Write a function which returns the number of nodes in a linked list: int countNodes(struct node *list) {... } Iteratively Recursively

11 Exercise Write an iterative function that takes a pointer to a linked list and returns the number of nodes in the list int countNodes(struct node *list) {

12 Exercise Write a recursive version of countNodes()

13 Exercise Recursive version of countNodes()
int countNodes(struct node *ptr) { if(ptr == NULL) return 0; return 1 + countNodes(ptr  next); }

14 Reversing a Linked List
Write an iterative function that reverses a linked list: struct node *reverse(struct node *list) {

15 Reversing a Linked List
Write an recursive function that reverses a linked list: struct node *reverse(struct node *list) {

16 Reversing a Linked List
Write an recursive function that reverses a linked list: struct node *reverse(struct node *list) { struct node *revList; if(list == NULL || list  next == NULL) return list; revList = reverse(list  next); list  next  next = list; list  next = NULL; return revList; }

17 Stable Sorting A stable sorting algorithm guarantees the relative order of equal items doesn't change [51, 71, 6, 52, 10, 72, 2, 1, 73] [1, 2, 51, 52, 6, 71, 72, 73, 10] After sorting with stable sort

18 Quicksort Invented by Tony Hoare
Recursive divide and conquer algorithm A list of 0 or 1 elements is already sorted For larger list, pick any list element p, the pivot Partition the list (excluding pivot) into sub-lists, one of values less than p, the other of values greater than p equal values go to either sublist smaller values to the left of p, larger values to the right Return quicksort of first sublist, followed by pivot, following by quicksort of second sublist Partitioning step: order in the 2 sublists doesn't matter. Just want all elements smaller than pivot to its left, all elements larger to its right.

19 Quicksort piv = 6 1 2 3 4 5 6 7 8 9 11 12 14 10 piv = 3 piv = 11 1 2 3 4 5 6 7 8 9 12 14 10 11 1 2 3 4 5 6 7 8 9 10 11 14 12


Download ppt "Recursion &Faster Sorting"

Similar presentations


Ads by Google