Download presentation

Presentation is loading. Please wait.

Published byGisselle Syms Modified over 2 years ago

1
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work How Recursion Works Internally Binary Search Merge Sort

2
CS 1032 Definition of Recursion A function is said to be recursive if it calls itself //Precondition: n is a non-negative integer //Postcondition: returns n!, which is 1*2*3*…*n; 0!=1 // Principle for recursion: n!=(n-1)! * n. long factorial(int n){ if (n==0) return 1; long m=factorial(n-1); // recursion m *=n; return m; }

3
CS 1033 One Example of Recursion: Investment Accounts Suppose you invest in an account $x every year, and that the account grows at an interest rate of 8%. Write a function that computes how much money you have in your account at the end of year n. Call S(n) the amount of money in the account at the end of year n.

4
CS 1034 Investment Example (contd.) The math: S(n) = S(n-1) + interest of past year + new deposit $x S(n)=S(n-1)+0.08*S(n-1)+x S(n)=1.08*S(n-1) + x Note that S(0)=x (the first deposit when opening the acct) //The recursive code: //Precondition: n is a non-negative integer //Postcondition: returns S(n) = 1.08*S(n-1) + x; S(0)=x double S(int n, double x){ if (n==0) return x; double lastYearS = S(n-1,x); // recursion return (1.08*lastYearS+x); }

5
CS 1035 Another Recursion Example: Finding the Minimum in an Array // Precondition: the input is a a double array x[ ] of at least end // elements. start and end are nonnegative integer indexes // marking the portion of x[ ] over which to find the minimum. // end >= start; // Postcondition: returns the smallest value in x[ ]. // Recursion principle: if you we have the minimum of the 1 st // half of x and the minimum of the 2 nd half of x, then the // global minimum is the smaller of those two minimums double min(double x[ ], int start, int end){ // …. On the next slide }

