Search & Find out

Thursday, December 3, 2009

Binary search

#include "stdio.h"
#include "conio.h"
void main ()
{
    int LA[6]={2,4,6,8,10,12},i,j,beg=0,mid,end=5;
    clrscr ();
    mid=((beg+end)/2);
    printf("2,4,6,8,10,12\n\n");
    printf ("Entr the number to be searched: ");
    scanf ("%d",&j);
    for (;beg<=end && j!=LA[mid];)
    {
        if (j
            end=mid-1;
        else
            beg=mid+1;
        mid=((beg+end)/2);
    }
    if (LA[mid]==j)
        printf ("The location is: %d",(mid+1));
    else
        printf ("The location does not exist!");
getch ();
}



Download the executable (.exe)  file for this program..
download

0 comments:

Post a Comment

 

My Blog List

Term of Use

Copyright © 2009 Black Nero is Designed by Ipietoon Sponsored by Online Business Journal