SlideShare a Scribd company logo
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.
Binary_Search(a, lower_bound, upper_bound, val) // 'a' is the
given array, 'lower_bound' is the index of the first array element, 'upper_bound' is
the index of the last array element, 'val' is the value to search
Step 1: set beg = lower_bound, end = upper_bound, pos = - 1
Step 2: repeat steps 3 and 4 while beg <=end
Step 3: set mid = (beg + end)/2
Step 4: if a[mid] = val
set pos = mid
print pos
go to step 6
else if a[mid] > val
set end = mid - 1
else
set beg = mid + 1
[end of if]
[end of loop]
Step 5: if pos = -1
print "value is not present in the array"
[end of if]
Step 6: exit
Working of Binary search
There are two methods to implement the binary search
algorithm -
■ Iterative method
■ Recursive method
The recursive method of binary search follows the divide
and conquer approach.
Let the elements of array are -
Let the element to search is, K =
56
To calculate the mid of the array -
mid = (beg + end)/2
So, in the given array -
beg = 0
end = 8
mid = (0 + 8)/2 = 4. So, 4 is the mid of the array.
Now, the element to search is found. So algorithm will return
the index of the element matched.
Binary search in C language
#include <stdio.h>
int binarySearch(int a[], int beg, int end, int val)
{
int mid;
if(end >= beg)
{
mid = (beg + end)/2;
if(a[mid] == val)
{
return mid+1;
}
else if(a[mid] < val)
{
return binarySearch(a, mid+1, end, val);
}
else
{
return binarySearch(a, beg, mid-1, val);
}
}
return -1;
}
int main() {
int a[] = {11, 14, 25, 30, 40, 41, 52, 57, 70};
int val = 40;
int n = sizeof(a) / sizeof(a[0]);
int res = binarySearch(a, 0, n-1, val);
printf("The elements of the array are - ");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("nElement to be searched is - %d", val);
if (res == -1)
printf("nElement is not present in the array");
else
printf("nElement is present at %d position of array", res);
return 0;
}
Binary search in Java language
class BinarySearch {
static int binarySearch(int a[], int beg, int end, int val)
{
int mid;
if(end >= beg)
{
mid = (beg + end)/2;
if(a[mid] == val)
{
return mid+1;
}
else if(a[mid] < val)
{
return binarySearch(a, mid+1, end, val);
}
else
{
return binarySearch(a, beg, mid-1, val);
}
}
return -1;
}
public static void main(String args[]) {
int a[] = {8, 10, 22, 27, 37, 44, 49, 55, 69};
int val = 37;
int n = a.length;
int res = binarySearch(a, 0, n-1, val);
System.out.print("The elements of the array are: ");
for (int i = 0; i < n; i++)
{
System.out.print(a[i] + " ");
}
System.out.println();
System.out.println("Element to be searched is: " + val);
if (res == -1)
System.out.println("Element is not present in the array");
else
System.out.println("Element is present at " + res + " position of
array");
}
}

More Related Content

Similar to Binary Searching Algorithm PowerPoint Presentation (20)

PDF
Data structures arrays
maamir farooq
 
PDF
Data structures arrays
maamir farooq
 
PDF
Linear and Binary Search
WinNie Sjr
 
PDF
Linear and Binary Search
WinNie Sjr
 
DOCX
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Sitamarhi Institute of Technology
 
DOCX
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Sitamarhi Institute of Technology
 
PPTX
ARRAY in python and c with examples .pptx
abhishekmaurya102515
 
PPTX
ARRAY in python and c with examples .pptx
abhishekmaurya102515
 
PPTX
Searching in DSA that follow a dsa searching.pptx
StephenRobert15
 
PPTX
Searching in DSA that follow a dsa searching.pptx
StephenRobert15
 
PPTX
Linear and Binary search .pptx
p83629918
 
PPTX
Linear and Binary search .pptx
p83629918
 
PPT
Unit6 jwfiles
mrecedu
 
PPT
Unit6 jwfiles
mrecedu
 
PPTX
searching in data structure.pptx
chouguleamruta24
 
PPTX
searching in data structure.pptx
chouguleamruta24
 
PPTX
Sorting and searching arrays binary search algorithm
David Burks-Courses
 
PPTX
Sorting and searching arrays binary search algorithm
David Burks-Courses
 
Data structures arrays
maamir farooq
 
Data structures arrays
maamir farooq
 
Linear and Binary Search
WinNie Sjr
 
Linear and Binary Search
WinNie Sjr
 
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Sitamarhi Institute of Technology
 
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Sitamarhi Institute of Technology
 
ARRAY in python and c with examples .pptx
abhishekmaurya102515
 
ARRAY in python and c with examples .pptx
abhishekmaurya102515
 
Searching in DSA that follow a dsa searching.pptx
StephenRobert15
 
Searching in DSA that follow a dsa searching.pptx
StephenRobert15
 
Linear and Binary search .pptx
p83629918
 
Linear and Binary search .pptx
p83629918
 
Unit6 jwfiles
mrecedu
 
Unit6 jwfiles
mrecedu
 
searching in data structure.pptx
chouguleamruta24
 
searching in data structure.pptx
chouguleamruta24
 
Sorting and searching arrays binary search algorithm
David Burks-Courses
 
Sorting and searching arrays binary search algorithm
David Burks-Courses
 

Recently uploaded (20)

PPTX
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
 
PPTX
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3
 
PDF
Which Hiring Management Tools Offer the Best ROI?
HireME
 
PDF
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
Shane Coughlan
 
PDF
Best Software Development at Best Prices
softechies7
 
PDF
Mastering VPC Architecture Build for Scale from Day 1.pdf
Devseccops.ai
 
PDF
The Next-Gen HMIS Software AI, Blockchain & Cloud for Housing.pdf
Prudence B2B
 
PDF
Rewards and Recognition (2).pdf
ethan Talor
 
PDF
IObit Uninstaller Pro 14.3.1.8 Crack for Windows Latest
utfefguu
 
PDF
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
 
PPTX
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
pcprocore
 
DOCX
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
SEOLIFT - SEO Company London
 
PDF
Code Once; Run Everywhere - A Beginner’s Journey with React Native
Hasitha Walpola
 
PDF
Automated Testing and Safety Analysis of Deep Neural Networks
Lionel Briand
 
PPTX
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
 
PPTX
Introduction to web development | MERN Stack
JosephLiyon
 
PPTX
For my supp to finally picking supp that work
necas19388
 
PDF
Building scalbale cloud native apps with .NET 8
GillesMathieu10
 
PDF
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
PDF
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
 
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
 
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3
 
Which Hiring Management Tools Offer the Best ROI?
HireME
 
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
Shane Coughlan
 
Best Software Development at Best Prices
softechies7
 
Mastering VPC Architecture Build for Scale from Day 1.pdf
Devseccops.ai
 
The Next-Gen HMIS Software AI, Blockchain & Cloud for Housing.pdf
Prudence B2B
 
Rewards and Recognition (2).pdf
ethan Talor
 
IObit Uninstaller Pro 14.3.1.8 Crack for Windows Latest
utfefguu
 
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
 
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
pcprocore
 
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
SEOLIFT - SEO Company London
 
Code Once; Run Everywhere - A Beginner’s Journey with React Native
Hasitha Walpola
 
Automated Testing and Safety Analysis of Deep Neural Networks
Lionel Briand
 
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
 
Introduction to web development | MERN Stack
JosephLiyon
 
For my supp to finally picking supp that work
necas19388
 
Building scalbale cloud native apps with .NET 8
GillesMathieu10
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
 
Ad

Binary Searching Algorithm PowerPoint Presentation

