SlideShare a Scribd company logo
1 of 13
Binary Search
☐ Name : A.S.M Sabbir Ahmed
☐ Roll No. :2110676154
☐ Semester : 4th
☐ Year : 2024
Introduction
☐ The Binary Search can be implemented on only
sorted list of elements.
The process starts with finding the middle of interval
and comparing the key with it (mid point), if the value
of the key is less than it then the search interval
reduces up to mid point and if the value of the key is
greater than it then we search the key from mid point
to the end of interval.
☐
Introduction
☐ Binary search, also known as half-interval
search or logarithmic search, is a search
algorithm that finds the position of a target value
within a sorted array.
☐ Binary search runs in at worst logarithmic time ,
making {O(log n)} comparisons, where {n} is the
number of elements in the array and {log } is
the binary logarithm ; and using only
constant {(O(1))}space.
Algorithm
Binary Search ( A[0 1 2 3…….n-1] , key)
low 0
high n-1
while(low <= high)
do
{
m (low + high)/2
if (key=A[m])
then
Algorithm
return m
else if (key<A[m])
then
high m-1
else
low m+1
}
Example
☐ Let the number givens be 4,6,7,9,10 and the key is 9.
So , according to the algorithm these numbers are to
be stored in a 1D array named A[0 1 2….n-1];where
n= number of elements i.e. in this example 5.
Thus according to the algorithm A[0 1 2 3 4] is
generated and key value is passed in the array that is
9.
☐
☐
Binary Search( A[0 1 2 3 4] , 9 )
low=0
high=4
while(0<=4)
Example
Iteration 1:
m=2
low=3 //here key=9 is greater than A[m]=7 ,thus it
executes last else condition .
Since,(3<4) loop will continue.
Iteration 2:
m=3
return 3;//Since key=9 is equal to A[m]=9 , where m=3
, it returns 3 and thus ends its binary search
here by returning the index where the key
value is stored.
Use Case
☐ Suppose we have an array A and in this array we are
searching for a value K. If A has no special properties,
then there is no better way to search K than linear
search -- to start at the beginning and go through the
array one step at a time, comparing each element to K
in turn. The time it takes (on average, and in the worst
case) is linear, or O(N), to the number of items of
array.
But if A is a sorted array, there is a much faster way,
Binary Search, to look for K. In binary search, after
each iteration, the length of the array we are looking in
gets cut in half.
☐
Space Analysis
☐ Binary Search can be done using recursion and
Iteration .
If we continuously divide the interval into two halves
then we can do it at most log(n) time . So we can say
the number of comparision is at most log(n).So the
number of time recursion will be done is
log(n).So,recursive space complexity is log(n).But In
case of iteration we need only three variables high,
mid and low.So in that case complexity is ~O(1).
☐
Time Analysis
☐ Best case - O (1) comparisons : In the best case, the
item X is the middle in the array A. A constant number
of comparisons (actually just 1) are required.
☐ Worst case - O (log n) comparisons : In the worst
case, the item X does not exist in the array A at all.
Through each comparision of Binary Search, the size
of the admissible range is halved. This halving can
be done ceiling(log n ) times. Thus, ceiling(log n )
comparisons are required.
Time Analysis
☐ Average case - O (log n) comparisons : To find the
average case, take the sum over all elements of the
product of number of comparisons required to find
each element and the probability of searching for that
element. To simplify the analysis, assume that no item
which is not in A will be searched for, and that the
probabilities of searching for each element are
uniform.
Advantage and Disadvantage
☐ Advantage:
1. Binary search is an optimal searching algorithm using
which we can search desired element very efficiently.
☐ Disadvantage:
1. This algorithm requires the list to be sorted . Then
only this method is applicable.
Application
1. The binary search is an efficient searching method
and is used to search desired record from database.
2. For solving nonlinear equations with one unknown
this method is used.
Thank-You

More Related Content

Similar to Binary.pptx

27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
AyanMandal44
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
guest2cb109
 
Selection sort
Selection sortSelection sort
Selection sort
asra khan
 

Similar to Binary.pptx (20)

Linear Search
Linear SearchLinear Search
Linear Search
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
27631722026_T._TAJESWAR_RAO_PCC-CS301_CSE(CS).pptx
 
Demonstrate interpolation search
Demonstrate interpolation searchDemonstrate interpolation search
Demonstrate interpolation search
 
Searching Algorithms for students of CS and IT using C++
Searching Algorithms for students of CS and IT using C++Searching Algorithms for students of CS and IT using C++
Searching Algorithms for students of CS and IT using C++
 
Searching.pptx
Searching.pptxSearching.pptx
Searching.pptx
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Search techniques and Hashing
Search techniques and HashingSearch techniques and Hashing
Search techniques and Hashing
 
Lecture3b searching
Lecture3b searchingLecture3b searching
Lecture3b searching
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 
Searching and Sorting Algorithms
Searching and Sorting AlgorithmsSearching and Sorting Algorithms
Searching and Sorting Algorithms
 
Binary search algorithm.pptx
Binary search  algorithm.pptxBinary search  algorithm.pptx
Binary search algorithm.pptx
 
Binary search
Binary searchBinary search
Binary search
 
Searching
Searching Searching
Searching
 
Selection sort
Selection sortSelection sort
Selection sort
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
 
Non-Uniform Gap Distribution Library Sort
Non-Uniform Gap Distribution Library SortNon-Uniform Gap Distribution Library Sort
Non-Uniform Gap Distribution Library Sort
 

