0% found this document useful (0 votes)
2 views4 pages

Binary Search Algorithm

The document explains the Binary Search Algorithm, a searching technique that efficiently finds an element in a sorted list using a divide and conquer approach. It outlines the algorithm's steps and provides implementations in both C and C++ programming languages. The document also emphasizes the importance of having a sorted list for the binary search to function correctly.

Uploaded by

Rama Krishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Binary Search Algorithm

The document explains the Binary Search Algorithm, a searching technique that efficiently finds an element in a sorted list using a divide and conquer approach. It outlines the algorithm's steps and provides implementations in both C and C++ programming languages. The document also emphasizes the importance of having a sorted list for the binary search to function correctly.

Uploaded by

Rama Krishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Binary Search Algorithm

In this article, we will discuss the Binary Search Algorithm. Searching is the process of finding some particular
element in the list. If the element is present in the list, then the process is called successful, and the process returns
the location of that element. Otherwise, the search is called unsuccessful.
Linear Search and Binary Search are the two popular searching techniques. Here we will discuss the Binary Search
Algorithm.
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.
Algorithm
1. Binary_Search(a, lower_bound, upper_bound, val) // 'a' is the given array, 'lower_bound' is the index of th
e first array element, 'upper_bound' is the index of the last array element, 'val' is the value to search
2. Step 1: set beg = lower_bound, end = upper_bound, pos = - 1
3. Step 2: repeat steps 3 and 4 while beg <=end
4. Step 3: set mid = (beg + end)/2
5. Step 4: if a[mid] = val
6. set pos = mid
7. print pos
8. go to step 6
9. else if a[mid] > val
10. set end = mid - 1
11. else
12. set beg = mid + 1
13. [end of if]
14. [end of loop]
15. Step 5: if pos = -1
16. print "value is not present in the array"
17. [end of if]
18. Step 6: exit
Working of Binary search
Now, let's see the working of the Binary Search Algorithm.
To understand the working of the Binary search algorithm, let's take a sorted array. It will be easy to
understand the working of Binary search with an example.
There are two methods to implement the binary search algorithm -
o Iterative method
o 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


We have to use the below formula to calculate the mid of the array -
1. 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.
Implementation of Binary Search
Now, let's see the programs of Binary search in different programming languages.
Program: Write a program to implement Binary search in C language.
1. #include <stdio.h>
2. int binarySearch(int a[], int beg, int end, int val)
3. {
4. int mid;
5. if(end >= beg)
6. { mid = (beg + end)/2;
7. /* if the item to be searched is present at middle */
8. if(a[mid] == val)
9. {
10. return mid+1;
11. }
12. /* if the item to be searched is smaller than middle, then it can only be in left subarray */
13. else if(a[mid] < val)
14. {
15. return binarySearch(a, mid+1, end, val);
16. }
17. /* if the item to be searched is greater than middle, then it can only be in right subarray */
18. else
19. {
20. return binarySearch(a, beg, mid-1, val);
21. }
22. }
23. return -1;
24. }
25. int main() {
26. int a[] = {11, 14, 25, 30, 40, 41, 52, 57, 70}; // given array
27. int val = 40; // value to be searched
28. int n = sizeof(a) / sizeof(a[0]); // size of array
29. int res = binarySearch(a, 0, n-1, val); // Store result
30. printf("The elements of the array are - ");
31. for (int i = 0; i < n; i++)
32. printf("%d ", a[i]);
33. printf("\nElement to be searched is - %d", val);
34. if (res == -1)
35. printf("\nElement is not present in the array");
36. else
37. printf("\nElement is present at %d position of array", res);
38. return 0;
39. }
Output

Program: Write a program to implement Binary search in C++.


1. #include <iostream>
2. using namespace std;
3. int binarySearch(int a[], int beg, int end, int val)
4. {
5. int mid;
6. if(end >= beg)
7. {
8. mid = (beg + end)/2;
9. /* if the item to be searched is present at middle */
10. if(a[mid] == val)
11. {
12. return mid+1;
13. }
14. /* if the item to be searched is smaller than middle, then it can only be in left subarray */
15. else if(a[mid] < val)
16. {
17. return binarySearch(a, mid+1, end, val);
18. }
19. /* if the item to be searched is greater than middle, then it can only be in right subarray */
20. else
21. {
22. return binarySearch(a, beg, mid-1, val);
23. }
24. }
25. return -1;
26. }
27. int main() {
28. int a[] = {10, 12, 24, 29, 39, 40, 51, 56, 70}; // given array
29. int val = 51; // value to be searched
30. int n = sizeof(a) / sizeof(a[0]); // size of array
31. int res = binarySearch(a, 0, n-1, val); // Store result
32. cout<<"The elements of the array are - ";
33. for (int i = 0; i < n; i++)
34. cout<<a[i]<<" ";
35. cout<<"\nElement to be searched is - "<<val;
36. if (res == -1)
37. cout<<"\nElement is not present in the array";
38. else
39. cout<<"\nElement is present at "<<res<<" position of array";
40. return 0;
41. }
Output

You might also like