  • 1. 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.
  • 2. Binary_Search(a, lower_bound, upper_bound, val) // 'a' is the given array, 'lower_bound' is the index of the first array element, 'upper_bound' is the index of the last array element, 'val' is the value to search Step 1: set beg = lower_bound, end = upper_bound, pos = - 1 Step 2: repeat steps 3 and 4 while beg <=end Step 3: set mid = (beg + end)/2 Step 4: if a[mid] = val set pos = mid print pos go to step 6 else if a[mid] > val set end = mid - 1 else set beg = mid + 1 [end of if] [end of loop] Step 5: if pos = -1 print "value is not present in the array" [end of if] Step 6: exit
  • 3. Working of Binary search There are two methods to implement the binary search algorithm - ■ Iterative method ■ Recursive method The recursive method of binary search follows the divide and conquer approach. Let the elements of array are - Let the element to search is, K = 56
  • 4. To calculate the mid of the array - mid = (beg + end)/2 So, in the given array - beg = 0 end = 8 mid = (0 + 8)/2 = 4. So, 4 is the mid of the array.
  • 5. Now, the element to search is found. So algorithm will return the index of the element matched.
  • 6. Binary search in C language #include <stdio.h> int binarySearch(int a[], int beg, int end, int val) { int mid; if(end >= beg) { mid = (beg + end)/2; if(a[mid] == val) { return mid+1; } else if(a[mid] < val) { return binarySearch(a, mid+1, end, val); } else { return binarySearch(a, beg, mid-1, val); } } return -1; }
  • 7. int main() { int a[] = {11, 14, 25, 30, 40, 41, 52, 57, 70}; int val = 40; int n = sizeof(a) / sizeof(a[0]); int res = binarySearch(a, 0, n-1, val); printf("The elements of the array are - "); for (int i = 0; i < n; i++) printf("%d ", a[i]); printf("nElement to be searched is - %d", val); if (res == -1) printf("nElement is not present in the array"); else printf("nElement is present at %d position of array", res); return 0; }
  • 8. Binary search in Java language class BinarySearch { static int binarySearch(int a[], int beg, int end, int val) { int mid; if(end >= beg) { mid = (beg + end)/2; if(a[mid] == val) { return mid+1; } else if(a[mid] < val) { return binarySearch(a, mid+1, end, val); } else { return binarySearch(a, beg, mid-1, val); } } return -1; }
  • 9. public static void main(String args[]) { int a[] = {8, 10, 22, 27, 37, 44, 49, 55, 69}; int val = 37; int n = a.length; int res = binarySearch(a, 0, n-1, val); System.out.print("The elements of the array are: "); for (int i = 0; i < n; i++) { System.out.print(a[i] + " "); } System.out.println(); System.out.println("Element to be searched is: " + val); if (res == -1) System.out.println("Element is not present in the array"); else System.out.println("Element is present at " + res + " position of array"); } }