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
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.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images