Recursion Problems.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Java Programming Strings Chapter 7.
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L10 (Chapter 19) Recursion.
JAVA, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli | Ralph Walde Trinity College Hartford, CT presentation slides for published by Prentice.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
CS102 – Recursion David Davenport Bilkent University Ankara – Turkey Spring 2003.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
CSE 143 Lecture 11 Recursive Programming reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 12 Lecture 12-2: recursive programming reading:
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
COSC 2006 Data Structures I Recursion II
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
From C++ to Java A whirlwind tour of Java for C++ programmers.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
String Processing Word processing term papers, writing memoirs, sending messages, responding to surveys, placing online orders and registering products.
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
RECURSION, CONTINUED Lecture 8 CS2110 – Fall 2015.
Print Me Who’s Data? First Executed Name Me My Mistake Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final Jeopardy.
A: A: double “4” A: “34” 4.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
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.
Recursion. What is Recursion? Method of solving problems Alternative to iteration All recursive solutions can be implemented iteratively Characteristic...
CS102 – Recursion David Davenport Bilkent University Ankara – Turkey
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
תרגול חזרה לבוחן נמרוד מילוא.
Chapter 18 Recursion CS1: Java Programming Colorado State University
Chapter 14 Recursion.
Recursion Version 1.0.
Lecture 24: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Lecture 10: recursive programming reading:
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Lecture 3 Linear Search and Binary Search ArrayLists
More on Recursion.
Chapter 19 Recursion.
Tutorial 8 Pointers and Strings
Alg2_1c Extra Material for Alg2_1
Modern Programming Tools And Techniques-I Lecture 11: String Handling
Java Strings Slides provided by the University of Washington Computer Science & Engineering department.
CS 106A, Lecture 9 Problem-Solving with Strings
F4105 JAVA PROGRAMMING F4105 Java Programming
יסודות מדעי המחשב – תרגול 4
Testing change to a linked list
An Introduction to Java – Part I, language basics
كلية المجتمع الخرج البرمجة - المستوى الثاني
Lecture 10: recursive programming reading:
CSE 143 Lecture 11 Recursive Programming reading:
searching Concept: Linear search Binary search
Building Java Programs
CS18000: Problem Solving and Object-Oriented Programming
Recursion.
CSE 143 Lecture 11 Recursive Programming slides created by Marty Stepp
Recursion Chapter 18.
Example. Sort {5, 1, 12, -5, 16, 2, 12, 14} using selection sort. Complexity analysis.
Chapter 18 Recursion.
Sieve of Eratosthenes The Sieve of Eratosthenes uses a bag to find all primes less than or equal to an integer value n. Begin by creating a bag an inserting.
Lecture 19: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
CSE 143 Lecture 13 Recursive Programming reading:
CSE 143 Lecture 13 Recursive Programming reading:
Recursion Method calling itself (circular definition)
CS101 - Algorithms & Programming I
Introduction to java Part I By Shenglan Zhang.
More on iterations using
Presentation transcript:

Recursion Problems

Recursion as Repetition public static void hello (int N) { for (int k = 0; k < N; k++) System.out.println (“Hello World!”); }

Recursion as Repetition public static void hello (int N) { if ( N == 0) return; else System.out.println (“Hello World!”); hello (N – 1); }

Recursion as Repetition Write a recursive method pow() that returns xn, where x and n are both integers greater than or equal to zero.

Recursion as Repetition public static long pow (int x, int n) { if ( x == 0) return 0; if ( n == 0) return 1; long result = x * pow ( x, n – 1); return result; }

Recursive String Methods Write a recursive method that will print the characters in a string recursively, one character at a time.

Recursive String Methods public static void printString (String s) { if ( s.length() == 0) return; else { System.out.println ( s.charAt(0) ); printString ( s.substring (1) ); }

Recursive String Methods Write a recursive method that will print a String in reverse order one character at a time.

Recursive String Methods public static void printReverse ( String s ) { if ( s.length() > 0 ) { printReverse ( s.substring ( 1 ) ); System.out.println ( s.charAt ( 0 ) ); } Many recursive solutions involve breaking a sequential structure, such as a string or an array, into its head and tail. An operation is performed on the head, and the algorithm recurses on the tail.

Recursive String Methods Write a recursive method that will count the number of occurrences of the character ch in the String s.

Recursive String Methods public static int countChar (String s, char ch) { if ( s.length() == 0 ) return 0; else if ( s.charAt ( 0 ) == ch) return 1 + countChar ( s.substring (1), ch); else return 0 + countChar ( s.substring (1), ch); }

Recursive String Methods Write a recursive method to rotate a String by N characters to the right. For example, rotateR (“hello”, 3) should return “llohe”.

Recursive String Methods public static String rotateR (String s, int n) { if ( n == 0 ) return s; else { StringBuffer buf = new StringBuffer (); buf.append (s.charAt (s.length() - 1)); buf.append (s.substring (0, s.length() - 1)); return rotateR (buf.toString(), n - 1); }

Recursive String Methods Write a recursive method to convert a String representing a binary number to its decimal equivalent. For example, binTodecimal (“101011”) should return the int 43.

Recursive String Methods public static int binTodecimal (String s) { if ( s.length() == 1) return Integer.parseInt (s); else return Integer.parseInt (s.substring (s.length() - 1)) + 2 * binTodecimal (s.substring (0, s.length() - 1); }

Recursive String Methods A palindrome is a string that is the same as its reverse – “radar” and “able was I ere I saw elba”. Write a recursive boolean method that determines whether its String parameter is a palindrome.

Recursive String Methods public static boolean palindrome ( String s) { if ( s.length() == 0 || s.length() == 1 ) return true; else { if ( s.charAt(0) != s.charAt (s.length() - 1)) return false; return palindrome ( s.substring (1, s.length() - 1) ); }

Recursive Array Methods Write a recursive method that will do a sequential search on an array.

Recursive Array Methods public static int rSearch (int[] arr, int head, int key ) { if ( head == arr.length ) return –1; else if ( arr[head] == key ) return head; else return rSearch ( arr, head + 1, key ); }

Recursive Array Methods Write a recursive method that will do a selection sort on an array.

Recursive Array Methods public static void selectionSort ( int[] arr, int last ) { if ( last > 0 ) { int maxLoc = findMaxIdx ( arr, last); swap ( arr, last, maxLoc ); selectionSort ( arr, last – 1 ); } public static int findMaxIdx ( arr, last) { int maxIdx = 0; for (int i = 0; i <= last; i++) if ( arr [i] > arr[maxIdx) maxIdx = i;