📌 CODE-LIBRARY:
// author: MSA
int BinarySearch(int a[],int n,int target)
{
int low=0, high =n-1;
while(low<=high)
{
int mid=low+(high-low)/2; // best formula to ignore overflow
if(target==a[mid]) return mid; //mid element is the ans
else if(a[mid]<target) low=mid+1; //moving to right side
else high=mid-1; // moving to left side
}
return -1; //no value found
}
📌 VIDEO-TUTORIALS :
1. mycodeschool
2. Errichto
3. AfterAcademy
4. Nick White
📌 PDF-TUTORIALS :
1. Binary Search »»»»»👨💻 I_Love_Equinox
📌 SCREENSHOT-BREAKDOWN :
1. Iterative Binary Search - [Method -01]::
source: mycodeschool
mid=(low+high)/2
2. Iterative Binary Search - [Method -02]::
mid=low+(high-low)/2
source: mycodeschool
source: Errichto
3. (Iterative vs Recursive) Binary Search::
source: mycodeschool
4. First occurrence of a number using Binary Search::
source: mycodeschool
if(x==A[mid])
{
result=mid;
high=mid-1;
}
5. Last occurrence of a number using Binary Search::
source: mycodeschool
if(x==A[mid])
{
result=mid;
low=mid+1;
}
6. Count occurrences of a number in a sorted array using Binary Search
source: mycodeschool
//array is sorted so at the beginning find the first and last element then
(last element-first element)+1