PROGRAM - VI
(A) Problem Statement :
Create a structure with members as register number, name , marks. For each student in the class sort all the records by using bubble sort, selection sort, Quick sort, Merge sort.
(B) Input and Output :
(i) Enter student details :
Enter register no: 2
Enter name : balaji
Enter marks :89
Do you want to enter details of another student(1/0):1
Enter register no: 1
Enter name : aditya
Enter marks : 88
Do you want to enter details of another student(1/0):0
************ Student Details ***********
Register no Name Marks
Student 0: 2 balaji 89
Student 1: 1 aditya 88
1.bubble sort 2.selection sort 3.quick sort 4.mergesort
Enter your choice of technique : 1
************ Student Details ***********
Register no Name Marks
Student 0: 1 aditya 88
Student 1: 2 balaji 89
(ii) Enter student details:
Enter register no: 23
Enter name: srikanth
Enter marks: 49
Do you want to enter details of another student (1/0):1
Enter register no: 12
Enter name: adita
Enter marks: 84
Do you want to enter details of another student (1/0):1
Enter register no: 19
Enter name : miller
Enter marks : 67
Do you want to enter details of another student(1/0):0
************ Student Details ***********
Register no Name Marks
Student 0: 23 srikanth 49
Student 1: 12 adita 84
Student 2: 19 miller 67
1.bubble sort 2.selection sort 3.quick sort 4.mergesort
Enter your choice of technique : 1
************ Student Details ***********
Register no Name Marks
Student 0: 12 adita 84
Student 1: 19 miller 67
Student 2: 23 srikanth 49
(C) Examples :
(i) List is{ 23,12,19}
Selection sort:
S : 23, 12, 19
0 1 2
Min=0
Compare S[min] with s[1]and s[2] find correct min value
position
Min =1
Then s is s : 12 23 19
0 1 2
Min = 1
Compare S[min] with s[2] find correct minimum value
Position.
Min = 2
Then s is s : 12 19 23
0 1 2
(ii) the list is { 34 , 12 ,56,45 }
S: 34 , 12 , 56 ,45
Merge sort :
34 , 12 , 56 , 45
34,12 56 ,45
34 12 56 45
12,34 45, 56
12,34,45,56
(D)Algorithm :
Step 1: Start
Step 2: Define the structure student with regno, name and
marks.
Step 3: Read the no of students n to be sorted and
details into array of structure s.
Step 4: select the technique to be used to sort students
according to register no of student.
Step 5: if bubble sort is selected then the following step6
are repeated for I values 0 to n-1 then goto step15.
Step 6:for j value 0 to n-1 compare I student regno with
jth student regno if student I is greaer then
exchange details of I and j th students.
Step 7:if selection sort is selected do the following step I
values 0 to n-1and goto step15.
Step 8: find minimum number among them and exchange
With I and take lt as remaining.
Step 9:if quick sort is selected do the following step10,11
Until list contains single element goto step15.
Step 10: select the mid element in the list as key element
Place them in correct position in list.
Step 11:Apply step10 for two list obtained one before key
and after key.
Step 12:if merge sort is selected do the following step13,
14 and after that goto step 15.
Step 13:divide the list into sublist until each sublist
contains single element.
Step 14:merge each sublist and sort while merging until
We get single list.
Step 15:print sorted student details.
Step 16:Stop
(E) Program:
#include
#include
struct student //define the structure
{
int regno;
char name[20];
int marks;
};
void swapList(struct student *m,struct student *n)
{ //exchange the contenet of m and n
struct student temp;
temp = *m;
*m = *n;
*n = temp;
}
void display(struct student s[10],int n)
{ //display the students details
int i;
printf("\n********* student details are **********");
printf("\n\n\t\tRegisterNo \tName \t\tMarks");
for(i=0;i<=n;i++)
{
printf("\n student %d :",i);
printf("\t%d",s[i].regno);
printf("\t\t%s",s[i].name);
printf("\t\t%d",s[i].marks);
}
}
void bubblesort(struct student s[10],int n) //bubble sort
{
int i,j;
for(i=0;i for(j=0;j if(s[j].regno > s[j+1].regno)
swapList(&s[j],&s[j+1]); //exchange the s[j] and s[j+1]
}
void selectsort(struct student s[10],int n) //selection sort
{
int i,j,k,min;
for(i=0;i{ //intialize mininimum no position
min=i;
for(j=i+1;j<=n;j++)
{
if(s[j].regno min=j; //change min velue to j
}
swapList(&s[i],&s[min]); //exchange the contents at i and min
}
}
void quicksort(struct student s[10],int m,int n)
{ // quick sort
int key,i,j,k;
if( m < n)
{ //pick key element is mid element
k = (m+n)/2;
swapList(&s[m],&s[k]); //exchange contents at m and k
key = s[m].regno;
i = m+1;
j = n;
while(i <= j)
{
while((i <= n) && (s[i].regno <= key))
i++;
while((j >= m) && (s[j].regno > key))
j--;
if( i < j)
swapList(&s[i],&s[j]);
}
swapList(&s[m],&s[j]); //position key element
quicksort(s,m,j-1); //call quick sort on first sub array
quicksort(s,j+1,n); //call quick sort on second sub array
}
}
void mergesort(struct student s[10],int start,int end)
{ //merge sort
int j = 0;
const int size = start - end + 1;
int mid = 0; //intialize mergelist 1 and 2 to 0
int mrg1 = 0;
int mrg2 = 0;
struct student executing[10];
if(end == start)
return;
mid = (end + start) / 2; //find mid position
mergesort(s, end, mid); //call mergsort first merge list
mergesort(s, mid + 1, start); //call mergesort for second list
for(j = 0; j < size; j++)
executing[j] = s[end + j];
mrg1 = 0; //merging lists
mrg2 = mid - end + 1;
for(j = 0; j < size; j++) {
if(mrg2 <= start - end)
if(mrg1 <= mid - end)
if(executing[mrg1].regno > executing[mrg2].regno)
s[j + end] = executing[mrg2++];
else
s[j + end] = executing[mrg1++];
else
s[j + end] = executing[mrg2++];
else
s[j + end] = executing[mrg1++];
}
}
void main()
{
struct student s[10];
int c,i=-1;
clrscr();
do
{ //reading student details
++i;
printf("\n enter student details:");
printf("\n enter register no:");
scanf("%d",&s[i].regno);
printf("\n enter name:");
scanf("%s",&s[i].name);
printf("\n enter the marks");
scanf("%d",&s[i].marks);
printf("\n do you want to enter details of another student(1\0):");
scanf("%d",&c);
}while(c!=0);
display(s,i); //selecting sorting technique
printf("\n 1.bubble sort 2.selection sort 3.quick sort 4.merge sort");
printf("\n enter your choice techinique");
scanf("%d",&c);
switch(c)
{ //call bubble sort
case 1: bubblesort(s,i); //display student details
display(s,i);
break;
case 2: selectsort(s,i); //call selection sort
display(s,i); //display student details
break;
case 3: quicksort(s,0,i); //call quick sort
display(s,i); //display student details
break;
case 4: mergesort(s,0,i); //call mergesort
display(s,i); //display student details
break;
}
getch();
}
(F) Testing:
The program can be tested for the given list of students in
the form of structure and sorted according to roll number
properly for the given sorting techniques.
(G) Limitations:
(i) this program is limited to sort only 10 student records.