Resources » Articles/Knowledge Sharing » Computer & Technology

Write a Program To search an element using “Binary Search”.


Updated: Category: Computer & Technology
Author: Member Level: SilverPoints: 2


Write a Program To search an element using “Binary Search”.



Write a Program To search an element using “Binary Search”.

SOURCE CODE:


#include
#include
void main()
{
int a[100];
int i,j,temp;
int start,last,mid,flag=0;
int x,n;
clrscr();
printf("This Program Is Used to Search An Element In I-d Array Using Binary Search");
printf("\n\n\nEnter The Number of Elements In Array:");
scanf("%d",&n);
printf("\n\nEnter The Elements of Array:");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
for(j=n;j>i;j--)
{
if(a[j] {
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}
}
printf("\nThe Sorted Elements Are:");
printf("\n\n");
for(i=1;i<=n;i++)
{




printf("\t%d",a[i]);
}
printf("\n\nEnter The Elements To Be Searched:");
scanf("%d",&x);
mid=0;
start=1;
last=n;
while(start<=last&&(x!=a[mid]))
{
mid=((start+last)/2);
if(x==a[mid])
{
printf("\n\nElement found at %dth position",mid);
flag=1;
}
else
{
if(x {
last=mid-1;
}
else
{
start=mid+1;
}
}
}
if(flag==0)
{
printf("\nSearch Unsuccessful.Element not found in List");
}
printf("\n\n\n\t\tTHANKYOU!");
getch();
}


OUTPUT

This program is used to Search an element in 1-d Array using Binary Serach

Enter the number of elements in Array:5

Enter the Elements of Array;12 76 34 10 98

The sorteds elements are:
10 12 34 76 98
Enter the elements to be Serched :76
Element found at 4th position.
ThankYOU.


Did you like this resource? Share it with your friends and show your love!




Responses to "Write a Program To search an element using “Binary Search”."

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index
    Complete the action items below to enter to win an iPad Mini from India Study Channel! in India Study Channel - Locker on LockerDome
    Awards & Gifts
    Active Members
    TodayLast 7 Daysmore...

     
    ISC Technologies, Kochi - India. © All Rights Reserved.