SlideShare a Scribd company logo
Binary Search
Efficient Searching Algorithm
Introduction
โ€ข Binary search is a divide-and-conquer algorithm used for searching in
a sorted array.
โ€ข It efficiently finds the position of a target value within the array.
How Binary Search Works
โ€ข Binary search starts by comparing the target value with the middle
element of the array.
โ€ข If the middle element is the target, the search is successful.
โ€ข If the target is smaller, the search continues on the left half of the
array.
โ€ข If the target is larger, the search continues on the right half of the
array.
โ€ข This process is repeated until the target is found or the search range
becomes empty.
Algorithm Steps
โ€ข Start with a sorted array and a target value.
โ€ข Set the search range to the entire array.
โ€ข Calculate the middle index of the search range.
โ€ข Compare the middle element with the target value.
โ€ข If they are equal, the search is successful.
โ€ข If the target is smaller, update the search range to the left half of the array
and go to step 3.
โ€ข If the target is larger, update the search range to the right half of the array
and go to step 3.
โ€ข Repeat steps 3-7 until the target is found or the search range is empty.
Implementation
โ€ข It can be implemented using iteration as well as recursion
Algorithm Steps
int binarySearch(int array[], int x, int start, int end) {
// Iterative Binary Search
// Repeat until the pointers start and end meet each other
while (start <= end) {
int mid = (start + start) / 2;
if (array[mid] == x)
return mid;
Algorithm Steps
if (array[mid] < x)
start = mid + 1;
else
end = mid - 1;
}
return -1;
}
Algorithm Steps
// Recursive Binary Search
int binarySearchRecursive(int arr[], int left, int right, int target) {
if (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
else if (arr[mid] > target)
return binarySearchRecursive(arr, left, mid - 1, target);
else
return binarySearchRecursive(arr, mid + 1, right, target);
}
return -1;
}
Time Complexity
โ€ข Binary search has a time complexity of O(log n), where n is the
number of elements in the array.
โ€ข It reduces the search space by half in each iteration, leading to a
logarithmic time complexity.
Advantages of Binary Search
โ€ข Efficient searching algorithm for sorted arrays.
โ€ข Works well even with large datasets.
โ€ข Requires fewer comparisons compared to linear search.
Limitations of Binary Search
โ€ข Requires a sorted array as input.
โ€ข Insertion and deletion operations are costly since the array needs to
be maintained in a sorted order.
โ€ข Binary search is not suitable for unsorted arrays.
Use Cases
โ€ข Searching for an element in a phonebook.
โ€ข Finding a specific word in a dictionary.
โ€ข Implementing autocomplete functionality.
Conclusion
โ€ข Binary search is a powerful algorithm for efficient searching in sorted
arrays.
โ€ข It reduces the search space in each iteration, resulting in a logarithmic
time complexity.
โ€ข Understanding binary search can help optimize search operations in
various applications.

More Related Content

Similar to an Introduction to binary search algorithm (20)

PPTX
data_structure_Chapter two_computer.pptx
Mohammed472103
ย 
PPTX
Chapter 2 Sorting and Searching .pptx.soft
kuruabeje7
ย 
PDF
Ocw chp6 2searchbinary
Prashant Rai
ย 
PPTX
Presentation
zohaib arif
ย 
PDF
DSA Lec 5+6(Search+Sort) (1).pdf
MustafaJutt4
ย 
PPTX
All Searching and Sorting Techniques in Data Structures
sonalishinge2015
ย 
PPTX
Chapter 2. data structure and algorithm
SolomonEndalu
ย 
PPTX
Searching and Sorting Algorithms in Data Structures
poongothai11
ย 
PPTX
searching techniques.pptx
Dr.Shweta
ย 
PPTX
Binary search Algorithm
FazalRehman79
ย 
PPTX
Analysis of Algorithm - Binary Search.pptx
Maulana Abul Kalam Azad University of Technology
ย 
PPTX
Parallel Sorting Algorithms. Quicksort. Merge sort. List Ranking
SukhrobAtoev2
ย 
PPTX
Data analysis and algorithms - UNIT 2.pptx
sgrishma559
ย 
PPTX
Module_1 Linear search search and Bsearch).pptx
vidhyapm2
ย 
PPT
Unit iv(dsc++)
Durga Devi
ย 
PPTX
Algorithm 8th lecture linear & binary search(2).pptx
Aftabali702240
ย 
ODP
Data operatons & searching and sorting algorithms
Anushdika Jeganathan
ย 
PPT
Chapter 11 - Sorting and Searching
Eduardo Bergavera
ย 
PPT
(Data Structure) Chapter11 searching & sorting
Fadhil Ismail
ย 
data_structure_Chapter two_computer.pptx
Mohammed472103
ย 
Chapter 2 Sorting and Searching .pptx.soft
kuruabeje7
ย 
Ocw chp6 2searchbinary
Prashant Rai
ย 
Presentation
zohaib arif
ย 
DSA Lec 5+6(Search+Sort) (1).pdf
MustafaJutt4
ย 
All Searching and Sorting Techniques in Data Structures
sonalishinge2015
ย 
Chapter 2. data structure and algorithm
SolomonEndalu
ย 
Searching and Sorting Algorithms in Data Structures
poongothai11
ย 
searching techniques.pptx
Dr.Shweta
ย 
Binary search Algorithm
FazalRehman79
ย 
Analysis of Algorithm - Binary Search.pptx
Maulana Abul Kalam Azad University of Technology
ย 
Parallel Sorting Algorithms. Quicksort. Merge sort. List Ranking
SukhrobAtoev2
ย 
Data analysis and algorithms - UNIT 2.pptx
sgrishma559
ย 
Module_1 Linear search search and Bsearch).pptx
vidhyapm2
ย 
Unit iv(dsc++)
Durga Devi
ย 
Algorithm 8th lecture linear & binary search(2).pptx
Aftabali702240
ย 
Data operatons & searching and sorting algorithms
Anushdika Jeganathan
ย 
Chapter 11 - Sorting and Searching
Eduardo Bergavera
ย 
(Data Structure) Chapter11 searching & sorting
Fadhil Ismail
ย 

