C program to read the data from the file “employee.txt” and sort on age using bubble sort, insertion sort and selection sort | Coding Expert 10

Read the data from the file “employee.txt” and sort on age using bubble sort :

#Bubble Sort

#include<stdio.h>

typedef struct record
{
  char name[30];
  int age;
}record;

int fileread(record a[20])
{
    FILE *fp;
    int i=0;

    fp=fopen("employee.txt","r");
    if(fp==NULL)
        printf("File Not Exist");
    else
    {
        while(!feof(fp))
        {
            fscanf(fp,"%s%d", a[i].name, &a[i].age);
            i++;
        }
        fclose(fp);
    }
    return i-1;
}

//Main
int main()
{
    int i, n;

    record a[20];
    n = fileread(a);

    for(int i=0; i<n; i++)
        printf("%s %d\n", a[i].name, a[i].age);
       /* displaying records*/

bubblesort(n);


}
void bubblesort(int n)
{
    record a[20],b[30];
    n=fileread(a);
    int i,j,temp,k;
    char str[20];
    for(i=0; i<n; i++)
    {
        for(j=0; j<n-i-1; j++)
        {
            if(a[j].age>a[j+1].age)
            {
                for(k=0;k<='\0';k++)
                b[k]=a[j];
                a[j]=a[j+1];
                for(k=0;k<='\0';k++)
                a[j+1]=b[k];

            }
        }
    }
    printf("Sorted list:\n");
    for(int i=0; i<n; i++)
        printf("%s %d\n", a[i].name, a[i].age);

}

Output :

rajiv 43
Prakash 34
Vinay 35
asad 23
rohit 24
Sorted list:
asad 23
rohit 24
Prakash 34
Vinay 35
rajiv 43

Process returned 0 (0x0)   execution time : 0.175 s
Press any key to continue.

Read the data from the file “employee.txt” and sort on age using Insertion sort :

#Insertion Sort

#include<stdio.h>

typedef struct record
{
  char name[30];
  int age;
}record;

int fileread(record a[20])
{
    FILE *fp;
    int i=0;

    fp=fopen("employee.txt","r");
    if(fp==NULL)
        printf("File Not Exist");
    else
    {
        while(!feof(fp))
        {
            fscanf(fp,"%s%d", a[i].name, &a[i].age);
            i++;
        }
        fclose(fp);
    }
    return i-1;
}

//Main
int main()
{
    int i, n;
    char key[20];
    record a[20];
    n = fileread(a);

    for(int i=0; i<n; i++)
        printf("%s %d\n", a[i].name, a[i].age);
       /* displaying records*/

insertion_sort(n);
}
void insertion_sort(int n)
{
	record a[20],b[30];
    n=fileread(a);
	int temp,i,j,k;
	for (i=0 ;i<n ;i++)
	{
		temp= a[i].age;
		for(k=0;k<='\0';k++)
        b[k]=a[i];
		j = i - 1;
		while (temp<a[j].age && j>=0)
		{

			a[j + 1].age = a[j].age;
			for(k=0;k<='\0';k++)
                a[j+1]=a[j];
			j = j - 1;
		}
		for(k=0;k<='\0';k++)
            a[j+1]=b[k];
		a[j + 1].age = temp;

	}
	printf("Sorted list:\n");
    for(int i=0; i<n; i++)
        printf("%s %d\n", a[i].name, a[i].age);
}

Output :

rajiv 43
Prakash 34
Vinay 35
asad 23
rohit 24
Sorted list:
asad 23
rohit 24
Prakash 34
Vinay 35
rajiv 43

Process returned 0 (0x0)   execution time : 0.123 s
Press any key to continue.

Read the data from the file “employee.txt” and sort on age using Selection sort :

#selection sort

#include<stdio.h>

typedef struct record
{
  char name[30];
  int age;
}record;

int fileread(record a[20])
{
    FILE *fp;
    int i=0;

    fp=fopen("employee.txt","r");
    if(fp==NULL)
        printf("File Not Exist");
    else
    {
        while(!feof(fp))
        {
            fscanf(fp,"%s%d", a[i].name, &a[i].age);
            i++;
        }
        fclose(fp);
    }
    return i-1;
}

//Main
int main()
{
    int i, n;

    record a[20];
    n = fileread(a);

    for(int i=0; i<n; i++)
        printf("%s %d\n", a[i].name, a[i].age);
       /* displaying records*/

selection_sort(n);
}
void selection_sort(int n)
{
	record a[20],b[30];
    n=fileread(a);
	int i,j,k,min;
	for(i=0;i<n;i++)
	{
		min = i;
		for(j=i+1;j<n;j++)
		{
			if(a[j].age<a[min].age)
				min = j;
		}
		for(k=0;k<='\0';k++)
		b[k]=a[i];
		a[i]=a[min];
		for(k=0;k<='\0';k++)
		a[min]=b[k];
	}
	printf("Sorted list:\n");
    for(int i=0; i<n; i++)
        printf("%s %d\n", a[i].name, a[i].age);
}

Output :

rajiv 43
Prakash 34
Vinay 35
asad 23
rohit 24
Sorted list:
asad 23
rohit 24
Prakash 34
Vinay 35
rajiv 43

Process returned 0 (0x0)   execution time : 0.359 s
Press any key to continue.

  • Related Programs:



Post a Comment

1 Comments

  1. Thank u sir
    I have question sir if we have to run this program on Ubuntu then it takes error in function input .kya ap ye program ubuntu mai using c dikha sakte ho sir plz

    ReplyDelete

Thanks,To visit this blog.