Recursion &Faster Sorting

Slides:



Advertisements
Similar presentations
Jyotishka Datta STAT 598Z – Sorting. Insertion Sort If the first few objects are already sorted, an unsorted object can be inserted in the sorted set.
Advertisements

COSC 2006 Data Structures I Recursion III
Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007.
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Recurrences. What is a Recurrence Relation? A system of equations giving the value of a function from numbers to numbers in terms of the value of the.
Recursion.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms.
COMPSCI 105 S Principles of Computer Science Recursion 3.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 15 Recursive Algorithms.
When confronted with a new problem there are two questions you should ask: 1. Is this problem like, or a special case of, a problem that I already know.
Recursion Apan Qasem Texas State University Spring 2011.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Ceng-112 Data Structures I Chapter 6 Recursion.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.
Recursion, Complexity, and Sorting By Andrew Zeng.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Computer Science Searching & Sorting.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 6 Recursion.
Recursion Review: A recursive function calls itself, but with a smaller problem – at least one of the parameters must decrease. The function uses the results.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Divide and Conquer Prudence Wong
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Recursion CENG 707.
Recursive Algorithm R ecursive Algorithm.
Chapter 15 Recursion.
Chapter 15 Recursion.
COMP108 Algorithmic Foundations Divide and Conquer
COMP108 Algorithmic Foundations Divide and Conquer
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Fast Sorting "The bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems."
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion.
Sorting Chapter 13 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Quicksort and Mergesort
Algorithm Analysis (for Divide-and-Conquer problems)
Chapter 4: Divide and Conquer
Data Structures & Algorithms
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion UW CSE 160 Winter 2017
CS1120: Recursion.
CS 2210 Discrete Structures Advanced Counting
Recursion: The Mirrors
Recursion.
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Design and Analysis of Algorithms
Presentation transcript:

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... 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) {

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.

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)

Fibonacci, version 2 int fibon(int n) { return addSeq(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 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 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(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)

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) = 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 = 2 + 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

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

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) {

Exercise Write a recursive version of countNodes()

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

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: struct node *reverse(struct node *list) {

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; }

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 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.

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