340 likes | 378 Views
Board: CBSE<br>Class: 12<br>Concept: Binary Search using recursion in Python<br>Subject : Python
E N D
GVK CHINMAYA VIDYALAYA GVK CHINMAYA VIDYALAYA SENIOR SECONDARY SCHOOL SENIOR SECONDARY SCHOOL Kothuru, Indukurupet, SPS Nellore Binary Search Class: 12 Subject: Python Teacher: C Vijaya Kumar B.Tech,MBA
MID HIGH LOW 2 3 4 10 40 high>=low: 4>=0 TRUE mid=2
MID LOW HIGH 2 3 4 10 40 If arr[mid]==10: arr[2]==10: 4 = 10 FALSE
LOW HIGH MID 2 3 4 10 40 Elif arr[mid]>x: arr[2]>10: 4>10: FALSE
LOW HIGH 2 3 4 10 40 binary(arr,3,4,10)
HIGH LOW,MID 2 3 4 10 40 binary(arr,3,4,10) If high>=low: 4>=3: TRUE mid=3
LOW and MID HIGH 2 3 4 10 40 arr[3]==10 TRUE
LOW and MID HIGH 2 3 4 10 40 Mid value is returned here
LOW and MID HIGH 2 3 4 10 40 result=3
LOW and MID HIGH 2 3 4 10 40 If 3 not equal to -1: Item found
Lets us see the flow of execution of item not found in the same binary search
HIGH LOW 2 3 4 10 40 def binary(arr,0,4,41):
HIGH MID LOW 2 3 4 10 40 If 4>=0: TRUE mid=2
HIGH MID LOW 2 3 4 10 40 If 4==41: False
HIGH MID LOW 2 3 4 10 40 elif 4 > 41: False
HIGH MID LOW 2 3 4 10 40 binary(arr,3,4,41):
HIGH LOW 2 3 4 10 40 binary(arr,3,4,41):
HIGH LOW and MID 2 3 4 10 40 If 4 > 3: TRUE mid=3
HIGH LOW and MID 2 3 4 10 40 elif 10 > 41: FALSE
HIGH LOW and MID 2 3 4 10 40 binary(arr,4,4,41)
LOW and HIGH 2 3 4 10 40 binary(arr,4,4,41)
MID,LOW and HIGH 2 3 4 10 40 If 4 >= 4: TRUE Mid = 4
MID,LOW and HIGH 2 3 4 10 40 if 40 == 41: FALSE
MID,LOW and HIGH 2 3 4 10 40 elif 40 > 41: FALSE
MID,LOW and HIGH 2 3 4 10 40 binary (arr,5,4,41):
HIGH 2 3 4 10 40 binary (arr,5,4,41): Low = 5 High = 4 X = 41
HIGH 2 3 4 10 40 If 4>=5: FALSE
HIGH 2 3 4 10 40 Return -1
HIGH 2 3 4 10 40 Return -1 (Third Recursive Call)
HIGH 2 3 4 10 40 Return -1 (Second recursive call)
HIGH 2 3 4 10 40 Return -1 (First recursive call)
HIGH 2 3 4 10 40 Result = -1
HIGH 2 3 4 10 40 If False: Else print(“element not present in array”)