Programming with Recursion Example of Recursion Ask Until












- Slides: 12

Programming with Recursion

Example of Recursion (Ask Until the User Gets It Right) import java. util. * ; public class Count. Down { private int count; public static void main(String[] args) { Count. Down count. Downer = new Count. Down( ); count. Downer. get. Count( ); count. Downer. show. Count. Down( ); } public void get. Count( ) { System. out. println("Enter a positive number: "); Scanner keyboard = new Scanner(System. in); count = keyboard. next. Int( ); if (count <= 0) { System. out. println("Input must be positive. "); System. out. println("Try again. "); get. Count( ); //start over } } } public void show. Count. Down( ) { int left; System. out. println("Counting down: "); for (left = count; left >= 0; left--) System. out. print(left + ", "); System. out. println("Blast Off!"); }

Binary Search Using Recursion mid = approximate midpoint between first and last; if (first > last) return -1; else if (target == a[mid]) return mid; else if (target < a[mid]) return the result of searching a[first] through a[mid -1]. else if (target > a[mid]) return the result of searching a[mid+1] through a[last].

Example of Recursion (Binary Search) /** Class for searching an already sorted array of ints. To search the sorted and completely filled array b, use the following: Array. Searcher b. Searcher = new Array. Searcher(b); int index = b. Searcher. find(target); index will be given an index of where target is located. index will be set to -1 if target is not in the array. */ public class Array. Searcher { private int[] a; /** Precondition: the. Array is full and is sorted from lowest to highest. */ public Array. Searcher(int[] the. Array) { a = the. Array; //a is now another name for the. Array. }

Example of Recursion (Binary Search) /** If target is in the array, returns the index of an occurrence of target. Returns -1 if target is not in the array. */ public int find(int target) { return search(target, 0, a. length - 1); } } //Uses binary search to search for target in a[first] through //a[last] inclusive. Returns the index of target if target //is found. Returns -1 if target is not found. private int search(int target, int first, int last) { int result = -1; //to keep the compiler happy. int mid; if (first > last) result = -1; else { mid = (first + last)/2; if (target == a[mid]) result = mid; else if (target < a[mid]) result = search(target, first, mid - 1); else //(target > a[mid]) result = search(target, mid + 1, last); } return result; }

Example of Recursion (Binary Search) import java. util. *; public class Array. Searcher. Demo { public static void main(String[] args) { int [] a = new int[10]; System. out. println("Enter 10 integers in increasing order. "); System. out. println("One per line. "); Scanner keyboard = new Scanner(System. in); int i; for (i = 0; i < 10; i++) a[i] = keyboard. next. Int( ); System. out. println( ); for (i = 0; i < 10; i++) System. out. print("a[" + i + "]=" + a[i] + " "); System. out. println( ); Array. Searcher finder = new Array. Searcher(a); String ans; do { System. out. println("Enter a value to search for: "); int target = keyboard. next. Int( ); int result = finder. find(target); if (result < 0) System. out. println( target + " is not in the array. "); else System. out. println( target + " is at index " + result); System. out. println("Again? "); ans = keyboard. next( ); }while (ans. equals. Ignore. Case("yes")); } } System. out. println( "May you find what you're searching for. ");

Merge Sort – A Recursive Sorting Method o Merge sort is an example of a divide-and -conquer algorithm. o The array to be sorted is divided in half, and the two halves of the array are sorted by recursive calls. o That process produces two smaller sorted arrays. o The two sorted arrays are then merged to form a single sorted array.

Merge Sort – A Recursive Sorting Method Algorithm o If the array a has only one element do nothing (stopping case). o Otherwise, do the following (recursive case): n n n Copy the first half of the elements in a to a smaller array named front. Copy the rest of the elements in the array a to another smaller array named tail. Sort the array front with a recursive call. Sort the array tail with a recursive call. Merge the elements in the arrays front and tail into the array a.

Merge Sort – A Recursive Sorting Method Code /** Class for sorting an array of ints from smallest to largest, using the merge sort algorithm. */ public class Merge. Sort { /** Precondition: Every indexed variable of a has a value. Action: Sorts a so that a[0] <= a[1] <=. . . <= a[a. length - 1]. */ public static void sort(int[] a) { if (a. length >= 2) { int half. Length = a. length/2; int[] front = new int[half. Length]; int[] tail = new int[a. length - half. Length]; divide(a, front, tail); sort(front); sort(tail); merge(a, front, tail); } //else do nothing. a. length == 1, so a is sorted. }

Merge Sort – A Recursive Sorting Method Code (cont’d) /** Precondition: a. length = front. length + tail. length. Postcondition: All the elements of a are divided between the arrays front and tail. */ private static void divide(int[] a, int[] front, int[] tail) { int i; for (i = 0; i < front. length; i++) front[i] = a[i]; for (i = 0; i < tail. length; i++) tail[i] = a[front. length + i]; }

Merge Sort – A Recursive Sorting Method Code (cont’d) /** Precondition: Arrays front and tail are sorted from smallest to largest, and a. length = front. length + tail. length. Postcondition: a contains all the values from front and tail, and a is sorted from smallest to largest. */ private static void merge(int[] a, int[] front, int[] tail) { int front. Index = 0, tail. Index = 0, a. Index = 0; while ((front. Index < front. length) && (tail. Index < tail. length)) { if (front[front. Index] < tail[tail. Index]) { a[a. Index] = front[front. Index]; a. Index++; front. Index++; } else { a[a. Index] = tail[tail. Index]; a. Index++; tail. Index++; } } //At least one of front and tail has been //completely copied to a. while (front. Index < front. length)//Copy rest of front, //if any. { a[a. Index] = front[front. Index]; a. Index++; front. Index++; } } } while (tail. Index < tail. length)//Copy rest of tail, if any. { a[a. Index] = tail[tail. Index]; a. Index++; tail. Index++; }

Merge Sort – A Recursive Sorting Method Code (cont’d) public class Merge. Sort. Demo { public static void main(String[] args) { int[] b = {7, 5, 11, 2, 16, 4, 18, 14, 12, 30}; System. out. println("Array values before sorting: "); int i; for (i = 0; i < b. length; i++) System. out. print(b[i] + " "); System. out. println( ); Merge. Sort. sort(b); } } System. out. println("Array values after sorting: "); for (i = 0; i < b. length; i++) System. out. print(b[i] + " "); System. out. println( );