6
CS 1036 The Minimum Example (Contd.) double min(double x[ ], int start, int end){ assert(end >= start && start >=0); if (end == start) // one number to minimize over return x[start]; int mid=(start+end)/2; // mid-point index double min1=min(x, start, mid); // 1 st recursive call double min2=min(x, mid+1, end); // 2 nd recursive call if (min1 <= min2) return min1; else return min2; }

7
CS 1037 Conditions for Valid Recursion For recursive functions to work, the following two conditions must be met: –The input (parameters) of every recursive call must be smaller in value or size than the input of the original function –There must be a basis step where the input value or size is the smallest possible, and in which case the processing is non-recursive.

8
CS 1038 Illustration of the Conditions long factorial(int n){ if (n==0) return 1; // basis step. Input value n is minimum (0). long m=factorial(n-1); // recursion. Input value of recursive call is n-1

9
CS 1039 Illustration of the Conditions (Contd.) double min(double x[ ], int start, int end){ assert(end >= start && start >=0); if (end == start) // Basis step. Input size is minimum = 1. return x[start]; // No recursion int mid=(start+end)/2; // mid-point index double min1=min(x, start, mid); // 1st recursive call double min2=min(x, mid+1, end); // 2nd recursive call // In both recursive calls, the input size is half the original, and so less. if (min1 <= min2) return min1; else return min2; }

10
CS 10310 How Recursion Works Internally It is illustrated in class on the factorial function and the min function

11
CS 10311 How to Think Recursively When coding, do not concern yourself how recursion is unfolding during execution Rather, think of yourself as a boss, and treat each recursive call as an order to one of your trusted subordinates to do something. As a boss, you need not worry how the subordinate does their work. Instead, take the outcome of their work Finally, take the outcome of the subordinate’s work, and use it to compute by yourself the final result.

12
CS 10312 Illustration of Recursive Thinking double min(double x[ ], int start, int end){ assert(end >= start && start >=0); if (end == start) // Basis step. return x[start]; // The boss does the basis step int mid=(start+end)/2; // mid-point index double min1=min(x, start, mid); // subordinate produces min1 double min2=min(x, mid+1, end); // subordinate produces min2 // The two recursive calls are the work of “subordinates”. Don’t // worry how the subordinates do their work. if (min1 <= min2) return min1; else return min2; } // You, the boss, take the // outcome min1 and min2 // from subordinates, and use // them to get the final result.

13
CS 10313 Binary Search Input: –A sorted array X[ ] of size n –A value b to be searched for in X[ ] Output: –If b is found, the index k where X[k]=b –If b is not found, return -1 Definition: An X[ ] is said to be sorted if: X[0] ≤ X[1] ≤ X[2] ≤ … ≤ X[n-1]

14
CS 10314 Binary Search: Method The method is recursive: Compare b with the middle value X[mid] If b = X[mid], return mid If b < X[mid], then b can only be in the left half of X[ ], because X[ ] is sorted. So call the function recursively on the left half. If b > X[mid], then b can only be in the right half of X[ ], because X[ ] is sorted. So call the function recursively on the right half.

15
CS 10315 Illustration of Binary search X[12]: Search for b=12 mid = (0+11)/2 = 5. Compare b with X[5]: 12<20. So search in left half X[0..4] mid = (0+4)/2 = 2. Compare b with X[2]: 12 > 7. So search right half X[3..4] mid = (3+4)/2 = 3.Compare b with X[3]: b=X[3]=12. Return 3. 03 152025273540476015712 14265871091103 152025273540476015712 14265871091103 152025273540476015712 142658710911

16
CS 10316 The Recursive Code of Binary Search int binarySearch(double b, double X[], int left, int right){ if (left == right) if (b==X[left]) return left; else return -1; int mid = (left+right)/2; if (b==X[mid]) return mid; if (b < X[mid]) return binarySearch (b, X, left, mid-1); if (b > X[mid]) return binarySearch(b, X, mid+1, right); }

17
CS 10317 Time Complexity of binarySearch Call T(n) the time of binarySearch when the array size is n. T(n) = T(n/2) + c, where c is some constant representing the time of the basis step and the last if-statement to choose between min1 and min2 Assume for simplicity that n= 2 k. (so k=log 2 n) T(2 k )=T(2 k-1 )+c=T(2 k-2 )+c+c=T(2 k-3 )+c+c+c = … = T(2 0 )+c+c+…c=T(1)+kc=O(k)=O(log n) Therefore, T(n)=O(log n).

18
CS 10318 MergeSort The general problem of sorting is to take as input an unordered array X[ ], and give as output the same set of data but sorted in increasing order Example: –Input: 3 5 2 7 10 8 20 15 14 3 -1 2 -5 –Output: -5 -1 2 2 3 3 5 7 8 10 14 15 20 We are interested in developing an algorithm that does the sorting

19
CS 10319 MergeSort: Recursive Sorting A recursive sorting function works as follows: 1.Make a recursive call to the sorting function to sort the first half of the input array 2.Make another recursive call to sort the 2 nd half of the input array 3.Finally, merge the two sorted halves into a single fully sorted array

20
CS 10320 How to Merge Two Sorted Arrays Say Y[ ] and Z[ ] are two sorted arrays to be merged into a single sorted array Call the first element of an array the head While both arrays Y and Z are non-empty, repeat the following steps: –Compare the two heads of Y and Z –Remove the smaller head and put it next in the output Now either Y or Z is empty. Move the non-empty array to the end of output, and stop. The output is now fully sorted.

21
CS 10321 Illustration of Merging (In class)

22
CS 10322 Code for Merge Time: Since each element of X1 and X2 and X are touched once, the time is proportional to the sum of the sizes of X1 and X2. void merge(double X1[ ], int left1, int right1, // merge X1[left1..right1] doubleX2[ ], int left2, int right2, // and X2[left2..right2] doubleX, int left) { // to X[left…] int i1 = left1; int i2=left2; int i= left; // heads of X1, X2, X. while (i1 <= right1 && i2 <= right2) if (X1[i1] <= X2[i2]) {X[i]=X1[i1]; i1++; i++;} else {X[i]=X2[i2]; i2++; i++;} if (i1

23
CS 10323 Code for MergeSort Time T(n) = 2T(n/2) + cn, where cn is time for merge, and n is size of X. This implies: T(n) = O(n log n). void mergeSort(double X[ ], doubleY[ ], int left, int right){ if (left==right) {Y[left] = X[left]; return;} int mid = (left+right)/2; double Z[right+1]; // next, sort left half of X and put the result in left half of Z mergeSort(X,Z,left,mid); // next, sort right half of X and put the result in right half of Z mergeSort(X,Z,mid+1,right); // next, merge the two halves of Z and put result in Y merge(Z,left,mid, Z,mid+1,right, Y,left); }

24
CS 10324 Illustration of mergeSort (In class)

25
CS 10325 Additional Things for YOU to Do Modify binarySearch so that even if the item b is not found, the function returns the index k where X[k] < b

26
CS 10326 More Things You can Do Write a function (called partition) that takes as input an unsorted array X to do the following: –Let b=the first element of X –Move the data around inside X so that at the end b lands in some position k where X[i] ≤ b for all i b for all i > k. Hint: Have an empty array Y of same size as X. Scan the array X; for each element X[i], if X[i] ≤ b, insert X[i] in the left end of Y, but if X[i]>b, insert X[i] in the right end of Y. At the end, you can copy Y onto X. Use partition to develop another recursive sorting algorithm: 1. call partition; 2. call recursively on X[0..k-1]; 3. call recursively on X[k+1..n-1].

Similar presentations

Presentation is loading. Please wait....

OK

Back to Sorting – More efficient sorting algorithms.

Back to Sorting – More efficient sorting algorithms.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on obesity management systems Ppt on operating system structure Ppt on porter's five forces analysis template Ppt on teachers day quotes Presenter media free download ppt on pollution Ppt on zener diode application Ppt on power grid failure usa Download ppt on phase controlled rectifier inversion Ppt on motivation for management students Free download ppt on conservation of wildlife