Recently uploaded

INTERRUPT CONTROLLER 8259 MICROPROCESSOR
INTERRUPT CONTROLLER 8259 MICROPROCESSORINTERRUPT CONTROLLER 8259 MICROPROCESSOR
INTERRUPT CONTROLLER 8259 MICROPROCESSOR
TanishkaHira1
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptx
hublikarsn
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 

Recently uploaded (20)

INTERRUPT CONTROLLER 8259 MICROPROCESSOR
INTERRUPT CONTROLLER 8259 MICROPROCESSORINTERRUPT CONTROLLER 8259 MICROPROCESSOR
INTERRUPT CONTROLLER 8259 MICROPROCESSOR
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Introduction to Geographic Information Systems
Introduction to Geographic Information SystemsIntroduction to Geographic Information Systems
Introduction to Geographic Information Systems
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Databricks Generative AI Fundamentals .pdf
Databricks Generative AI Fundamentals  .pdfDatabricks Generative AI Fundamentals  .pdf
Databricks Generative AI Fundamentals .pdf
 
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
 
Path loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelPath loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata Model
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptx
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Post office management system project ..pdf
Post office management system project ..pdfPost office management system project ..pdf
Post office management system project ..pdf
 
Adsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptAdsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) ppt
 
Computer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesComputer Graphics Introduction To Curves
Computer Graphics Introduction To Curves
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Danikor Product Catalog- Screw Feeder.pdf
Danikor Product Catalog- Screw Feeder.pdfDanikor Product Catalog- Screw Feeder.pdf
Danikor Product Catalog- Screw Feeder.pdf
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 

Binary.pptx

  • 1. Binary Search ☐ Name : A.S.M Sabbir Ahmed ☐ Roll No. :2110676154 ☐ Semester : 4th ☐ Year : 2024
  • 2. Introduction ☐ The Binary Search can be implemented on only sorted list of elements. The process starts with finding the middle of interval and comparing the key with it (mid point), if the value of the key is less than it then the search interval reduces up to mid point and if the value of the key is greater than it then we search the key from mid point to the end of interval. ☐
  • 3. Introduction ☐ Binary search, also known as half-interval search or logarithmic search, is a search algorithm that finds the position of a target value within a sorted array. ☐ Binary search runs in at worst logarithmic time , making {O(log n)} comparisons, where {n} is the number of elements in the array and {log } is the binary logarithm ; and using only constant {(O(1))}space.
  • 4. Algorithm Binary Search ( A[0 1 2 3…….n-1] , key) low 0 high n-1 while(low <= high) do { m (low + high)/2 if (key=A[m]) then
  • 5. Algorithm return m else if (key<A[m]) then high m-1 else low m+1 }
  • 6. Example ☐ Let the number givens be 4,6,7,9,10 and the key is 9. So , according to the algorithm these numbers are to be stored in a 1D array named A[0 1 2….n-1];where n= number of elements i.e. in this example 5. Thus according to the algorithm A[0 1 2 3 4] is generated and key value is passed in the array that is 9. ☐ ☐ Binary Search( A[0 1 2 3 4] , 9 ) low=0 high=4 while(0<=4)
  • 7. Example Iteration 1: m=2 low=3 //here key=9 is greater than A[m]=7 ,thus it executes last else condition . Since,(3<4) loop will continue. Iteration 2: m=3 return 3;//Since key=9 is equal to A[m]=9 , where m=3 , it returns 3 and thus ends its binary search here by returning the index where the key value is stored.
  • 8. Use Case ☐ Suppose we have an array A and in this array we are searching for a value K. If A has no special properties, then there is no better way to search K than linear search -- to start at the beginning and go through the array one step at a time, comparing each element to K in turn. The time it takes (on average, and in the worst case) is linear, or O(N), to the number of items of array. But if A is a sorted array, there is a much faster way, Binary Search, to look for K. In binary search, after each iteration, the length of the array we are looking in gets cut in half. ☐
  • 9. Space Analysis ☐ Binary Search can be done using recursion and Iteration . If we continuously divide the interval into two halves then we can do it at most log(n) time . So we can say the number of comparision is at most log(n).So the number of time recursion will be done is log(n).So,recursive space complexity is log(n).But In case of iteration we need only three variables high, mid and low.So in that case complexity is ~O(1). ☐
  • 10. Time Analysis ☐ Best case - O (1) comparisons : In the best case, the item X is the middle in the array A. A constant number of comparisons (actually just 1) are required. ☐ Worst case - O (log n) comparisons : In the worst case, the item X does not exist in the array A at all. Through each comparision of Binary Search, the size of the admissible range is halved. This halving can be done ceiling(log n ) times. Thus, ceiling(log n ) comparisons are required.
  • 11. Time Analysis ☐ Average case - O (log n) comparisons : To find the average case, take the sum over all elements of the product of number of comparisons required to find each element and the probability of searching for that element. To simplify the analysis, assume that no item which is not in A will be searched for, and that the probabilities of searching for each element are uniform.
  • 12. Advantage and Disadvantage ☐ Advantage: 1. Binary search is an optimal searching algorithm using which we can search desired element very efficiently. ☐ Disadvantage: 1. This algorithm requires the list to be sorted . Then only this method is applicable.
  • 13. Application 1. The binary search is an efficient searching method and is used to search desired record from database. 2. For solving nonlinear equations with one unknown this method is used. Thank-You