Close Back

#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<rajesh.h>

main()
{
	int array[30],n;
	clrscr();
	n = GetArray(array);
	BubbleSort(array,n);
	BinarySer(array,n);
	getch();
	return;
}
GetArray(array)
int *array;
{
	int n,i;
	do
	{
		printf("\nHow many numbers do you want to enter (below 30)\t");
		scanf("%d",&n);
	}
	while(n<1 || n>30);
	printf("\nEnter the numbers\t");
	for(i=0;i<n;i++)
		scanf("%d",(array+i));
	return n;
}
BubbleSort(array,n)
int n,*array;
{
	int i,j,temp;
	for(i=0;i<n;i++)
		for(j=0;j<n-1;j++)
			if(*(array+j) > *(array+j+1))
			{
				temp = *(array+j);
				*(array+j) = *(array+j+1);
				*(array+j+1) = temp;
			}
	printf("\nThe sorted array is\n");
	for(i=0;i<n;i++)
		printf("%d\t",*(array+i));
	return;
}
BinarySer(array,n)
int *array,n;
{
	int first,last,mid,key,loc,i,found,probe;
	printf("\nEnter the key to be searched\t");
	scanf("%d",&key);
	found = 0;
	first = 0;
	last = n-1;
	probe = 0;
	while(first<=last && !found)
	{
		mid = (first+last)/2;
		probe++;
		if(*(array+mid) > key)
			last = mid-1;
		else if(*(array+mid) < key)
			first = mid+1;
		     else
		     {
			  loc = mid + 1;
			  found = 1;
		     }
	}
	if(found)
	{
		printf("\nThe element found in location %d",loc);
		printf("\nNumber of probes is %d",probe);
	}
	else
		printf("\nThe element not found");
	return;
}