recursion2 - Recursion - II Binary search Fibonacci...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Recursion - II Binary search Fibonacci sequence Printing string Palindrome GCD of two numbers
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Binary search ( recursive version) Divide and conquer implementation We have already looked at the iterative version of binary search algorithm. Given an array with elements in the ascending order, the problem is to search for a target element in the array. Here is the recursive version of the binary search. It returns the position of the element which matches the target. If the target is not found, it returns -1. Use this code to implement binary search program, and test for arrays containing odd numbered elements as well as arrays containing even numbered elements. In order to understand the working of the program introduce a statement to print the middle element every time. Modify the program so that it can handle an array containing elements in descending order. #include <stdio.h> int targetsearch ( int target, int A[], int n ) { int temp; temp = binary(target, A, 0, n-1) ; return temp ; } int binary ( int target, int A [ ], int left, int right) { int mid; if ( left > right ) return -1 ; mid = ( left + right )/ 2; if ( target == A [mid] ){ return (mid);} if ( target < A [mid]) return (binary (target, A, left, mid - 1 )); else return (binary (key, A, mid + 1, right )) ; } The time complexity of the algorithm can be obtained by noting that every time the function is called, the number of elements to be handled reduces to half the previous value. Assuming it takes one operation for each of the 3 IF statements, and 2 operations for computing mid, the total number of operations can be expressed through the recurrence relation T(n) = T( n/2) + 5 Note this relation is very similar to the one that we had for the efficient version of the exponentiation algorithm, and it can be shown that the complexity works out to O(log n).
Background image of page 2
3 Fibonacci Sequence This is a very famous sequence which can be generated through a recursive solution. It has a small history. In 1202, Italian mathematician Leonardo Fibonacci posed a problem that has had a wide influence on many fields. The problem is related to growth in population of rabbits, generation to generation. It is assumed that the rabbits are reproduced according to the following rules: 1. Each pair of fertile rabbits produces a new pair of offspring each month. 2.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/12/2011.

Page1 / 10

recursion2 - Recursion - II Binary search Fibonacci...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online