Binary Search Algorithm
Binary Search Algorithm
3 6 8 12 14 17 25 29 31 36 42 47 53 55 62
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Do Repeat this process Again & Again until value reach’s. we divide and divide array list
until value reach’s. 3
SaiviCodes © 2020 6/26/2020
Binary Search
3 6 8 12 14 17 25 29 31 36 42 47 53 55 62
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
We Find Key = 42 in above Sorted Array List and Value of Midpoints less Than Key so Change
the Low Value
Old_Mid = 8
Low = Old_Mid+1 = [8 + 1] = 9 We Noticed that the key
High = 15 element is Less than its Key = 42
Midpoint we have to Midpoint = 47
Midpoint = [Low + High]/2 leave the Second half
and moved to the first 42 <= 47
3 6 8 12 14 17 25 29 31 36 42 47 53 55 62
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
We Find Key = 42 in above Sorted Array List and Value of Midpoints Greater Than Key so
Change the High Value
Old_Mid = 12
We Noticed that the key
Low = 9
element is greater than Key = 42
High = Old_Mid – 1 = [12-1] = 11 Midpoint = 36
its Midpoint we have to
leave the first half and
Midpoint = [Low + High]/2 42 >= 36
moved to the second
Half.
Midpoint = [9 + 11]/2
Midpoint = [20]/2
Midpoint = 10
Do Repeat this process Again & Again until value reach’s. we divide and divide array list
SaiviCodes © 2020 until value reach’s. 6/26/2020 5
Binary Search
3 6 8 12 14 17 25 29 31 36 42 47 53 55 62
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
mid Low
high
mid
We Find Key = 42 in above Sorted Array List and Value of Midpoints less Than Key so Change
the Low Value
Old_Mid = 10
Low = Old_Mid + 1 = 10 + 1 = 11 Key = 42
High = 11 We Noticed that the key Midpoint = 36
element is Equal than its
Midpoint = [Low + High]/2 Midpoint. Now the 42 == 42
Search is Complete…
Midpoint = [11 + 11]/2
Midpoint = [22]/2
Midpoint = 11
Value Matched we required 42 Value in Array in this Steps Both the Value are Equal and
SaiviCodes © 2020
Binary Search Algorithm Stop… 6/26/2020 6
SUMMARY
If we search this value using Linear Search we noticed into array the value = 42 founded at
index = 11 , so we have to search out this key using 11 Comparisons its too hard and take a
lot of Time.
SaiviCodes © 2020 6/26/2020 7
Binary Search
3 6 8 12 14 17 25 29 31 36 42 47 53 55 62
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Do Repeat this process Again & Again until value reach’s. we divide and divide array list
SaiviCodes © 2020
until value reach’s. 6/26/2020 8
Binary Search
3 6 8 12 14 17 25 29 31 36 42 47 53 55 62
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
We Find Key = 30 in above Sorted Array List and Value of Midpoints less Than Key so Change
the Low Value
Old_Mid = 8
Low = Old_Mid+1 = [8 + 1] = 9 We Noticed that the key
High = 15 element is Less than its Key = 30
Midpoint we have to Midpoint = 47
Midpoint = [Low + High]/2 leave the Second half
and moved to the first 30 <= 47
3 6 8 12 14 17 25 29 31 36 42 47 53 55 62
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
We Find Key = 30 in above Sorted Array List and Value of Midpoints greater Than Key so
Change the High Value
Old_Mid = 12
We Noticed that the key
Low = 9
element is greater than Key = 30
High = Old_Mid – 1 = [12-1] = 11 Midpoint = 36
its Midpoint we have to
leave the first half and
Midpoint = [Low + High]/2 30 <= 36
moved to the second
Half.
Midpoint = [9 + 11]/2
Midpoint = [20]/2
Midpoint = 10
Do Repeat this process Again & Again until value reach’s. we divide and divide array list
SaiviCodes © 2020 until value reach’s. 6/26/2020 10
Binary Search
3 6 8 12 14 17 25 29 31 36 42 47 53 55 62
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Low
high
mid
We Find Key = 42 in above Sorted Array List and Value of Midpoints greater Than Key so
Change the High Value
Old_Mid = 10
Low = 9 Key = 42
High = Old_Mid – 1 = [10-1] = 9 We Noticed that the key Midpoint = 36
element is Equal than its
Midpoint = [Low + High]/2 Midpoint. Now the 30 <= 31
Search is Complete…
Midpoint = [9 + 9]/2
Midpoint = [18]/2
Midpoint = 9
Do Repeat this process Again & Again until value reach’s. we divide and divide array list
until value reach’s. 6/26/2020 11
SaiviCodes © 2020
Binary Search
3 6 8 12 14 17 25 29 31 36 42 47 53 55 62
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Low
high
mid
We Find Key = 42 in above Sorted Array List and Value of Midpoints greater Than Key so
Change the High Value
3 6 8 12 14 17 25 29 31 36 42 47 53 55 62
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Log n 8
4 12
2 6 10 14
1 3 5 7 9 11 13 15
3 6 8 12 14 17 25 29 31 36 42 47 53 55 62
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2 6 10 14
1 3 5 7 9 11 13 15
• BEST CASE IT IS IF YOU ARE SEARCHING FOR A KEY ELEMENT IN THE MIDDLE OF
ARRAY LIST THAT IS BEST CASE AND TIME IS 1 O(1).
• WORST CASE IF YOU ARE SEARCHING FOR A KEY ELEMENT PRESENT ON THE
LEAFE AND AT THE TIME IS LOG N IN WORST CASE O(LOG N).
3 6 8 12 14 17 25 29 31 36 42 47 53 55 62
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15