Binary Search (BS) : Md. Mehedi Hassan

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Binary Search (BS)

Md. Mehedi Hassan


ID: IT 19603, Session: 2018-2019, M.Sc. (Engg.) student,
Department of Information and Communication Technology (ICT),
Mawlana Bhashani Science and Technology University
Santosh, Tangail-1902, Bangladesh.

28 January 2022 Md. Mehedi Hassan 1


Binary Searching
 Binary search is the search technique that works efficiently on
sorted lists. Hence, to search an element into some list using
the binary search technique, we must ensure that the list is
sorted.

Binary search follows the divide and conquer approach in


which the list is divided into two halves, and the item is
compared with the middle element of the list. If the match is
found then, the location of the middle element is returned.
Otherwise, we search into either of the halves depending
upon the result produced through the match.
Md. Mehedi Hassan 2
Binary Searching
 Search a sorted array by repeatedly dividing the search
interval in half. Begin with an interval covering the whole
array. If the value of the search key is less than the item in the
middle of the interval, narrow the interval to the lower half.
Otherwise, narrow it to the upper half. Repeatedly check until
the value is found or the interval is empty.

 NOTE: Binary search can be implemented on sorted array


elements. If the list elements are not arranged in a sorted
manner, we have first to sort them.

Md. Mehedi Hassan 3


Start
A [] = Input Array
Svalue = Search Value
Max size = Maximum size of input array No Yes
Max size = -1
BEG = Starting index
END = Ending index
MID = Middle index Read (Svalue)

Array is empty
BEG=0, END= Max size, MID=(BEG+END)/2

No Yes
A [MID]=Svalue
No Yes
BEG=END
Value is found

No Yes
A [MID]>Svalue Value is not found

BEG=MID+1 END=MID-1
MID=(BEG+END)/2 MID=(BEG+END)/2
Stop
28 January 2022 Md. Mehedi Hassan 4
BEG = B, END = E, MID = M = (B+E)/2

Search = 24 3 6 9 13 16 24 39 55 72 93
0 1 2 3 4 5 6 7 8 9
24 > 16,
3 6 9 13 16 24 39 55 72 93
Take 2nd half
B=0 1 2 3 M=4 5 6 7 8 E=9

24 < 55,
Take 1st half
3 6 9 13 16 24 39 55 72 93
0 1 2 3 4 B=5 6 M=7 8 E=9

Found 24,
Return 5
3 6 9 13 16 24 39 55 72 93
0 1 2 3 4 B=5 E=6 7 8 9
M=5

28 January 2022 Md. Mehedi Hassan 5


Binary Searching Complexity
 Best Case Complexity - In Binary search, best case occurs when the
element to search is found in first comparison, i.e., when the first
middle element itself is the element to be searched. The best-case
time complexity of Binary search is O(1).

 Average Case Complexity - The average case time complexity of


Binary search is O(logn).

 Worst Case Complexity - In Binary search, the worst case occurs,


when we have to keep reducing the search space till it has only
one element. The worst-case time complexity of Binary search is
O(logn).
Md. Mehedi Hassan 6
Real Life Examples
 Dictionary
 Height of Students
 Library
 Page Number
 University

Md. Mehedi Hassan 7


Thank You
Any Question ?

28 January 2022 Md. Mehedi Hassan 8

You might also like