forked from hacktoberfest17/programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'hacktoberfest17/master'
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include<stdio.h> | ||
// Program 5: To search for the presence of a given number in the array by Binary Search. | ||
const int max=1000; | ||
int enter(int a[max],int n) | ||
{ | ||
for( int i=0;i<n;i++) | ||
{ | ||
scanf("%d",&a[i]); | ||
} | ||
return 0; | ||
} | ||
|
||
int B_search( int a[max], int n, int d) | ||
{ | ||
int l=0,u=n-1,mid,f=0; | ||
while (l<=u && !f) | ||
{ | ||
mid=(l+u)/2; | ||
if(a[mid]>d) | ||
u=mid-1; | ||
else if(a[mid]<d) | ||
l=mid+1; | ||
else f++; | ||
} | ||
if (f!=0) | ||
{ printf("\nFound at %d position", mid+1); | ||
printf("\n"); | ||
} | ||
else printf("\nNot found \n"); | ||
return 0; | ||
|
||
} | ||
|
||
int main() | ||
{ int n,d; | ||
printf("Enter the size of the array: "); | ||
scanf("%d", &n); | ||
int a[max]; | ||
printf("Enter the contents of the array in ascending order...."); | ||
enter (a,n); | ||
printf("Enter the number to be searched: "); | ||
scanf("%d", &d); | ||
B_search(a,n,d); | ||
return 0; | ||
} | ||
/* | ||
Developed By : Jap Leen Kaur Jolly | ||
OUTPUT: | ||
Enter the size of the array: 4 | ||
Enter the contents of the array in ascending order....3 4 5 2 | ||
Enter the number to be searched: 1 | ||
Not Found | ||
*/ | ||
|
||
|