### Tutorial 4

Q-1. Binary Search.

#include<stdio.h>
#include<conio.h>
void main()
{
int array[10];
int i, j, N, temp, keynum;
int low,mid,high;
clrscr();
printf("Enter the value of N\n");
scanf("%d",&N);
printf("Enter the elements one by one\n");
for(i=0; i {
scanf("%d",&array[i]);
}
printf("Input array elements\n");
for(i=0; i {
printf("%d\n",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("Sorted array is...\n");
for(i=0; i {
printf("%d\n",array[i]);
}
printf("Enter the element to be searched\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");
}
} /* End of main*/