Question

In: Computer Science

In C++ Write an IterativeMergeSort function given these parameters and following the prompt IterativeMergeSort(vector<int> v, int...

In C++

Write an IterativeMergeSort function given these parameters and following the prompt

IterativeMergeSort(vector<int> v, int low, int high)

IterativeMergeSort

In-place sorting refers to sorting that does not require extra memory arrays. For

example, QuickSort performs partitioning operations by repeating a swapping operation on two

data items in a given array. This does not require an extra array.

We can improve the performance of MergeSort by utilizing a non-recursive method and

using only one additional array (instead of one array on each recursive call). In this improved

version of MergeSort,

IterativeMergeSort

, one would merge data from the original array into

the additional array and

alternatively

copy back and forth between the original and the

additional temporary array.

Please re-read the last sentence as it is critical to the grading of

the lab.

For the IterativeMergeSort we still need to allow data items to be copied between the

original and this additional array as many times as O(

log

n). However, given the iterative nature

we are not building up state on the stack.

Solutions

Expert Solution

Hello,

Please find below logic code for iterative merge sort in C/C++,

Note: here I have used Array instead vector for better get/put. if you want, you can modify your function's input to array or modify below code from array to vector.

This code was written for lector's note 3 years back. Hope this will help you.

CODE:

/* Recursive program for merge sort */

#include<stdlib.h>

#include<stdio.h>

  

/* Function to merge the two haves arr[l..m] and arr[m+1..r] of array arr[] */

void merge(int arr[], int l, int m, int r);

  

/* l is for left index and r is right index of the sub-array of arr to be sorted */

void mergeSort(int arr[], int l, int r)

{

   if (l < r)

   {

      int m = l+(r-l)/2; //Same as (l+r)/2 but avoids overflow for large l & h

      mergeSort(arr, l, m);

      mergeSort(arr, m+1, r);

      merge(arr, l, m, r);

   }

}

  

/* Function to merge the two haves arr[l..m] and arr[m+1..r] of array arr[] */

void merge(int arr[], int l, int m, int r)

{

    int i, j, k;

    int n1 = m - l + 1;

    int n2 = r - m;

  

    /* create temp arrays */

    int L[n1], R[n2];

  

    /* Copy data to temp arrays L[] and R[] */

    for (i = 0; i < n1; i++)

        L[i] = arr[l + i];

    for (j = 0; j < n2; j++)

        R[j] = arr[m + 1+ j];

  

    /* Merge the temp arrays back into arr[l..r]*/

    i = 0;

    j = 0;

    k = l;

    while (i < n1 && j < n2)

    {

        if (L[i] <= R[j])

        {

            arr[k] = L[i];

            i++;

        }

        else

        {

            arr[k] = R[j];

            j++;

        }

        k++;

    }

  

    /* Copy the remaining elements of L[], if there are any */

    while (i < n1)

    {

        arr[k] = L[i];

        i++;

        k++;

    }

  

    /* Copy the remaining elements of R[], if there are any */

    while (j < n2)

    {

        arr[k] = R[j];

        j++;

        k++;

    }

}

  

/* Function to print an array */

void printArray(int A[], int size)

{

    int i;

    for (i=0; i < size; i++)

        printf("%d ", A[i]);

    printf("\n");

}

  

/* Driver program to test above functions */

int main()

{

    int arr[] = {12, 11, 13, 5, 6, 7};

    int arr_size = sizeof(arr)/sizeof(arr[0]);

  

    printf("Given array is \n");

    printArray(arr, arr_size);

  

    mergeSort(arr, 0, arr_size - 1);

  

    printf("\nSorted array is \n");

    printArray(arr, arr_size);

    return 0;

}


Related Solutions

Write a function in c++, called afterAll that takes two parameters, a vector of string and...
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Write another C++ function,lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. An analysis and design of the function smallestIndex is given below. Write an analysis and design for the function lastLargestIndex. Write...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Also, write a program to test your function. You must write our commands in the middle: Write a C++ Program: #include <iostream> using namespace std; const int ARRAY_SIZE = 15; void printArray(const int x[], int sizeX); int smallestIndex(const int x[], int sizeX); int main() {      int list[ARRAY_SIZE] = {56,...
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
Write a C++ function which takes an int array and its size as parameters. It returns...
Write a C++ function which takes an int array and its size as parameters. It returns an int indicating how many multiples of 3 are contained in the array. For example, if the array contains {2, 6, 8} your function should return 1. If the array contains {3, 10, 5, 6} your function should return 2. Here is the function prototype: // int array[]: array to search // size: number of elements in the array int countMult(const int array[], int...
Write a program that contains the following Write a function copies with two int parameters, named...
Write a program that contains the following Write a function copies with two int parameters, named n and x. It should dynamically allocate a new array of size n.  The function should then copy the value in x to every position in the array. The function should return a pointer to the new array. In the main function, call the copies function from part 1. with size 5 and value -1.  Output the resulting array to the screen, and deallocate the array....
#include<vector> #include<iostream> using namespace std; void println(const vector<int>& v) {    for (int x : v)...
#include<vector> #include<iostream> using namespace std; void println(const vector<int>& v) {    for (int x : v)        cout << x << " ";    cout << endl; } void println(const vector<string>& v) {    for (const string& x : v)        cout << " "<< x << " ";    cout << endl; } int main() {    vector<int> v0;    cout << "An empty vector of integers: ";    println(v0);    vector<int> v1(3);    cout << "A...
C++ Write the implementation of the function concatenateIntArrays. This function receives 4 parameters in the following...
C++ Write the implementation of the function concatenateIntArrays. This function receives 4 parameters in the following order: An array of integer values (array1). An integer representing the size of array1 (size1). An array of integer values (array2). An integer representing the size of array2 (size). The function creates a dynamic array of integers of size size1+size2 to store all the values in array1, followed by all the values in array2. The function returns the pointer used to create the dynamic...
C Practice 1: 1) Write a forward declaration for the following C function int triple_it (int...
C Practice 1: 1) Write a forward declaration for the following C function int triple_it (int x) { return (x * 3); } 2) What is C syntax to declare two variables, one called num of integer type and another called farray which is an array of 10 floating point numbers? 3) Write a C function array_max(int a[], int len) that takes an integer array & its length as its parameters and returns the largest value in the array. 4)...
Write a function in R named counts. This function should take as parameters a numeric vector...
Write a function in R named counts. This function should take as parameters a numeric vector x and also a number indicating a number of bins n. The function will consider the range [min(x),max(x)], and then consider a parti- tion of this interval into n non-overlapping equally sized half open intervals: I1 = [min(x),b1),I2 = [b1,b − 2),...,In = (bn−1,max(x)]. Note that since the intervals are equally sized, the value of bi is constrained. The function will then return a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT