Type Here to Get Search Results !

To perform Binary Search

 #include<stdio.h>

#include<conio.h>

void main()

{

int a[10],n,i,j,temp; int

beg,end,mid,target;clrscr();

printf("Enter the total numbers:");

scanf("%d",&n);

printf("Enter the array elements:" );

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

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

beg=0; end=n;

mid=(beg+end)/2;

printf("\nEnter the number to be searched:");

scanf("%d",&target);

while(beg<=end && a[mid]!=target)

{

if(target<a[mid])

end=mid-1;

else

beg=mid+1;

mid=(beg+end)/2;

}

if(a[mid]==target)

{

printf("\nThe number is found at position %2d",mid);

}

else

{

printf("\nThe number is not found");

}

getch();

}


OutPut:

Enter the total numbers:5

Enter the array elements:1 2 3 4 5 Enter the

number to be searched: 5The number is

found at position 4

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Featured post

M

M