New Text Document - Dec 29, 2022

Posted on Dec 29, 2022

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct employee
{
char name[20];
};
void bubble(struct employee *emp,int n)
{
int i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
struct employee temp;
if(strcmp(emp[j].name, emp[j+1].name) > 0)
{
temp = emp[j+1];
emp[j+1]=emp[j];
emp[j]=temp;
}
}
}
}
void main()
{
struct employee *emp=NULL,temp;
FILE *fp;
int i,j,n;
char name[20];
fp=fopen("employeebb.txt","r");
while(fscanf(fp,"%s",name)!=EOF)
n++;
emp=malloc(sizeof(struct employee)*n);
n=0;
rewind(fp);
while(fscanf(fp,"%s",emp[n].name)!=EOF)
n++;
fclose(fp);
bubble(emp,n);
fp=fopen("employeebb.txt","w");
for(i=0;i<n;i++)
fprintf(fp,"%s\n",emp[i].name);
fclose(fp);
}



// Run insert.c file on compiler // File is created as employee.txt at the same location where your insert.c is exist. // Open this employee.txt file and write name of employee. Rohit Narendra Sachin Mohit Akshay Kartik // After this save and close that file. // Again Run your insert.c file on compiler. // Then open your employee.txt and see the output as the ages are sorted by the insertion sort Output: Akshay Kartik Mohit Narendra Rohit Sachin