In session 21 Exercise 4, you have created a program to sort data struct mhs saved in array of structure, where the data are sorted by gpa in ascending order, and for similar gpa, the data are sorted by name in ascending order. For that exercise, you are asked to use selection sort. Here is the question In session 21 Exercise 4 : Sort is based on gpa (ascending), for similar gpa then sort by name (ascending).
In session 21 Exercise 4, you have created a program to sort data struct mhs saved in array of structure, where the data are sorted by gpa in ascending order, and for similar gpa, the data are sorted by name in ascending order. For that exercise, you are asked to use selection sort.
Here is the question In session 21 Exercise 4 :
Sort is based on gpa (ascending), for similar gpa then sort by name (ascending).
struct mhs{
int nim;
float gpa;
char name[20];
};
Now, perform the same sorting using insertion sort and merge sort.
Code for sorting using insertion sort and merge sort is below
Insertion Sort
====================
void Insertion(int *A, int n){
int i, j, key;
for(i=n; i; i++) {
key = A[i];
j = i-1;
while (j>=0 && key<A[j]){
A[j+1] = A[j];
j--;
}
A[j+1] = key;
}
}
Merge Sort
====================
void merge(int A[], int L, int m1, int m2, int R){
int Lidx = L;
int Ridx = m2;
int Cidx = L;
int B[SIZE];
int i;
while (Lidx <= m1 && Ridx <= R) {
if (A[Lidx] <= A[Ridx])
B[Cidx++] = A[Lidx++];
else
B[Cidx++] = A[Ridx++];
}
if (Lidx == m2) {
while (Ridx <= R)
B[Cidx++] = A[Ridx++];
}
else {
while (Lidx <= m1)
B[Cidx++] = A[Lidx++];
}
for (i=R; i>=L; i--)
A[i] = B[i];
}
void mergeSort(int A[], int low, int high){
int m1, m2;
if (high-low >= 1) {
m1 = (low+high) / 2;
m2 = m1+1;
mergeSort(A, low, m1);
mergeSort(A, m2, high);
merge(A, low, m1, m2, high);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps