recursion problems

21
Recursion Problems

Upload: darby

Post on 12-Jan-2016

45 views

Category:

Documents


1 download

DESCRIPTION

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 { - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Recursion Problems

Recursion Problems

Page 2: Recursion Problems

Recursion as Repetition

public static void hello (int N)

{ for (int k = 0; k < N; k++)

System.out.println (“Hello World!”);

}

Page 3: Recursion Problems

Recursion as Repetition

public static void hello (int N)

{

if ( N == 0)

return;

else

{

System.out.println (“Hello World!”);

hello (N – 1);

}

}

Page 4: Recursion Problems

Recursion as Repetition

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

Page 5: Recursion Problems

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;

}

Page 6: Recursion Problems

Recursive String Methods

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

Page 7: Recursion Problems

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

}

}

Page 8: Recursion Problems

Recursive String Methods

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

Page 9: Recursion Problems

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.

Page 10: Recursion Problems

Recursive String Methods

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

Page 11: Recursion Problems

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

}

Page 12: Recursion Problems

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

Page 13: Recursion Problems

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

}

}

Page 14: Recursion Problems

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.

Page 15: Recursion Problems

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

}

Page 16: Recursion Problems

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.

Page 17: Recursion Problems

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;

else

return palindrome ( s.substring (1, s.length() - 1) );

}

}

Page 18: Recursion Problems

Recursive Array Methods

• Write a recursive method that will do a sequential search on an array.

Page 19: Recursion Problems

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

}

Page 20: Recursion Problems

Recursive Array Methods

• Write a recursive method that will do a selection sort on an array.

Page 21: Recursion Problems

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