
Binary Search
Search Element using Binary search in the Linear & 2-D Arrays
Time Complexity always O(Logn)
Q1: Binary Search
Q1: Binary Searchint BinarySearch(int arr[], int key, int size){
int s = 0; // starting index
int e = size-1; // ending index
while(s <= e){
int mid = s + (e - s)/2;
if(arr[mid] == key){
return mid;
}
// if arr[mid] != key, then decide the part using this conditions > or <
else if(arr[mid] > key){
e = mid - 1;
}
else{
s = mid + 1;
}
}
return -1;
} Q4: Pivot Index
Q4: Pivot IndexQ6: sqrt(x)
Q6: sqrt(x)Last updated
Was this helpful?