Sunday, 1 February 2015

Program to implement Binary search in C++

//Program to implement Binary search in C++.

#include<iostream.h>
#include<conio.h>
void main()
{
int arr[10],i,j,mid,no,found=0;
clrscr();
cout<<"Enter 10 values: \n";
for(i=0;i<10;i++)
cin>>arr[i];
cout<<"Enter value to find: ";
cin>>no;
i=0; j=9;
while(!found && i<=j)
{
mid=(i+j)/2;
if(arr[mid]==no)
found=1;
else
if(arr[mid]<no)
i=mid+1;
else
j=mid-1;
}
if(found)
cout<<"Value found at "<<mid<<" index";
else
cout<<"Value not found";
getch();
}

No comments:

Post a Comment

Read contacts in iOS using Swift with error handling

Just copy and paste the below code Pre-step before pasting the below code Add below line in AppDelegate.swift file below import statement ...