A Recipe for Algorithm Design Or, What to do when the data type doesn't match the function…yet.

Slides:



Advertisements
Similar presentations
Chapter 14 Recursion Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,, E. Reingold.
Advertisements

Higher-Order Functions and Loops c. Kathi Fisler,
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
Introduction to Algorithms Quicksort
Chapter 7 Sorting Part II. 7.3 QUICK SORT Example left right pivot i j 5 > pivot and should go to the other side. 2 < pivot and should go to.
Lab1 Lab 1: CSG 711: Programming to Structure Karl Lieberherr.
David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Recursion Michael Ernst UW CSE 140 To seal: moisten flap, fold over, and seal.
1 Today’s Material Medians & Order Statistics – Ch. 9.
Quicksort File: D|\data\bit143\Fall01\day1212\quicksort.sdd BIT Gerard Harrison Divide and Conquer Reduce the problem by reducing the data set. The.
Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 6: Generative.
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
QuickSort Example Use the first number in the list as a ‘pivot’ First write a list of the numbers smaller than the pivot, in the order.
 2003 Prentice Hall, Inc. All rights reserved. 1 Recursion Recursive functions –Functions that call themselves –Can only solve a base case If not base.
Creating Functional Programs Brad Vander Zanden. Basic Techniques Tail Recursion – Use continuation arguments if necessary – Akin to pre-processing a.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
© 2004 Goodrich, Tamassia Selection1. © 2004 Goodrich, Tamassia Selection2 The Selection Problem Given an integer k and n elements x 1, x 2, …, x n, taken.
S: Application of quicksort on an array of ints: partitioning.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 14 Recursion.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.5 TexPoint fonts used in EMF. Read the TexPoint manual.
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
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
Quick Sort By: HMA. RECAP: Divide and Conquer Algorithms This term refers to recursive problem-solving strategies in which 2 cases are identified: A case.
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.
1 Lecture 16: Lists and vectors Binary search, Sorting.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Sorting What makes it hard? Chapter 7 in DS&AA Chapter 8 in DS&PS.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
 initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.
1 Heapsort, Mergesort, and Quicksort Sections 7.5 to 7.7.
Halting Measures and Termination Arguments CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Lab 11 Lab 1: CSG 711: Programming to Structure Karl Lieberherr.
 initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Recursive Algorithms Section 5.4.
Chapter 19: Recursion.
CS 550 Programming Languages Jeremy Johnson
Class 11: Two-argument recursion
CS 1321.
Programming in Java: lecture 10
CS 270 Math Foundations of CS Jeremy Johnson
Quicksort 1.
How can this be simplified?
Chapter 14: Recursion Starting Out with C++ Early Objects
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion UW CSE 160 Winter 2017
Recursion Spring 2015 UW CSE 160
Recursion UW CSE 160 Spring 2018
Recursion Winter 2014 UW CSE 140
Yan Shi CS/SE 2630 Lecture Notes
Recursion UW CSE 160 Winter 2016
RECURSION Haskell.
Announcements Quiz 5 HW6 due October 23
Lecture #7 מבוא מורחב.
Chapter 9: Medians and Order Statistics
Recursive Algorithms 1 Building a Ruler: drawRuler()
Core Assessments Core #1: This Friday (5/4) Core #2: Tuesday, 5/8.
Rewriting your function using map and foldr
Presentation transcript:

A Recipe for Algorithm Design Or, What to do when the data type doesn't match the function…yet.

The Recipe Before Chapter 25 Data analysis and design Contract, purpose, header Examples Template The thing with the dot dot dots. On lists, we fully captured the template with the fold function. Look! Ma! No dot dot dots! We said it can do "almost everything"…

Quicksort Breaks Template (define (qsort l) (cond [(empty? l) empty] [else (local ((define pivot (first l)) (define other (rest l))) (append (qsort [filter (lambda (x) (< x pivot)) other]) (list pivot) (qsort [filter (lambda (x) (>= x pivot)) other])))]))

Quicksort Still Terminates (define (qsort l) (cond [(empty? l) empty] [else (local ((define pivot (first l)) (define other (rest l))) (append (qsort [filter (lambda (x) (< x pivot)) other]) (list pivot) (qsort [filter (lambda (x) (>= x pivot)) other])))]))

The Notsoquicksort (define (qsort l) (cond [(empty? l) empty] [else (local ((define pivot (first l)) (define other l)) (append (qsort [filter (lambda (x) (< x pivot)) other]) (qsort [filter (lambda (x) (>= x pivot)) other])))]))

A Recipe for General Algorithms Data analysis and design Contract, purpose, header Examples Template A bit more flexible than before Termination argument Needs to be done, explicitly, seperately

Example termination argument ; At each step, quick-sort partitions the list into two ; sublists using smaller-items and larger-items. Each ; function produces a list that is smaller than the ; input (the second argument), even if the threshold ; (the first argument) is an item on the list. Hence ; each recursive application of quick-sort consumes ; strictly shorter list than the given one. Eventually, ; quick-sort receives and returns empty. Without such an argument an algorithm must be considered incomplete.

Why Generative Recursion? What if we can chose between A structural solution and A generative solution? Often, the second can be much faster Book has very nice example Greatest-common-divisor (GCD) gcd(6,9)=3, gcd (99, 18) = 9, etc.