Binary Search and Insertion Sort Program in C++.

#include <iostream>
#include <conio>
#include <stdlib.h>
void main()
{
int arr[100], i, n, j, temp,beg,end,mid,num;
cout<<"Enter the size of number "<<endl;
cin>>n;
for(i=0; i<=n-1; i++)
{
cout<<"Ente element "<<endl;
cin>>arr[i];
}
for(i=1; i<n; i++)
{
for(j=i; j>=1; j--)
{
if(arr[j]>arr[j-1])
{
temp=arr[j];
arr[j]=arr[j-1];
arr[j-1]=temp;
}
else
break;
}
}
cout<<"The element s sorted order are "<<endl;
for(i=0; i<n; i++)
{
cout<<"Element are "<<arr[i]<<endl;
}
beg=0;
end=n-1;
cout<<"Enter element to be searched "<<endl;
cin>>num;
while(beg<=end)
{
mid=(beg+end)/2;
if(arr[mid]==num)
{
cout<<"Element is found at location "<<(mid+1);
break;
}
if(num>arr[mid])
{
beg=mid+1;
}
else
{
cout<<"Element is not found "<<endl;
}
}
getch();
}

Output Result:


Post a Comment

Total Pageviews