Question
#include using namespace std; void Merge(int numbers[], int i, int j, int k) { int mergedSize; // Size of merged partition int mergePos; //
#include
using namespace std;
void Merge(int numbers[], int i, int j, int k)
{
int mergedSize; // Size of merged partition
int mergePos; // Position to insert merged number
int leftPos; // Position of elements in left partition
int rightPos; // Position of elements in right partition
int* mergedNumbers = nullptr;
mergePos = 0;
mergedSize = k - i + 1;
leftPos = i; // Initialize left partition position
rightPos = j + 1; // Initialize right partition position
mergedNumbers = new int[mergedSize]; // Dynamically allocates temporary array // for merged numbers // Add smallest element from left or right partition to merged numbers
while (leftPos <= j && rightPos <= k)
{
if (numbers[leftPos] < numbers[rightPos]) { mergedNumbers[mergePos] = numbers[leftPos]; ++leftPos;
}
else
{
mergedNumbers[mergePos] = numbers[rightPos];
++rightPos;
}
++mergePos; } // If left partition is not empty, add remaining elements to merged numbers
while (leftPos <= j) {
mergedNumbers[mergePos] = numbers[leftPos];
++leftPos; ++mergePos; } // If right partition is not empty, add remaining elements to merged numbers
while (rightPos <= k) {
mergedNumbers[mergePos] = numbers[rightPos];
++rightPos;
++mergePos; } // Copy merge number back to numbers
for (mergePos = 0; mergePos < mergedSize; ++mergePos)
{
numbers[i + mergePos] = mergedNumbers[mergePos];
}
delete[] mergedNumbers;
}
void MergeSort(int numbers[], int i, int k) { int j;
if (i < k) { j = (i + k) / 2; // Find the midpoint in the partition // Recursively sort left and right partitions
MergeSort(numbers, i, j);
MergeSort(numbers, j + 1, k); // Merge left and right partition in sorted order
Merge(numbers, i, j, k); } }
int main() {
int numbers[] = { 10, 2, 78, 4, 45, 32, 7, 11 };
const int NUMBERS_SIZE = 8; int i;
cout << "UNSORTED: ";
for(i = 0; i < NUMBERS_SIZE; ++i)
{ cout << numbers[i] << " ";
}
cout << endl;
MergeSort(numbers, 0, NUMBERS_SIZE - 1); cout << "SORTED: ";
for(i = 0; i < NUMBERS_SIZE; ++i) { cout << numbers[i] << " "; } cout << endl; return 0; }
Can you rewrite this program without arrays the [] operator on arrays or vectors pointers and dynamic allocation (e.g., no "new int[mergedsize]") ?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
CPP include using namespace std void ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started