Recently uploaded (20)

PPTX
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
ย 
PPTX
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3
ย 
PDF
From Data Preparation to Inference: How Alluxio Speeds Up AI
Alluxio, Inc.
ย 
PPTX
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
ย 
PDF
What Is an Internal Quality Audit and Why It Matters for Your QMS
BizPortals365
ย 
PPTX
Introduction to web development | MERN Stack
JosephLiyon
ย 
PDF
Humans vs AI Call Agents - Qcall.ai's Special Report
Udit Goenka
ย 
PPTX
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
ย 
PPTX
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
ย 
PPTX
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
ย 
PDF
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
ย 
PDF
Mastering VPC Architecture Build for Scale from Day 1.pdf
Devseccops.ai
ย 
PDF
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
ย 
PPTX
arctitecture application system design os dsa
za241967
ย 
PPTX
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
ย 
PDF
The Next-Gen HMIS Software AI, Blockchain & Cloud for Housing.pdf
Prudence B2B
ย 
PDF
Why Edge Computing Matters in Mobile Application Tech.pdf
IMG Global Infotech
ย 
PDF
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
ย 
PPTX
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
ย 
PDF
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
ย 
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
ย 
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3
ย 
From Data Preparation to Inference: How Alluxio Speeds Up AI
Alluxio, Inc.
ย 
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
ย 
What Is an Internal Quality Audit and Why It Matters for Your QMS
BizPortals365
ย 
Introduction to web development | MERN Stack
JosephLiyon
ย 
Humans vs AI Call Agents - Qcall.ai's Special Report
Udit Goenka
ย 
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
ย 
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
ย 
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
ย 
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
ย 
Mastering VPC Architecture Build for Scale from Day 1.pdf
Devseccops.ai
ย 
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
ย 
arctitecture application system design os dsa
za241967
ย 
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
ย 
The Next-Gen HMIS Software AI, Blockchain & Cloud for Housing.pdf
Prudence B2B
ย 
Why Edge Computing Matters in Mobile Application Tech.pdf
IMG Global Infotech
ย 
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
ย 
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
ย 
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
ย 
Ad

an Introduction to binary search algorithm

  • 2. Introduction โ€ข Binary search is a divide-and-conquer algorithm used for searching in a sorted array. โ€ข It efficiently finds the position of a target value within the array.
  • 3. How Binary Search Works โ€ข Binary search starts by comparing the target value with the middle element of the array. โ€ข If the middle element is the target, the search is successful. โ€ข If the target is smaller, the search continues on the left half of the array. โ€ข If the target is larger, the search continues on the right half of the array. โ€ข This process is repeated until the target is found or the search range becomes empty.
  • 4. Algorithm Steps โ€ข Start with a sorted array and a target value. โ€ข Set the search range to the entire array. โ€ข Calculate the middle index of the search range. โ€ข Compare the middle element with the target value. โ€ข If they are equal, the search is successful. โ€ข If the target is smaller, update the search range to the left half of the array and go to step 3. โ€ข If the target is larger, update the search range to the right half of the array and go to step 3. โ€ข Repeat steps 3-7 until the target is found or the search range is empty.
  • 5. Implementation โ€ข It can be implemented using iteration as well as recursion
  • 6. Algorithm Steps int binarySearch(int array[], int x, int start, int end) { // Iterative Binary Search // Repeat until the pointers start and end meet each other while (start <= end) { int mid = (start + start) / 2; if (array[mid] == x) return mid;
  • 7. Algorithm Steps if (array[mid] < x) start = mid + 1; else end = mid - 1; } return -1; }
  • 8. Algorithm Steps // Recursive Binary Search int binarySearchRecursive(int arr[], int left, int right, int target) { if (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; else if (arr[mid] > target) return binarySearchRecursive(arr, left, mid - 1, target); else return binarySearchRecursive(arr, mid + 1, right, target); } return -1; }
  • 9. Time Complexity โ€ข Binary search has a time complexity of O(log n), where n is the number of elements in the array. โ€ข It reduces the search space by half in each iteration, leading to a logarithmic time complexity.
  • 10. Advantages of Binary Search โ€ข Efficient searching algorithm for sorted arrays. โ€ข Works well even with large datasets. โ€ข Requires fewer comparisons compared to linear search.
  • 11. Limitations of Binary Search โ€ข Requires a sorted array as input. โ€ข Insertion and deletion operations are costly since the array needs to be maintained in a sorted order. โ€ข Binary search is not suitable for unsorted arrays.
  • 12. Use Cases โ€ข Searching for an element in a phonebook. โ€ข Finding a specific word in a dictionary. โ€ข Implementing autocomplete functionality.
  • 13. Conclusion โ€ข Binary search is a powerful algorithm for efficient searching in sorted arrays. โ€ข It reduces the search space in each iteration, resulting in a logarithmic time complexity. โ€ข Understanding binary search can help optimize search operations in various applications.