Explanation_BinarySearch_slide2

Add to Favourites
Post to:

Description
Explaining Binary search with examples

Comments
Presentation Transcript Presentation Transcript

Example : Example Let A[9] = { 4,8,10,12,30,40,50,55,60 } be an array containing 9 elements sorted in ascending order and 12 be the data /item to be searched. The middle position of the array (0….8) is 4 (mid = 4), and the value of the element at position four is 30 ( A[mid] = 30 ). A[9] = { 4 , 8 , 10 , 12 , 30 , 40 , 50 , 55 , 60 } ?middle Since the data < A[mid] i.e. (12 < 30 ), therefore the data lies in the upper half ( 0….4) division (say subdivision_1) of the array.The middle position of the upper half division of the array (0….4) is 2 (mid = 2) and the value of the element at position two is 10 i.e. A[2] = 10 i.e. A[mid] = 10 A[9] = { [ 4 , 8 , 10 , 12 , 30 ] , 40 , 50 , 55 , 60 } ?middle Now the data > A[mid] i.e. (12 > 10) which indicates that the data lies in the lower half of the newly divided array i.e. subdivision1. The newly divided array (say subdivision_2) will now contain only 3 elements (10, 12,30 ). Again the middle position of the array subdivision_2 is 3 and the value of the element at position 3 is 12 i.e. A[3]=12 a[9 ] = { [4 , 8 , (10 , 12 , 30) ], 40 , 50 , 55 , 60 } ? middle At this stage since the data 12 is equal to A[3], hence the search is over. In the above example, if the data were to be 15 instead of 12, then we would conclude that “ the data is not in the list ” because the array would be left with only one element and can not be further divided

Want to learn?

Sign up and browse through relevant courses.

Name:
Your Email:
Password:
Country:
Contact no:


Area code Number
Subjects you are interested in:
Word verification: (Enter the text as in image)


Sign Up Already a member? Sign In
I agree to WizIQ's User Agreement & Privacy Policy
5 Members Recommend
46 Followers

Your Facebook Friends on WizIQ

Give live classes, create & sell online courses

Try it free Plans & Pricing

Connect