/*----------------------------------------------------------------------------------------------
Program : Implement Searching Methods-
1. Sequential Search
2. Binary Search.
Created
By : JABIR DAUD PATHAN
Branch :
COMPUTER ENGINEERING
-------------------------------------------------------------------------------------------------*/
/*--------------------<< Header File
>>---------------------*/
#include<stdio.h>
#include<conio.h>
/*-----------<<
Global Declaration of Functions
>>-----------*/
void
accept(void);
void
sequentialsearch(void);
void
binarysearch(void);
int
arr[30],i,j,k,n,temp,key,ch;//Global Declaration of variables
/*----<<
function defination for accept array elements >>------*/
void
accept()
{
printf("\n\nHow many element you want to
enter in array==>");
scanf("%d",&n);
printf("\nEnter element in
array==>\n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]); //accept
array elements from user
}
printf("\nAccepted elements
are==>\n");
for(i=0;i<n;i++)
{
printf("\narr[%d]=%d",i,arr[i]);
//display accepted elements
}
}
/*----<<
function defination for Sequential search >>------*/
void
sequentialsearch()
{
printf("\n\nEnter any no. you want to
search==>");
scanf("%d",&key);//accept key
element from user
for(i=0;i<n;i++) //used to traverse all
array elements
{
if(arr[i]==key)
{
printf("\n\n%d is present at
location=%d",key,i);
//display
key element with its location
break;
}
}
if(arr[i]!=key)
{
printf("\n\n%d is not
present",key);
}
}
/*----<<
function defination for Binary search >>------*/
void
binarysearch()
{
/* using bubble sort technique */
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j]; //[we
arr[j]=arr[j+1];// swapping
arr[j+1]=temp; // values]
}
}
}
printf("\n\nafter sorting array elements
are==>\n");
for(i=0;i<n;i++)
{
printf("\narr[%d]=%d",i,arr[i]);
//display sorted elements
}
printf("\n\nenter any no. you want to
search==>");
scanf("%d",&key); //accept key element from user
i=0;
j=n-1;
k=(i+j)/2; //calculate mid of array
while(i<=j) //test condition till value of
i becomes j
{
if(arr[k]==key)//check key element is equal
to mid of array
{
printf("\n\n%d is present at
location=%d",key,k);
//display key
element with its location
break;
}
if(key>arr[k])
{
i=k+1;
k=(i+j)/2; //calculate new mid of array
}
if(key<arr[k])
{
j=k-1;
k=(i+j)/2; //calculate new mid of array
}
}
if(arr[k]!=key)
{
printf("\n\n%d is not
present",key);
}
}
/*-----------<< Main Function Starts >>------------*/
void
main()
{
char ans;
do
{
clrscr();
printf("\n\t\t------<< MENU
>>--------");
printf("\n\n\t\t1.Sequential
Search");
printf("\n\t\t2.Binary Search");
printf("\n\t\t3.Exit");
printf("\n\n\t\t--------------------------");
printf("\n\n\t\tEnter your
choice==>");
scanf("%d",&ch); //accept
choice from user
switch(ch)
{
case 1 :
accept(); //function call
sequentialsearch();//function call
break;
case 2 :
accept();
binarysearch();
break;
case 3 :
exit(0);
break;
default :
printf("\nInvalid choice");
break;
}
printf("\n\n\t\tDo you want to continue(Y/N)?==>");
ans=getch();
clrscr();
}while(ans=='Y'||ans=='y');
getch();
}
/*-----------<< End Of Main Function >>-----------*/
/*-----------------<< OUTPUT
SCREEN >>-------------------*/
------<< MENU
>>--------
1.Sequential Search
2.Binary Search
3.Exit
--------------------------
Enter your choice==>1
How
many element you want to enter in array==>5
Enter
element in array==>
1
5
9
7
8
Accepted
elements are==>
arr[0]=1
arr[1]=5
arr[2]=9
arr[3]=7
arr[4]=8
Enter
any no. you want to search==>7
7
is present at location=3
Do you want to
continue(Y/N)?==>Y
------<< MENU
>>--------
1.Sequential Search
2.Binary Search
3.Exit
--------------------------
Enter your choice==>2
How
many element you want to enter in array==>7
Enter
element in array==>
5
7
9
1
2
3
8
Accepted
elements are==>
arr[0]=5
arr[1]=7
arr[2]=9
arr[3]=1
arr[4]=2
arr[5]=3
arr[6]=8
after
sorting array elements are==>
arr[0]=1
arr[1]=2
arr[2]=3
arr[3]=5
arr[4]=7
arr[5]=8
arr[6]=9
enter
any no. you want to search==>8
8
is present at location=5
Do you want to
continue(Y/N)?==>Y
------<< MENU
>>--------
1.Sequential Search
2.Binary Search
3.Exit
--------------------------
Enter your choice==>3
No comments:
Post a Comment