Create a list of employee name and phone numbers and sort them using insertion sort. Ensure that the sorting is done each time a new record gets inser

Leave a Comment
#include <string.h>
#include<stdlib.h>
#include<stdio.h>

struct emp{
char name[20];
long ph;
}a[10];
int n;


void sort(struct emp a[],int n)
{
int i,j,k;
struct emp t;
for(i=1;i {
for(j=0;j {
if(strcmpi(a[j].name,a[i].name)>0)
{
t=a[j];
a[j]=a[i];
for(k=i;k>j;k--)
a[k]=a[k-1];

a[k+1]=t;
}
}
}
}



void input(struct emp a[],int n)
{
int i;
for(i=0;i {system("cls");
printf("\nEnter the name:");
fflush(stdin);
gets(a[i].name);
printf("\nEnter the telephone number:");
scanf("%ld",&a[i].ph);
sort(a,i);
}
}



void print(struct emp a[], int n)
{
int i;
printf("\nDetails of employee sorted!!");
for(i=0;i {
printf("\nNAME=%s",a[i].name);
printf("\nTELEPHONE NUMBER=%ld",a[i].ph);
}
}

int main()
{
printf("\nEnter the maximum limit:");
scanf("%d",&n);
input(a,n);
print(a,n);
getche();
return 0;
}


0 comments: