# Binary Search in C++

Binary Search works on sorted array, unlike linear search we saw here. It works on the concept of divide and conquer. It performs better than linear search.

Algorithm for binary search is stated below

1. To search for the key element, we divide the array into two parts.
2. Then we compare key element to the middle item.
3. If key element is more than middle element, we search the element in next part of the array.
4. Else, we search the element in the first half of the array.
5. We repeat the steps 3 and 4 till we find the element.
6. Else we give the output as -1, to represent that the element is not present.

For example, we need to find 6, from the elements {1,2,3,4,5,6,7,8,9}.

The implementation of the same algorithm is stated below in C++ program.

#include<iostream>
using namespace std;
int binary_search(int a[],int key,int n){
int lo = 0;
int hi = n-1;
int mid;
while(lo<=hi){
mid = (lo+hi)/2;
if(a[mid]==key) return mid;
if(key<a[mid]) hi = mid-1;
else lo = mid+1;
}
return -1;
}
int main(){
int a[]={1,2,3,4,5,6,7,8,9};
int z = binary_search(a,6,sizeof(a)/sizeof(int));
if(z!=-1){
cout<<"Element is at "<<z;
}
else {
cout<<"Element is not present";
}
return 0;
}

## 56 Replies to “Binary Search in C++”

1. Like!! I blog quite often and I genuinely thank you for your information. The article has truly peaked my interest.

2. Thank you ever so for you article post.

3. Thanks so much for the blog post.

4. Like!! I blog quite often and I genuinely thank you for your information. The article has truly peaked my interest.

5. Very good article! We are linking to this particularly great content on our site. Keep up the great writing.

6. Thanks a lot for the blog post.Really looking forward to read more. Really Great.

7. Fantastic blog post.Really looking forward to read more. Keep writing.

8. Im thankful for the article post.Much thanks again. Much obliged.

9. Looking forward to reading more. Great article post.Thanks Again. Cool.

10. I value the blog.Really looking forward to read more.

11. Thanks-a-mundo for the article post.Really looking forward to read more. Really Great.

12. Thank you for your blog article. Much obliged.

13. Major thanks for the blog article.Really looking forward to read more. Will read on…

14. Im obliged for the blog article.Thanks Again. Really Cool.

15. Thanks-a-mundo for the post.Much thanks again. Cool.

16. Great, thanks for sharing this blog post.Thanks Again. Fantastic.

17. A big thank you for your blog post.Really thank you! Keep writing.

18. Thanks a lot for the blog.Really thank you! Much obliged.

19. Thanks for the blog.Really thank you! Great.

20. Muchos Gracias for your blog article.Really looking forward to read more. Really Great.

21. I really enjoy the post.Much thanks again. Great.

22. Awesome blog article. Really Great.

23. A round of applause for your post.Really thank you!

24. I am so grateful for your blog.Really looking forward to read more. Fantastic.

25. Appreciate you sharing, great post.Much thanks again. Great.

27. Thanks for the article post.Really thank you!

28. I think this is a real great post.Much thanks again. Want more.

29. Muchos Gracias for your blog article. Great.

30. Im grateful for the article post. Want more.

31. I really enjoy the article post.Much thanks again. Will read on…

32. Hey, thanks for the article post.Thanks Again. Really Cool.

33. I appreciate you sharing this post.Much thanks again. Cool.

34. Really enjoyed this blog.Thanks Again. Much obliged.

35. I cannot thank you enough for the article.Much thanks again. Great.

36. A big thank you for your article.Much thanks again. Really Great.

37. I really liked your article.Really thank you!