Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1101: Programming Methodology Recitation 9 – Recursion.

Similar presentations


Presentation on theme: "CS1101: Programming Methodology Recitation 9 – Recursion."— Presentation transcript:

1 CS1101: Programming Methodology Recitation 9 – Recursion

2 2 Recursive Methods Every recursive method has the following elements: 1. A test to stop or continue the recursion. 2. An end case (base case) that terminate the recursion. 3. A recursive call(s) that continues the recursion. Example factorial method public int factorial (int N) { if (N == 1) { return 1; } else { return N * factoral (N-1); } Test to stop or continue End case: recursion stops Recursive case: recursion continues with recursive call

3 3 Recursion for Mathematical Functions Compute the sum of first N positive integers public int sum (int N) { if (N == 1) { return 1; } else { return N + sum (N-1); } Compute the exponential A N public double exponent (double A, int N) { if (N == 1) { return A; } else { return A * exponent (A, N-1); } Pass two arguments: Value of A does not change in the calls, but the value of N is decremented after each recursive call

4 4 Recursion for Nonnumerical Applications Compute the length of a string public int length (String str) { if (str.equals(“”)) {//str has no characters return 0; } else { return 1 + length (str.substring(1)); } Compute all the anagrams of a word. An anagram is a word formed by reordering letters of another word Example: anagrams of CAT are CTA, ATC, ACT, TCA, TAC Index of second position is 1

5 5 HOALAHLOLAOHOLHA rotate left recursion Apply recursion to find all the anagrams of these three letters Anagram

6 public void anagram (String word) { int numOfChars = word.length(); if (numOfChars == 1) { //end case – cannot recurse anymore } else { for (int i=1; i <= numOfChars; i++) { char firstLetter = word.charAt(0); suffix = word.substring (1, numOfChars); anagram (suffix);//recurse with remaining letters in word //rotate left word = suffix + firstLetter; } Recursive algorithm to find anagrams What do we do when recursion stops ?  Print out the anagram found But words passed in successive recursive calls are getting shorter since we chop off the first letter  Pass two parameters – prefix and suffix

7 public void anagram (String prefix, String suffix) { int numOfChars = suffix.length(); if (numOfChars == 1) { //end case – print out one anagram System.out.println (prefix + suffix); } else { for (int i=1; i <= numOfChars; i++) { String newSuffix = suffix.substring (1, numOfChars); String newPrefix = prefix + suffix.charAt(0); anagram (newPrefix, newSuffix); //recursive call //rotate left to create a new rearranged suffix suffix = newSuffix + suffix.charAt(0); } Recursive algorithm to find anagrams Call method initially with empty prefix and word as suffix  Anagram (“”, “HALO”); What happens when user enter an empty string ?  Anagram (“”, “”);

8 8 Quicksort p low high p mid partition number[i] < p number[i] > p Array number … Quicksort

9 9 1.Any element can be used as a pivot. 2.For simplicity, use number[low] as pivot p. 3.Scan array and move elements smaller than p to left half and elements larger than p to upper half. 4.Sort lower and upper halves recursively, using quicksort. 5.Pivot p is placed at location mid. 6.Recursion stops when low >= high.

10 public void quickSort (int[] number, int low, int high) { if (low < high) { int mid = partition (number, low, high); quickSort (number, low, mid-1); quickSort (number, mid+1, high ); } private int partition (int[] number, int start, int end) { int pivot = number[start];//start the pivot do {//look for a number smaller than pivot from the end while (start = pivot) { end--; } if (start < end) {//found a smaller number number[start] = number[end]; //now find a number larger than pivot from the start while (start < end && number[start] <= pivot) { start++; } if (start < end) { //found a larger number number[end] = number[start]; } } while (start < end); number[start] = pivot;//done. Move pivot back to array return start; }

11 23179051248833777 0 1 2 3 4 5 6 7 8 startend 23179051248833777 0 1 2 3 4 5 6 7 8 mid pivot = number[start] while (number[end] > pivot) end--; pivot 23 start end number[start] = number[end] 1217905124883377712179051248833777 0 1 2 3 4 5 6 7 8 start end while (number[start] < pivot) start++; number[end] = number[start] 1217905 488337771217905 48833777 0 1 2 3 4 5 6 7 8 while (number[end] > pivot) end--; start, end number[start] = pivot 12172359048833777


Download ppt "CS1101: Programming Methodology Recitation 9 – Recursion."

Similar presentations


Ads by Google