Download presentation
Presentation is loading. Please wait.
Published byTheresa Welch Modified over 9 years ago
1
1 “Not all recursive solutions are better than iterative solutions…” “… recursion, however, can provide elegantly simple solutions to problems of great complexity” - Textbook, p. 49 CompSci 105 SS 2005 Principles of Computer Science Lecture 7: Searching via Recursion Lecturer: Santokh Singh Assignment 1 due Today!!! Date: 14.1.05
2
2 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p. 57-58 n = 0 A: fact(n-1)= ? return= ? n= 1 A: fact(n-1)= ? return= ? n = 3 A: fact(n-1)= ? return= ? n= 2 A: fact(n-1)= ? return= ? A A A A
3
3 Proving recursive algorithms 1.Prove each base cases works 2.Prove each recursive case works* 3.Prove all recursive calls meet a base case public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Proof by induction Textbook, p. 751-5
4
4 Invariants public static int fact( int n ) { if ( n == 0 ) { return 1; } else { // Invariant: return n * fact( n-1 ); } Textbook, p. 56
5
5 Reversing a String Textbook, p. 59ff god “ ” If string is empty do nothing Otherwise, Output last character Reverse substring of first (n-1) characters
6
6 //General Description: Converts a decimal number to // Binary representation. //Precondition: Input is a positive decimal // number. //Postcondition: Returns Binary representation // of input. //----------------------------------------------------- private static String binVal(long decVal){ String retString = ""; if(decVal / 2 > 0) retString = binVal(decVal / 2); retString += (decVal % 2); return retString; } Recursive Decimal to Binary Conversion by student “Charlie” during his CS 105 here
7
7 Loop invariants Is there something that we want to be true every time the while test is executed? // Computes the sum of // the first n items. int sum = 0; int j = 0; while (j < n) { sum += item[j]; j++; } Textbook, p. 10
8
8 sum contains the sum of the first j items j is in the range 0..n-1 Textbook, p. 10 // Computes the sum of // the first n items. int sum = 0; int j = 0; while (j < n) { sum += item[j]; j++; } Loop Invariants:
9
9 sum contains the sum of the first j items j is in the range 0..n-1 Textbook, p. 10 // Computes the sum of // the first n items. int sum = 0; int j = 0; while (j < n) { sum += item[j]; j++; } Loop Invariants: 31 item: j: n: 2 01 sum:
10
10 sum contains the sum of the first j items j is in the range 0..n Textbook, p. 10 // Computes the sum of // the first n items. int sum = 0; int j = 0; while (j <= n) { sum += item[j]; j++; } Loop Invariants: an error
11
11 Searching in a sorted array Binary search Searching in an unsorted array Finding the smallest item Finding the k th smallest item
12
12 Divide and Conquer Search Phone Directory Search 1st Half of Phone Directory Search 2nd Half of Phone Directory Textbook, p. 50
13
13 Binary Search Search Phone Directory Search 1st Half of Phone Directory Search 2nd Half of Phone Directory Textbook, pp. 77ff
14
14 bSearch( anArray, value) if (anArray is of size 1) Is anArray’s item = to value? else Find midpoint of anArray Which half of anArray has value? bSearch( halfOfAnArray, value) Binary Search Algorithm
15
15 bSearch( anArray, value) if (anArray is of size 1) Is anArray’s item = to value? else Find midpoint of anArray Which half of anArray has value? bSearch( halfOfAnArray, value) Binary Search Algorithm Textbook, pp. 78
16
16 73198 anArray: value: Textbook, pp. 78 01234
17
17 73198 anArray: first: last: value: Textbook, pp. 78 01234
18
18 73198 anArray: first: last: mid: value: Textbook, pp. 80 01234
19
19 bSearch( anArray, value, first, last ) if (last == first) Is anArray’s item = to value? else mid = (first + last) / 2 if (value is in first half ) bSearch( anArray,value,first,mid) else bSearch( anArray,value,mid+1,last) Textbook, pp. 78
20
20 Searching in a sorted array Binary search Searching in an unsorted array Finding the smallest item Finding the k th smallest item
21
21 Unsorted array: Find Smallest Find smallest Find smallest in First half Find smallest in Second half 93871 01234 Textbook, pp. 76
22
22 Unsorted array: Find k th Smallest Find k th smallest Find k th smallest in First half Find k th smallest in Second half 93871 01234 Textbook, pp. 81ff
23
23 Please remember to write YOUR name and login/ID on your assignment 1 answers’ booklet.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.