SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
Binary Search Algorithm
By : Bhagyashree Madan
Introduction
 A Binary search algorithm finds the position of a
specified input value (the search "key") within a
sorted array .
 For binary search, the array should be arranged in
ascending or descending order.
Binary search algorithm
 Algorithm is quite simple. It can be done either recursively
or iteratively:
 Get the middle element;
 If the middle element equals to the searched value, the
algorithm stops.
 Otherwise, two cases are possible:
 Searched value is less, than the middle element.
 In this case, go to the step 1 for the part of the array, before
middle element and value high=mid-1
 Searched value is greater, than the middle element. In this
case, go to the step 1 for the part of the array, after middle
element. And value of low=mid+1
 Now, The iterations should stop when searched element is
found & when sub array has no elements. In this case, we
can conclude, that searched value is not present in the
array.
Binary Search
Item to be search=37
1. Sort Array.
Binary Search
2. Calculate
Middle = int (low + high)
/2
If 37 == array[middle] 
return middle
Else if 37 < array[middle] 
high = middle -1
Else if 37 > array[middle] 
low = middle +1
 Here Mid=int(0+8)/2
=int(8/2)
Mid =4
Array[Mid]=45
37<45
Hence,high=4-1=3
Now,
Low=0 and high=3
Repeat 2. Calculate Mid =int (low + high) / 2.
= (0 + 3) / 2 = 1.
Array[mid]=35
37>35
Hence, low=mid+1=1+1=2
Now, Low=2 & high=3
Repeat 2. Calculate Mid = int(low + high) / 2.
= (2 + 3) / 2 = 2.
 Array[Mid]=37=Item
Item Found at position 2
 If not found  stop when low > high.
Analysis of binary search
 To analyze the binary search algorithm, we need to recall
that each comparison eliminates about half of the
remaining items from consideration.
 If we start with n items, about n/2 items will be left after the
first comparison. After the second comparison, there will be
about n/4. Then n/8, n/16, and so on.
 When we split the list enough times, we end up with a list
that has just one item. Either that is the item we are looking
for or it is not. Either way, we are done.
 The number of comparisons necessary to get to this point is
i where n/2^i=1. Solving for i gives us i=(log n). The
maximum number of comparisons is log n. Therefore, the
complexity of binary search is O(log n).
THANK YOU
QUESTIONS

More Related Content

PPTX
Linear Search
PPTX
Searching & Sorting Algorithms
PDF
Binary search algorithm
PPTX
Hashing Technique In Data Structures
PPTX
Insertion sort
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
PPT
Linear search in ds
PPT
Red black tree
Linear Search
Searching & Sorting Algorithms
Binary search algorithm
Hashing Technique In Data Structures
Insertion sort
Data Structures - Lecture 8 [Sorting Algorithms]
Linear search in ds
Red black tree

What's hot (20)

PPT
Chapter 11 - Sorting and Searching
PPT
Selection sort
PPTX
LINEAR SEARCH
PPTX
Heap tree
PDF
Insertion sort
PPTX
Hashing in datastructure
PPTX
Stacks IN DATA STRUCTURES
PPTX
Red black trees
PPTX
Presentation on the topic selection sort
PPTX
Linear search-and-binary-search
PPTX
Divide and conquer - Quick sort
PPTX
Binary Search Tree
PPTX
Priority Queue in Data Structure
PPTX
Quick sort
PPTX
Demonstrate interpolation search
PPT
Quick Sort
PPTX
Analysis of Algorithm - Binary Search.pptx
PPTX
Binary Tree in Data Structure
PPTX
Rahat &amp; juhith
Chapter 11 - Sorting and Searching
Selection sort
LINEAR SEARCH
Heap tree
Insertion sort
Hashing in datastructure
Stacks IN DATA STRUCTURES
Red black trees
Presentation on the topic selection sort
Linear search-and-binary-search
Divide and conquer - Quick sort
Binary Search Tree
Priority Queue in Data Structure
Quick sort
Demonstrate interpolation search
Quick Sort
Analysis of Algorithm - Binary Search.pptx
Binary Tree in Data Structure
Rahat &amp; juhith
Ad

Similar to Binary Search Algorithm.pptx (20)

