Recursion Faster Sorting Plan for Today More fun

  • Slides: 19
Download presentation
Recursion &Faster Sorting Plan for Today: More fun with recursion Introduction to Quicksort "If

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

Recursion • Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21. .

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

Fibonacci long fibon(int n) { if(n == 0) return 0; if(n == 1) return

Fibonacci long fibon(int n) { if(n == 0) return 0; if(n == 1) return 1; 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.

Fibonacci, version 2 • The Fibonacci sequence is an example of an additive sequence:

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

Fibonacci, version 2 int fibon(int n) { return add. Seq(n, 0, 1); } •

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

Towers of Hanoi • 3 pegs (A, B and C) and n disks •

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

Towers of Hanoi • Move n disks from peg A to peg B •

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

Towers of Hanoi • Move disks from peg source to peg dest tower(num. Disks,

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

Towers of Hanoi T(N) = # of moves tower makes for N disks •

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) + 1 + T(N-1) = 2 T(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) = 2 T(1) + 1 = 2 + 1 = 3 T(3) = 2 T(2) + 1 = 7 T(4) = 2 T(3) + 1 = 2(7) + 1 = 15 T(5) = 2 T(4) + 1 = 31 • T(N) = 2 N - 1

Recursion & Linked Lists • For these examples, assume the following node definition: struct

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 count. Nodes(struct node *list) {. . . } • Iteratively • Recursively

Exercise • Write an iterative function that takes a pointer to a linked list

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

Exercise • Write a recursive version of count. Nodes()

Exercise • Write a recursive version of count. Nodes()

Exercise • Recursive version of count. Nodes() int count. Nodes(struct node *ptr) { if(ptr

Exercise • Recursive version of count. Nodes() int count. Nodes(struct node *ptr) { if(ptr == NULL) return 0; return 1 + count. Nodes(ptr next); }

Reversing a Linked List • Write an iterative function that reverses a linked list:

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

Reversing a Linked List • Write an recursive function that reverses a linked list:

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

Reversing a Linked List • Write an recursive function that reverses a linked list:

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

Stable Sorting • A stable sorting algorithm guarantees the relative order of equal items

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

Quicksort • Invented by Tony Hoare • Recursive divide and conquer algorithm • A

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

Quicksort 0 9 1 7 2 5 3 11 4 12 5 2 6

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