Download presentation
Presentation is loading. Please wait.
1
Programming with Recursion
2
Example of Recursion (Ask Until the User Gets It Right) import java.util.* ; public class CountDown { private int count; public static void main(String[] args) { CountDown countDowner = new CountDown( ); countDowner.getCount( ); countDowner.showCountDown( ); } public void getCount( ) { System.out.println("Enter a positive number:"); Scanner keyboard = new Scanner(System.in); count = keyboard.nextInt( ); if (count = 0; left--) System.out.print(left + ", "); System.out.println("Blast Off!"); } }
3
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].
4
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: ArraySearcher bSearcher = new ArraySearcher(b); int index = bSearcher.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 ArraySearcher { private int[] a; /** Precondition: theArray is full and is sorted from lowest to highest. */ public ArraySearcher(int[] theArray) { a = theArray;//a is now another name for theArray. }
5
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, mid + 1, last); } return result; } }
6
Example of Recursion (Binary Search) import java.util.*; public class ArraySearcherDemo { 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.nextInt( ); System.out.println( ); for (i = 0; i < 10; i++) System.out.print("a[" + i + "]=" + a[i] + " "); System.out.println( ); System.out.println( ); ArraySearcher finder = new ArraySearcher(a); String ans; do { System.out.println("Enter a value to search for:"); int target = keyboard.nextInt( ); 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.equalsIgnoreCase("yes")); System.out.println( "May you find what you're searching for."); } }
7
Merge Sort – A Recursive Sorting Method Merge sort is an example of a divide- and-conquer algorithm. The array to be sorted is divided in half, and the two halves of the array are sorted by recursive calls. That process produces two smaller sorted arrays. The two sorted arrays are then merged to form a single sorted array.
8
Merge Sort – A Recursive Sorting Method Algorithm If the array a has only one element do nothing (stopping case). Otherwise, do the following (recursive case): 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.
9
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 MergeSort { /** Precondition: Every indexed variable of a has a value. Action: Sorts a so that a[0] = 2) { int halfLength = a.length/2; int[] front = new int[halfLength]; int[] tail = new int[a.length - halfLength]; divide(a, front, tail); sort(front); sort(tail); merge(a, front, tail); } //else do nothing. a.length == 1, so a is sorted. }
10
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]; }
11
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 frontIndex = 0, tailIndex = 0, aIndex = 0; while ((frontIndex < front.length) && (tailIndex < tail.length)) { if (front[frontIndex] < tail[tailIndex]) { a[aIndex] = front[frontIndex]; aIndex++; frontIndex++; } else { a[aIndex] = tail[tailIndex]; aIndex++; tailIndex++; } } //At least one of front and tail has been //completely copied to a. while (frontIndex < front.length)//Copy rest of front, //if any. { a[aIndex] = front[frontIndex]; aIndex++; frontIndex++; } while (tailIndex < tail.length)//Copy rest of tail, if any. { a[aIndex] = tail[tailIndex]; aIndex++; tailIndex++; } } }
12
Merge Sort – A Recursive Sorting Method Code (cont’d) public class MergeSortDemo { 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( ); MergeSort.sort(b); System.out.println("Array values after sorting:"); for (i = 0; i < b.length; i++) System.out.print(b[i] + " "); System.out.println( ); } }
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.