PPTX
Chapter #3 (Searchinmmmg ALgorithm).pptx
PPTX
Binary search Algorithm
PDF
Ocw chp6 2searchbinary
PPTX
Binary.pptx
PPT
Binary Search
PDF
Binary Search - Design & Analysis of Algorithms
PDF
Linear and Binary Search
PPTX
linear-search-and-binary-search-170418023907.pptx
PPTX
Binary search algorithm.pptx
PPTX
Dsa – data structure and algorithms searching
DOCX
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPT
Searching
PPTX
Binary search
PPTX
Lecture_Oct26.pptx
PDF
Binary Search.pdf
PPTX
Binary Searching Algorithm PowerPoint Presentation
PDF
BINARY SEARCH - A - DATA STRUCTURE AND ALGORITHM.pdf
PPT
Linear Search & Binary Search
PPTX
Algorithm 8th lecture linear & binary search(2).pptx
Chapter #3 (Searchinmmmg ALgorithm).pptx
Binary search Algorithm
Ocw chp6 2searchbinary
Binary.pptx
Binary Search
Binary Search - Design & Analysis of Algorithms
Linear and Binary Search
linear-search-and-binary-search-170418023907.pptx
Binary search algorithm.pptx
Dsa – data structure and algorithms searching
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Searching
Binary search
Lecture_Oct26.pptx
Binary Search.pdf
Binary Searching Algorithm PowerPoint Presentation
BINARY SEARCH - A - DATA STRUCTURE AND ALGORITHM.pdf
Linear Search & Binary Search
Algorithm 8th lecture linear & binary search(2).pptx
Ad

Recently uploaded (20)

PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
Well-logging-methods_new................
PPTX
Current and future trends in Computer Vision.pptx
PPTX
Geodesy 1.pptx...............................................
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Sustainable Sites - Green Building Construction
PPTX
additive manufacturing of ss316l using mig welding
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Well-logging-methods_new................
Current and future trends in Computer Vision.pptx
Geodesy 1.pptx...............................................
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
bas. eng. economics group 4 presentation 1.pptx
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
CYBER-CRIMES AND SECURITY A guide to understanding
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
R24 SURVEYING LAB MANUAL for civil enggi
Sustainable Sites - Green Building Construction
additive manufacturing of ss316l using mig welding

Binary Search Algorithm.pptx

  • 1. Binary Search Algorithm By : Bhagyashree Madan
  • 2. Introduction  A Binary search algorithm finds the position of a specified input value (the search "key") within a sorted array .  For binary search, the array should be arranged in ascending or descending order.
  • 3. Binary search algorithm  Algorithm is quite simple. It can be done either recursively or iteratively:  Get the middle element;  If the middle element equals to the searched value, the algorithm stops.  Otherwise, two cases are possible:  Searched value is less, than the middle element.  In this case, go to the step 1 for the part of the array, before middle element and value high=mid-1  Searched value is greater, than the middle element. In this case, go to the step 1 for the part of the array, after middle element. And value of low=mid+1  Now, The iterations should stop when searched element is found & when sub array has no elements. In this case, we can conclude, that searched value is not present in the array.
  • 4. Binary Search Item to be search=37 1. Sort Array.
  • 5. Binary Search 2. Calculate Middle = int (low + high) /2 If 37 == array[middle]  return middle Else if 37 < array[middle]  high = middle -1 Else if 37 > array[middle]  low = middle +1  Here Mid=int(0+8)/2 =int(8/2) Mid =4 Array[Mid]=45 37<45 Hence,high=4-1=3 Now, Low=0 and high=3
  • 6. Repeat 2. Calculate Mid =int (low + high) / 2. = (0 + 3) / 2 = 1. Array[mid]=35 37>35 Hence, low=mid+1=1+1=2 Now, Low=2 & high=3
  • 7. Repeat 2. Calculate Mid = int(low + high) / 2. = (2 + 3) / 2 = 2.  Array[Mid]=37=Item
  • 8. Item Found at position 2
  • 9.  If not found  stop when low > high.
  • 10. Analysis of binary search  To analyze the binary search algorithm, we need to recall that each comparison eliminates about half of the remaining items from consideration.  If we start with n items, about n/2 items will be left after the first comparison. After the second comparison, there will be about n/4. Then n/8, n/16, and so on.  When we split the list enough times, we end up with a list that has just one item. Either that is the item we are looking for or it is not. Either way, we are done.  The number of comparisons necessary to get to this point is i where n/2^i=1. Solving for i gives us i=(log n). The maximum number of comparisons is log n. Therefore, the complexity of binary search is O(log n).