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

- To search for the key element, we divide the array into two parts.
- Then we compare key element to the middle item.
- If key element is more than middle element, we search the element in next part of the array.
- Else, we search the element in the first half of the array.
- We repeat the steps 3 and 4 till we find the element.
- 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;
}
```

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

Thank you ever so for you article post.

Thank you ever so for you article post.

I like this website very much, Its a very nice office to read and incur information.

Thanks so much for the blog post.

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

I really like and appreciate your blog post.

I learn something new and challenging on blogs I stumbleupon everyday.

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

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

Very informative post.Really thank you! Want more.

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

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

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

Really enjoyed this blog. Really Cool.

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

Very informative blog post.Thanks Again. Want more.

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

Thank you for your blog article. Much obliged.

Really informative blog post.Much thanks again.

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

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

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

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

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

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

Thanks for the blog.Really thank you! Great.

I cannot thank you enough for the post. Great.

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

I really liked your blog. Much obliged.

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

Muchos Gracias for your article.Much thanks again. Great.

Awesome blog article. Really Great.

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

Great post. Will read on…

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

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

Great, thanks for sharing this article.Thanks Again. Awesome.

Thanks for the article post.Really thank you!

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

Wow, great article post.Really looking forward to read more. Really Great.

This is one awesome blog post.Really thank you! Fantastic.

Muchos Gracias for your blog article. Great.

Im grateful for the article post. Want more.

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

Major thanks for the blog.Really looking forward to read more. Really Great.

Thanks a lot for the blog. Great.

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

I am so grateful for your article post. Really Great.

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

Really enjoyed this blog.Thanks Again. Much obliged.

Thanks for the post.Thanks Again. Will read on…

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

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

I really liked your article.Really thank you!