Use the table of contents to navigate to the portion that you are interested in.
Introduction
We know that there is two type of searching technique 1.Linear Search and 2.Binary Search. Here we discus only about binary search.
Binary search is a searching technique, it is used to locate a specific element with in the sorted array.
Warning
It is applicable only on monotonic function which means values should be in the increasing or decreasing order.
Time Complexity:
Option
Description
Best Case:
O(1)
Average Case:
O(log N)
Worst Case:
O(log N)
Space Complexicity:
Option
Description
Best Case:
O(1)
Creation of Function
First initialise start and end indices to the first and last element of the array respectively. then calculate the middle index by (start+end)/2.
Inside the while loop, compare the value of mid with the key until the start index becomes less than or equal to the end index. Here three cases arises.
Case 1: If the key matches with the mid element it returns the mid element.
Case 2: If the key is greater than the mid element then we have to check the right part of the array for that we have to initialise the start as mid + 1.
Case 3: If the key is less than the mid element than we have to check the left part of the array for that we have to update the end as mid - 1.
Tip
use mid= start + (end-start)/2 instead of (start+end)/2.
#include<iostream>
usingnamespacestd;intbinarysearch(intarr[],intsize,intkey){intstart=0;intend=size-1;//int mid= (start+end)/2;
intmid=start+(end-start)/2;while(start<=end){if(arr[mid]==key){returnmid;}if(key>arr[mid]){//go to right part
start=mid+1;}else{end=mid-1;//go to left part
}//mid= (start+end)/2;
mid=start+(end-start)/2;}return-1;}
The Main Function
Now we check the code for both if the number of elements in the array is even or odd. In the main function we define two arrays 1st is even array and the 2nd one is odd array. After that we call the function and store the result in evenindex for even array and in oddindex for odd array after that we print the indexes. Here we see that our function works properly for both cases. We can see the output.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
intmain(){inteven[6]={2,4,6,8,12,18};intodd[5]={3,8,11,14,16};intevenindex=binarysearch(even,6,6);cout<<"index of 6 is "<<evenindex<<endl;intoddindex=binarysearch(odd,5,14);cout<<"index of 14 is "<<oddindex<<endl;return0;}
#include<iostream>
//it applicable only on monotonic function values should be in inc or dec order.
usingnamespacestd;intbinarysearch(intarr[],intsize,intkey){intstart=0;intend=size-1;//int mid= (start+end)/2;
intmid=start+(end-start)/2;while(start<=end){if(arr[mid]==key){returnmid;}//go to right part
if(key>arr[mid]){start=mid+1;}else{end=mid-1;}//mid= (start+end)/2;
mid=start+(end-start)/2;}return-1;}intmain(){inteven[6]={2,4,6,8,12,18};intodd[5]={3,8,11,14,16};intevenindex=binarysearch(even,6,6);cout<<"index of 6 is "<<evenindex<<endl;intoddindex=binarysearch(odd,5,14);cout<<"index of 14 is "<<oddindex<<endl;return0;}
// pair <int, int> p;
// p.first = 5;
// p.second = 6;
#include<iostream>
usingnamespacestd;intfirstOccurance(intarr[],intn,intkey){intstart=0;intend=n-1;intmid=start+(end-start)/2;intans=-1;while(start<=end){if(arr[mid]==key){ans=mid;end=mid-1;}elseif(arr[mid]<key){// go to right part
start=mid+1;}elseif(arr[mid]>key){// go to left part
end=mid-1;}mid=start+(end-start)/2;}returnans;}intlastOccurance(intarr[],intn,intkey){intstart=0;intend=n-1;intmid=start+(end-start)/2;intans=-1;while(start<=end){if(arr[mid]==key){ans=mid;start=mid+1;}elseif(arr[mid]<key){// go to right part
start=mid+1;}elseif(arr[mid]>key){// go to left part
end=mid-1;}mid=start+(end-start)/2;}returnans;}pair<int,int>firstAndLastOccurance(intarr[],intn,intkey){pair<int,int>p;p.first=firstOccurance(arr,n,key);p.second=lastOccurance(arr,n,key);returnp;}intmain(){intarr[9]={1,2,3,3,3,3,3,4,5};cout<<" first occurace of 3 is index "<<firstOccurance(arr,9,3)<<endl;cout<<" last occurace of 3 is index "<<lastOccurance(arr,9,3)<<endl;firstAndLastOccurance(arr,9,3);}