Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 103 #14 2012.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
MiniDraw Testing COMP 102 # T1
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
Problem Solving #6: Search & Sort ICS Outline Review of Key Topics Review of Key Topics Problem 1: Recursive Binary Search … Problem 1: Recursive.
Searching Arrays Linear search Binary search small arrays
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review COMP 102 #
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Computer Science Victoria University of Wellington Copyright: david streader, Victoria University of Wellington Simple Design COMP
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Recursion.
Building Java Programs Chapter 13 Searching reading: 13.3.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
COSC 2006 Data Structures I Recursion II
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Recursion COMP 102 #32.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays with meaningful indices.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors Data Abstraction & Problem Solving.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Patterns with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 More Collections: Queues,
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
An introduction to costs (continued), and Binary Search 2013-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington.
David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays with meaningful indices.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington “For each” & Patterns with.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
More about costs: cost of “ensureCapacity”, cost of ArraySet, Binary Search 2014-T2 Lecture 12 School of Engineering and Computer Science, Victoria University.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Programs with Choice Booleans,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Fast Sorting COMP
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Recursion COMP 102 #30.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 Analysing Costs COMP 103.
2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on ArrayLists COMP 102.
Computer Science Victoria University of Wellington Copyright: david streader, Victoria University of Wellington Simple Design COMP
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
COMP 103 Tree Traversals Lindsay Groves 2016-T2 Lecture 22
Binary Search one reason that we care about sorting is that it is much faster to search a sorted list compared to sorting an unsorted list the classic.
RECURSION COMP 103 Thomas Kuehne 2016-T2 Lecture 16
Binary Search Trees (I)
Adapted from Pearson Education, Inc.
searching Concept: Linear search Binary search
COMPUTER 2430 Object Oriented Programming and Data Structures I
The structure of programming
Running Time Exercises
Presentation transcript:

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 103 #

© Peter Andreae COMP :2 Menu Binary Search SortedArraySet: Implementing Set with Binary Search Recursion Notes:

© Peter Andreae COMP :3 Recursion Methods that call themselves No different from calling another method! Gives lots of power for designing algorithms Critical for dealing with some data structures public void drawBoxes(int x, int y, int wd, int ht){ if ( ht>10 ) { UI.fillRect(x-wd/2, y-ht, wd, ht); drawBoxes(x, y, wd-5, ht-5); } Base case: When does it not call itself? Recursive call Each call has its own set of arguments and local variables.

© Peter Andreae COMP :4 Recursion Doing stuff before or after the recursive call: public void drawBoxes(int x, int y, int wd, int ht){ if ( ht>10 ) { drawBoxes(x, y, wd-5, ht-5); UI.fillRect(x-wd/2, y-ht, wd, ht); } } public void drawBoxes(int x, int y, int wd, int ht){ if ( ht>10 ) { UI.fillRect(x-wd/2, y-ht, wd, ht); drawBoxes(x, y, wd-5, ht-5); UI.drawLine(x-wd/2, y-ht, x+wd/2, y); } } done "on the way back"

© Peter Andreae COMP :5 More recursion Draw fancy square: draw outline square draw top-left smaller fancy square draw top-right smaller fancy square draw bottom-left smaller fancy square draw bottom-right smaller fancy square public void drawFSquareint x, int y, int sz,){ if ( sz>=20 ) { UI.drawRect(x, y, sz, sz); int offset = size/2+2.5; int smallSz = (sz- 15/2); drawFSquare(x+5, y+5, smallSz); drawFSquare(x+offset, y+5, smallSz); drawFSquare(x+5, y+offset, smallSz); drawFSquare(x+offset, y+offset, smallSz); }

© Peter Andreae COMP :6 Recursion with an array public double findMax(double[] data, int start, int end){ if ( end-start == 1 ) { return data[start]; } else { int mid = (start+end)/2; double[] lmax = findMax(data, start, mid); double[] rmax = findMax(data, mid, end); return Math.max(lmax, rmax); } past the last item: items at :start … end-1

© Peter Andreae COMP :7 Recursive binary search. private boolean contains (Object item){ return BinarySearch((Comparable ) item, 0, count-1); } private boolean contains (Comparable item, int low, int high){ if (low > high) { return false; } int mid = (low + high) / 2; int c = item.compareTo(data[mid]); if (c < 0) { return contains (item, low, mid-1); } else if (c > 0) { return contains (item, mid+1, high); } else { return true; } } Same as the other binary search, Easier to design.

© Peter Andreae COMP :8 Recursive binary search with trail. List path = new ArrayList (); BinarySearch( item, 0, count-1, path); UI.println(path); private boolean search(String item, int low, int high, List path ){ if (low > high) { return false; } int mid = (low + high) / 2; path.add(data[mid]); int c = item.compareTo(data[mid]); if (c < 0) { return contains (item, low, mid-1, path); } else if (c > 0) { return contains (item, mid+1, high, path); } else { return true; } } Pass the path along, adding items to it as you go.