CS1101: Programming Methodology Recitation 9 – Recursion
-
Upload
malik-higgins -
Category
Documents
-
view
42 -
download
0
description
Transcript of CS1101: Programming Methodology Recitation 9 – Recursion
CS1101: Programming Methodology
Recitation 9 – Recursion
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 continueEnd case: recursion stops
Recursive case: recursion continues with recursive call
3
Recursion for Mathematical Functions
Compute the sum of first N positive integerspublic int sum (int N) {
if (N == 1) {return 1;
}else {
return N + sum (N-1);}
}
Compute the exponential AN
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
Recursion for Nonnumerical Applications
Compute the length of a stringpublic 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
H OA L
A HL O
L AO H
O LH A
rotate left
rotate left
rotate left
recursion
recursion
recursion
recursion
Apply recursion to find all the anagrams of these three letters
Anagram
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 leftword = 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
public void anagram (String prefix, String suffix) {int numOfChars = suffix.length();
if (numOfChars == 1) {//end case – print out one anagramSystem.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 suffixsuffix = 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
Quicksort
p
low high
p
mid
partition
number[i] < p number[i] > p
Array number …
Quicksort Quicksort
9
Quicksort
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.
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 pivotdo { //look for a number smaller than pivot from the end
while (start < end && number[end] >= pivot) {end--;
}if (start < end) { //found a smaller number
number[start] = number[end];//now find a number larger than pivot from the
startwhile (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 arrayreturn start;
}
23 17 905 12 48 8337 77
0 1 2 3 4 5 6 7 8
start end
23 17 905 12 48 8337 77
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] 12 17 905 12 48 8337 77
12 17 905 12 48 8337 77
0 1 2 3 4 5 6 7 8
start end
while (number[start] < pivot) start++;
number[end] = number[start] 12 17 905 90 48 8337 77
12 17 905 90 48 8337 77
0 1 2 3 4 5 6 7 8
while (number[end] > pivot) end--;
start, end
number[start] = pivot 12 17 235 90 48 8337 77