'How to print out all the steps in Merge Sort?
I'm a 2nd year college student and we have this subject Data Structures and Algorithm and a newbie. My professor gives me task to code the merge sort algorithm but she lately added that it needs to display the partition (the image that I posted) in my code. This is the sample image that I want to have in my code after the "1st Partition". Thank you in advance I wish you all safe and sound!

and this is my code
#include <iostream>
using namespace std;
//Merge two sub arrays L and M into collection'
//(array, lower bound, mid bound, upper bound)
//(array, 0, 3, 7)
void merge(int collection[], int p, int q, int r, int series) {
// Create L <-- A[p..q] and M <-- A[q+1..r]
// n1 = 3 - 0 + 1
// n1 = 4
int n1 = q - p + 1;
// n2 = 7 - 3
// n2 = 4
int n2 = r - q;
// L[4], M[4]
int L[n1], M[n2];
//for (int i = 0; 0 < 4 ; i++)
for (int i = 0; i < n1; i++)
//L[0,1,2,3] = collection[0,1,2,3] = [3,10,21,47]
L[i] = collection[p + i];
//for (int j = 0; 0 < 4 ; j++)
for (int j = 0; j < n2; j++)
//M[0,1,2,3] = collection[4,5,6,7] = [4,15,38,59]
M[j] = collection[q + 1 + j];
// Maintain current index of sub-arrays and main array
int i, j, k;
i = 0;
j = 0;
k = p;
//If else statement to determine whether (ASCENDING) lowest to highest or (DESCENDING) highest to lowest
if(series == 1){
// Until we reach either end of either L or M, pick larger among
// elements L and M and place them in the correct position at A[p..r]
// (0 < 4 && 0 < 4)
while (i < n1 && j < n2) {
//if ( 3 <= 4 )
if (L[i] <= M[j]) {
//collection[0] = 3
collection[k] = L[i];
i++;
} else {
collection[k] = M[j];
j++;
}
k++;
}
}else{
while (i < n1 && j < n2) {
if (L[i] >= M[j]) {
collection[k] = L[i];
i++;
} else {
collection[k] = M[j];
j++;
}
k++;
}
}
// When we run out of elements in either L or M,
// pick up the remaining elements and put in A[p..r]
while (i < n1) {
collection[k] = L[i];
i++;
k++;
}
while (j < n2) {
collection[k] = M[j];
j++;
k++;
}
}
//------------------------MERGING-----------------------------
// Divide the array into two subarrays, sort them and merge them
//(array, lower bound, upper bound)
//(array, 0, 7)
void mergeSort(int collection[], int lb, int ub, int sequence) {
//if (lower bound < upper bound)
//if (0 < 7)
if (lb < ub) {
// m is the point where the array is divided into two subarrays
// m = 0 + (7 -0) / 2
// m = 0 + 7 / 2
// m = 3
int mid = lb + (ub - lb) / 2;
//Call mergeSort
//mergeSort(collection, 0, 3)
mergeSort(collection, lb, mid, sequence);
//mergeSort(collection, 3 + 1, 7)
mergeSort(collection, mid + 1, ub, sequence);
// Merge the sorted subarrays
//merge(collection, 0, 3, 7)
merge(collection, lb, mid, ub, sequence);
}
}
//----------------OUTPUT---------------------
// Print the array
void printArray(int collection[], int scale) {
for (int i = 0; i < scale; i++)
cout << collection[i] << " ";
cout << endl;
}
//---------------MAIN PROGRAM----------------
// Driver program
int main() {
int array_size;
int order;
cout << "Instructions. Enter array size with a minimum of 4 and maximum of 16" << endl;
cout << "Enter array size: ";
cin >> array_size;
cout << endl;
if (array_size <=3 || array_size >= 17){
cout << "Your array size is maybe below or above the array size limits. Please try again" << endl;
exit(0);
}
cout << "In what order do you want to sort your numbers? Press [1] ASCENDING, [2] DESCENDING" << endl;
cout << "Enter: ";
cin >> order;
cout << endl;
int collection[array_size];
cout << "Enter " << array_size << " numbers: ";
for (int u = 0; u < array_size; u++){
cin >> collection[u];
}
int scale = sizeof(collection) / sizeof(collection[0]);
cout << endl;
cout << "Before Sorting: \n";
printArray(collection, scale);
mergeSort(collection, 0, scale - 1, order);
cout << "1st Partition:";
//RIGHT HERE
cout << endl;
cout << "Sorted Array: \n";
printArray(collection, scale);
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
