Program details:
1. Display the data in descending order of Mobile No.(insertion sort).
2. Display data in ascending order of name (selection sort).
3. Display details of Mobileno specified by user (Binary search).
4. Display the number of passes and comparisons for different test cases(
(Worst, Average,Best case).
;i++)>;i++)>
#include
#include
#include
#include
typedef struct mobile
{ long mobileno;
char name[20];
float billamt;
}mobile;
int binsearch(mobile st[],long mobileno,int n);
/* Different cases for searching :
Best case - Data being searched is exactly at the centre
worst Case - Data being searched is not there.
Average Case - Data being searched is at a random place .
*/
void print(mobile st[],int n);
void read(mobile st[],int n);
void insertionsort(mobile a[],int n); //sort in descending order on MobileNo
/* Different cases for insertin sort
Best case : when the data is already sorted in descending order on mobileno
worst case: when the data is already sorted in ascending order on mobileno.
Average case : when the data is in a random sequence
*/
void selectionsort(mobile a[],int n);
/*Different cases for selection sort :
Only one case :
No. of passes = n-1
no. of comparisons= n(n-1)/2
*/
void main()
{ mobile st[30];
int n,i,op,position;
long mobileno;
clrscr();
do{
flushall();
printf("\n1)create\n2)print");
printf("\n3)Display the data in descending order of mobileno(insertion sort)");
printf("\n4)Display the data in ascending order of name(selection sort)");
printf("\n5)Display details for mobileno specified by user(binary search)");
printf("\n6)Quit");
printf("\nEnter Your Choice:");
scanf("%d",&op);
switch(op)
{ case 1: printf("\nEnter No. of Users :");
scanf("%d",&n);
read(st,n);
break;
case 2: print(st,n);break;
case 3: insertionsort(st,n); print(st,n); break;
case 4: selectionsort(st,n); print(st,n); break;
case 5: printf("\nPlease ensure that data is sorted using option no.3");
printf("\nenter Mobile number : ");
scanf("%ld",&mobileno);
position=binsearch(st,mobileno,n);
if(position==-1)
printf("\nnot found");
else
{ printf("\n found at location=%d",position+1);
printf("\n %s\t%ld\t%6.2f",st[position].name,st[position].mobileno,st[position].billamt);
}
break;
}
}while(op!=6);
}
int binsearch(mobile st[],long mobileno,int n)
{ int i,j,k,comp=0;
i=0;
j=n-1;
while(i<=j)
{
k=(i+j)/2;
comp++;
if(mobileno==st[k].mobileno)
{
printf("\nNo. of comparisons = %d ",comp);
return(k);
}
else
if(mobileno > st[k].mobileno)
j=k-1;
else
i=k+1;
}
printf("\nNo. of comparisons = %d ",comp);
return(-1);
}
void print(mobile st[],int n)
{ int i;
for(i=0;i=0 && a[j].mobileno < temp.mobileno;j--)
{
comp++;
a[j+1]=a[j];
}
a[j+1]=temp;
}
printf("\nPasses = %d\t Comparisons = %d",passes,comp);
}
void selectionsort(mobile a[],int n)
{
int i,j,k,passes=0,comp=0;
mobile temp;
for(i=0;i< 0 )
k=j;
}
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
printf("\nPasses = %d\t Comparisons = %d",passes,comp);
}
Output
1)create
2)print
3)Display the data in descending order of mobileno(insertion sort)
4)Display the data in ascending order of name(selection sort)
5)Display details for mobileno specified by user(binary search)
6)Quit
Enter Your Choice:1
Enter No. of Users :3
enter data(name mobile No.(9 disgits max) Bill Amount ):
Shreya 989087256 2289
Shruti 976015736 1345
Shina 989076543 1100
1)create
2)print
3)Display the data in descending order of mobileno(insertion sort)
4)Display the data in ascending order of name(selection sort)
5)Display details for mobileno specified by user(binary search)
6)Quit
Enter Your Choice:2
Shreya 989087256 2289.00
Shruti 976015736 1345.00
Shina 989076543 1100.00
1)create
2)print
3)Display the data in descending order of mobileno(insertion sort)
4)Display the data in ascending order of name(selection sort)
5)Display details for mobileno specified by user(binary search)
6)Quit
Enter Your Choice:3
Passes = 2 Comparisons = 1
Shreya 989087256 2289.00
Shina 989076543 1100.00
Shruti 976015736 1345.00
1)create
2)print
3)Display the data in descending order of mobileno(insertion sort)
4)Display the data in ascending order of name(selection sort)
5)Display details for mobileno specified by user(binary search)
6)Quit
Enter Your Choice:4
Passes = 2 Comparisons = 3
Shina 989076543 1100.00
Shreya 989087256 2289.00
Shruti 976015736 1345.00
1)create
2)print
3)Display the data in descending order of mobileno(insertion sort)
4)Display the data in ascending order of name(selection sort)
5)Display details for mobileno specified by user(binary search)
6)Quit
Enter Your Choice:5
Please ensure that data is sorted using option no.3
enter Mobile number : 976015736
No. of comparisons = 2
found at location=3
Shruti 976015736 1345.00
1)create
2)print
3)Display the data in descending order of mobileno(insertion sort)
4)Display the data in ascending order of name(selection sort)
5)Display details for mobileno specified by user(binary search)
6)Quit
Enter Your Choice:6
Leave a Reply