QUESTION 1 Following are the functions for insertion sort to sort an array of integers in ascending order. Modify the functions to sort the array in descending order.   ==================== Insertion Sort ====================   void Insertion(int *A, int n){    int i, j, key;    for(i=1; i=0 && key

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

QUESTION 1

Following are the functions for insertion sort to sort an array of integers in ascending order. Modify the functions to sort the array in descending order.

 

====================

Insertion Sort

====================

 

void Insertion(int *A, int n){

   int i, j, key;

   for(i=1; i<n; i++) {

       key = A[i];

       j = i-1;

       while (j>=0 && key<A[j]){

          A[j+1] = A[j];

          j--;

       }

       A[j+1] = key;

   }

}

 

QUESTION 2

Following are the functions for merge sort to sort an array of integers in ascending order. Modify the functions to sort the array in descending order.

====================

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=L; i<=R; 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);

   }

}

 

QUESTION 3

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. You modify the codes given in Questions 1 and Question 2.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Arrays
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
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr