# Best C Programing code for Binary Search using function (February,2021)

## Best C Programming code for Binary Search using function(February,2021)

**Introduction**

This blog is completely dedicated for implementing Binary Search technique. When we have a list of elements and if we like to search any element, we will use this technique.

**Pre-requisite**

Before starting Binary Search , the list of elements must be sorted. Because Binary search logic can only work on sorted list. Before entering into Binary search logic, you can use any sorting algorithm.

**Explanation**

In this searching technique, first of all the complete element list is divided into halves and from those halves, depending upon the condition program will select proper half. The code is very easy to understand. But to keep the focus on Binary Search technique, we are not performing any sorting algorithm before starting Binary Search technique. We assume that user will enter elements in ascending order.

I hope that this code will help you to understand the Binary Search technique very easily. If anyone have any doubt please feel free to contact me through email id : creativeresource2020@gmail.com.

You also can put your valuable comment in the comment section.

You also can visit my Youtube channel :

https://www.youtube.com/channel/UC51-9SUu0-CaAYwT4XLNtSw

#include<stdio.h> int binsear(int [],int,int); int main() { int ar[50],m,i,n,p; printf("\nEnter the number of elements : "); scanf("%d",&m); printf("\nEnter the values in ascending order : "); for(i=0;i<m;i++) scanf("%d",&ar[i]); printf("\nEnter the value you wanna search : "); scanf("%d",&n); if(binsear(ar,m,n))//1/non zero = true 0 = false printf("\nFound"); else printf("\nNot Found"); } int binsear(int ar[],int p,int q) { int lb=0,ub=p-1,mid; while(lb<=ub) { mid=(lb+ub)/2; if(ar[mid]== q) return 1; else if(ar[mid]>q) ub=mid-1; else lb=mid+1; } return 0; }