Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion Problems.

Similar presentations


Presentation on theme: "Recursion Problems."— Presentation transcript:

1 Recursion Problems

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


Download ppt "Recursion Problems."

Similar presentations


Ads by Google