#include<stdio.h>
#include<conio.h>
int main()
{
int array[10];
int i, j, N, temp, keynum;
int low, mid, high;
printf("Enter the value of N\n");
scanf("%d",&N);
printf("enter the element one by one\n");
for (i=0; i<N; i++) {
scanf("%d",&array[i]);
}
/* Bubble sorting begins*/
for (i=0; i<N; i++)
{
for (j=0; j<(N-i-1); j++)
{
if (array[j]>array[j+1])
{
temp = array[j];
array[j] = array [j+1];
array[j+1] = temp;
}
}
}
printf("sort array is...\n");
for(i=0;i<N; i++) {
printf("%d\n", array[i]);
}
printf("Enter the element to be search\n");
scanf("%d", &keynum);
/*Binary searching begins*/
low=1;
high=N;
do
{
mid=(low + high)/2;
if (keynum < array[mid])
high = mid - 1;
else if (keynum > array [mid])
low = mid + 1;
}
while (keynum !=array[mid] && low <= high); /*End of do-while*/
if (keynum == array[mid])
{
printf("SUCCESSFUL SEARCH\n");
}
else
{
printf("search is FAILED\n");
}
getch ();
} /*End of main*/