Thursday 9 January 2014

HOW TO PERFORM BINARY SEARCH IN C

#include<stdio.h>
#include<conio.h>
void main()
{
 int low=1,mid,high,n=10,k[10],i,x;
 clrscr();
 for(i=1;i<=n;i++)
 {
  printf("\nEnter value:-");
  scanf("%d",&k[i]);
 }
  printf("\n\n\t\tEnter value of you want to search:-");
  scanf("%d",&x);
  high=n;
 while(low<=high)
 {
  mid=(high+low)/2;
  if(x==k[mid])
  {
    printf("\n\nsuccessful search");
    getch();
    return;
  }
  else
  {
   if(x>k[mid])
   {
    low=mid+1;
   }
   else
   {
    high=mid-1;
   }
  }
  }
   printf("\n\n\t\tUnsuccessful search");
   getch();
   }

No comments:

Post a Comment

How to change Background color by selecting color from Listview

activitymain.xml file::> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xm...