Rokibul Alam
CSE 01205989
Team Member’s
Md. Tohidul Alam
CSE 01205984
Antar Hossain
CSE 01205975
2
Md Isma Azam Bappy
CSE 01205994
Our topic
Binary Search
Algorithm
What is Binary
search?
In computer science, Binary search,
also known as ------
* half-interval search,
*logarithmic search,
*binary chop,
Binary search is a search algorithm
that finds the position of a target
value within a sorted array.
How Binary search work
It work only for Sorted Array.
At each level the Array is halved and the key is search in
any one of the half while the other half is rejected.
Best case occurs when we the key we are looking for is
Present in the middle of the array.
Worst case occurs when the key is not present.
Lets see a Example how Binary search Works
How Binary Search Work
value -4 2 7 10 15 20 22 25 30 36
Index 0 1 2 3 4 5 6 7 8 9
min max
Here,
min=0 and Max=9
Now,
Mid=(min+max)/2;
=(0+9)/2
= 4.5 we get floor value 4 here
mid
Example: Searching the array below for the value 20.
Now,
Compare index[mid] with 20
here,
index[4] = 15, Where 15<20
Now Ignore the ( left side ) from mid.
How Binary Search Work
min max
Here,
min= 5 and Max = 9
Now,
Mid=(min+max)/2;
=(5+9)/2
= 7
Example: Searching the array below for the value 20.
Now,
Compare index[mid] with 20
mid
Here,
Index[7] = 25, Where 25>20
Now Ignore the ( Right side ) from mid.
value 20 22 25 30 36
Index 5 6 7 8 9
How Binary Search Work
min max
Here,
min= 5 and Max= 6
Now,
Mid=(min+max)/2;
=(5+6)/2
= 5
Example: Searching the array below for the value 20.
Now,
Compare index[mid] with 20
mid
Here,
Index[5] = 20 => Where 20 ==20
At last we get our Target value (20) from this array
value 20 22
Index 5 6
Flow Chart of Binary Search Start
Lb=0
Ub = size of array
Lb<=Ub?
yes
No
Mid=(Lb+Ub)/2
Key Not Found!
Array[mid]==key?Key Is Found
yes
Array [mid] > key?
yes
Lb=mid+1
No
Ub=mid-1
Pseudo Code:
Why Binary search is more
Better then Linear search.
1.Linear search follows sequence and Binary search doesn’t follow.
2.Linear search starts searching from the starting to ending point. Binary searching starts from
middle point.
3.Space complexity of Linear Search is O(1) and Binary Search is O(logn). In this case Binary
Search is better than Linear Search.
Let’s see the Binary Search Code In
(C program)
Binary search
Binary search

Binary search

  • 2.
    Rokibul Alam CSE 01205989 TeamMember’s Md. Tohidul Alam CSE 01205984 Antar Hossain CSE 01205975 2 Md Isma Azam Bappy CSE 01205994
  • 3.
  • 4.
    What is Binary search? Incomputer science, Binary search, also known as ------ * half-interval search, *logarithmic search, *binary chop, Binary search is a search algorithm that finds the position of a target value within a sorted array.
  • 5.
    How Binary searchwork It work only for Sorted Array. At each level the Array is halved and the key is search in any one of the half while the other half is rejected. Best case occurs when we the key we are looking for is Present in the middle of the array. Worst case occurs when the key is not present. Lets see a Example how Binary search Works
  • 6.
    How Binary SearchWork value -4 2 7 10 15 20 22 25 30 36 Index 0 1 2 3 4 5 6 7 8 9 min max Here, min=0 and Max=9 Now, Mid=(min+max)/2; =(0+9)/2 = 4.5 we get floor value 4 here mid Example: Searching the array below for the value 20. Now, Compare index[mid] with 20 here, index[4] = 15, Where 15<20 Now Ignore the ( left side ) from mid.
  • 7.
    How Binary SearchWork min max Here, min= 5 and Max = 9 Now, Mid=(min+max)/2; =(5+9)/2 = 7 Example: Searching the array below for the value 20. Now, Compare index[mid] with 20 mid Here, Index[7] = 25, Where 25>20 Now Ignore the ( Right side ) from mid. value 20 22 25 30 36 Index 5 6 7 8 9
  • 8.
    How Binary SearchWork min max Here, min= 5 and Max= 6 Now, Mid=(min+max)/2; =(5+6)/2 = 5 Example: Searching the array below for the value 20. Now, Compare index[mid] with 20 mid Here, Index[5] = 20 => Where 20 ==20 At last we get our Target value (20) from this array value 20 22 Index 5 6
  • 9.
    Flow Chart ofBinary Search Start Lb=0 Ub = size of array Lb<=Ub? yes No Mid=(Lb+Ub)/2 Key Not Found! Array[mid]==key?Key Is Found yes Array [mid] > key? yes Lb=mid+1 No Ub=mid-1
  • 10.
  • 11.
    Why Binary searchis more Better then Linear search. 1.Linear search follows sequence and Binary search doesn’t follow. 2.Linear search starts searching from the starting to ending point. Binary searching starts from middle point. 3.Space complexity of Linear Search is O(1) and Binary Search is O(logn). In this case Binary Search is better than Linear Search.
  • 12.
    Let’s see theBinary Search Code In (C program)