**Problem:-**binary search program in c or C Program to accept Sorted Array and do Search using Binary Search or C program for binary search or C Program to accept Sorted Array and do Search using Binary Search or C program for Binary Search or Binary Search in C or Binary Search Program in C or Binary Search In C Programming or C Program For BINARY SEARCH or C program to implement binary search or C Program/Code for Binary Search or binary search for strings or C Program to Search an Array Element using BINARY SEARCH or Simple Binary Searching Program in C or C Program for binary search on array using recursion.

**Check:-**Hacker rank solution for Strings, Classes, STL, Inheritance in C++.

**Logic:-**Binary search logic is simple and very useful. Let's take an example of an array and try to perform a Binary search operation on it. First, we have to know that Binary search applies is only sorted data, so if data is not sorted then you can not apply Binary search. Now come to point so basically we divide the array data in two parts and compare the elements first iteration, and compare the element of the mid element of an array if the element is less than the array element then again divide the array from staring to midpoint into 2 part and again matches the element if again array element is greater than the element than again divide the array into two parts. If the array element is less the element than again divide the array into two parts starting to mid to end and repeat the step until to the last divide the array. If you are not getting than don;t worry Check below for step by step Explanation.

**Check This:-**C++ Program For Binary Search

**Explanation:-**We are taking an example of an array with the Non-Sorted element and performing a Binary search An Array is 9, 8, 3, 5, 4, 7, 1, 2, 6. Now follow the steps below.

**Step 1:-**First we have to sort the array or we can perform in already sorted array element but in this problem, we are sorting the array first. After the sorting, an array element is 1, 2, 3, 4, 5, 6, 7, 8, 9. total 9 elements. Now we have to Find the Element 7 in an array.

**Step 2:-**Now first we compare divide the array into two(Two Exactly). After the dividing, an array we got the midpoint Now compare the element 7 into the array element 5(MidPoint) we can clearly see that element is greater than the array element now we have to again divide the array into two part array is Now 6, 7, 8, 9.

**Step 3:-**Now divide the array and compare the element again after dividing an array we got a mid element of an array, Now compare an array mid to 7 we got the element in an array.

**Step 4:-**Now print the message that an Element is found in an Array. If Not found than Print Not Found the element in an Array.

**Note:-**Always perform the Binary search in the sorted element of an array, here I First sorted than performing. Sorting is compulsory for performing Binary search. If you are not entering the sorted array than you are not followed the Binary search properties.

**Key Note:-**Here is some feature of a Binary search.

1. Binary Search always performe in

**Sorted**Data.

2. Binary Search

**Worst-case**performance======>

**O(log n)**

3. Binary Search

**Best-case**performance=======>

**O(1)**

4. Binary Search

**Average**performance =======>

**O(log n)**

5. Binary Search

**Worst-case**space complexity==>

**O(1)**

**Solution:-**

#include<stdio.h>

main()

{

//C program for binary search

int *a,i,itm,s,j,flag=0,low,up,mid,temp;

printf("==================================");

printf("\nC++ Program For Binary Search\n");

printf("==================================\n");

printf("\nEnter The Size Of An Array :");

scanf("%d",&s);

a=(int*)malloc(s*sizeof(int));

printf("\nEnter An Elements Of An Array\n");

for(i=0;i<s;i++)

{

scanf("%d",&a[i]);

}

for(i=1;i<s;++i)

{

for(j=0;j<(s-1);++j)

{

if(a[j]>a[j+1])

{

temp = a[j];

a[j] = a[j+1];

a[j+1] = temp;

}

}

}

printf("\nEnter The Number You Want To Search In An Array :");

scanf("%d",&itm);

low=0;

up=s-1;

mid=(low+up)/2;

if(itm<=a[mid])

{

for(i=0;i<=mid;i++)

{

if(a[i]==itm)

flag=1;

}

}

if(itm>a[mid])

{

for(i=mid;i<s;i++)

{

if(a[i]==itm)

flag=1;

}

}

if(flag==1)

{

printf("\nNumber Found In an Array \n");

}

else

{

printf("\nNumber Not Found In an Array \n");

}

}

**Output:-**

**You May Also Like:-**

1. C Program For Find A Grade Of Given Marks Using Switch Case

2. C++ Program For Store Employee Information And Display Using Structure

3. C Program For Finding Radius Circumference Using Switch Case

4. C Program For Calculator Using Switch Case

5. Java Program For Find The Gross Salary Of An Employee

6. Hacker Rank Solution For Birthday Cake Candles

7. C++ Program For Calculate Percentage Of 5 Subjects

8. Hacker Rank Solution For Strings

9. Hacker Rank Solution For Conditional Statements

10. Hacker Rank solution for 30 Days Of Code

## No comments:

## Post a Comment