C Program For Binary Search With Sorting

#include<stdio.h>
#include<conio.h>
void main()
{
 int a[10],p,temp,i,n,key,flag=0,low,high,mid;
 clrscr();
 printf("Enter the size of an array:");
 scanf("%d",&n);
 printf("Enter the elements:");
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);
    for(p=0;p<n;p++)
    if(a[i]>a[i+1])
       temp=a[i];
       a[i]=a[i+1];
       a[i+1]=temp;
 printf("Enter the number to be searched:");
 scanf("%d",&key);

 low=0,high=n-1;
 while(low<=high)
      {
  mid=(low+high)/2;
  if(key==a[mid])
    {
   printf("search element found at location: %d",mid);
   flag=1;
   return;
    }
  else if (key<a[mid])
   high=mid-1;
   else
   low=mid+1;
       }
       if(flag==0)
       printf("The number is not found");
       getch();
      }

Bhanu Namikaze

Bhanu Namikaze is an Ethical Hacker, Web Developer, Student and Mechanical Engineer. He Enjoys writing articles, Blogging, Solving Errors and Social Networking. Feel Free to let me know any of your concerns about hacking or let me know if you need any more methods on hacking anything. Enjoy Learning

No comments:

Post a Comment