Get the name, age and address of students who enrolled for admission of a certificate course and order them based on their age by finding the pivot.

Leave a Comment
quick sort program in c



#include<string.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct student{
char name[30];
int age;
char add[90];
}a[100];


void q_sort(struct student a[],int low,int top)
{
int i;
if(top>low)
{
i=split(a,low,top);
q_sort(a,low,i-1);
q_sort(a,i+1,top);
}
}


int split(struct student a[],int low,int top)
{
int i,p,q;
struct student t;
p=low+1;
q=top;
i=a[low].age;
while(q>=p)
{
while(a[p].age p++;
while(a[q].age>i)
q--;
if(q>p)
{
t=a[p];
a[p]=a[q];
a[q]=t;
}
}
t=a[low];
a[low]=a[q];
a[q]=t;
return q;
}

int main()
{
int n,i;
printf("\nEnter the max. limit:");
scanf("%d",&n);
for(i=0;i {
printf("\nEnter the name of the student:");
fflush(stdin);
gets(a[i].name);
printf("\nEnter the age of the student:");
scanf("%d",&a[i].age);
printf("\nEnter the address:");
fflush(stdin);
gets(a[i].add);
}

q_sort(a,0,n-1);
printf("\nAfter sorting!\n");
for(i=0;i {
printf("\nThe student name:%s",a[i].name);
printf("\nAge of the student:%d",a[i].age);
printf("\nThe address:%s",a[i].add);
}

getche();
return 0;
}

0 comments: