C Program to Sort Records
This is the C Program to sort records of the students according to their names
Program can be modified to sort according to marks or any criteria
#include<stdio.h>
#include<string.h>
struct person
{
char name[10];
};
typedef struct person NAME;
NAME stud[10], temp[10];
void main()
{
int no,i;
printf("Enter the number of students in the listn");
scanf("%d",&no);
for(i=0;i<no;i++)
{
printf("nEnter the name of person %d : ", i+1);
gets(stud[i].name);
temp[i] = stud[i];
}
printf("n*****************************n");
printf("Names before sorting n");
/* Print the list of names before sorting */
for(i=0;i<no;i++)
{
printf("%-10stn",temp[i].name);
}
sort(no);
printf("n*****************************n");
printf(" Names after sorting n");
printf("n*****************************n");
/* Display the sorted names */
for(i=0;i<no;i++)
{
printf("%-10st%3dn",stud[i].name);
}
printf("n*****************************n");
}
void sort(int N)
{
int i,j;
NAME temp;
for(i=0;i<N-1;i++)
{
for(j=i+1;j<N;j++)
{
if(strcmp(stud[i].name,stud[j].name)>0)
{
temp=stud[i];
stud[i]=stud[j];
stud[j] = temp;
}
}
}
}
Recent Comments