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).

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter12: Points, Classes, Virtual Functions And Abstract Classes
Section: Chapter Questions
Problem 29SA
icon
Related questions
Question

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);

   }

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps

Blurred answer
Knowledge Booster
Lists
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning