13323 - RSTDS110 Merge Sort   

Description

Please implement merge sort by C++ code.

 

Fill in the blanks marked "//TODO"

Template:

/* Iterative C program for merge sort */
#include<iostream>
using namespace std;

/* 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);

// Utility function to find minimum of two integers
int min(int x, int y) { return (x<y)? x :y; }

/* Function to print an array */
void printArray(int A[], int size)
{
    int i;
    for (i=0; i < size; i++)
    cout <<A[i]<<" ";
    cout <<"\n";
}

/* Iterative mergesort function to sort arr[0...n-1] */
void mergeSort(int arr[], int n)
{
    int curr_size; // For current size of subarrays to be merged
                    // curr_size varies from 1 to n/2
    int left_start; // For picking starting index of left subarray
                    // to be merged

    // Merge subarrays in bottom up manner. First merge subarrays of
    // size 1 to create sorted subarrays of size 2, then merge subarrays
    // of size 2 to create sorted subarrays of size 4, and so on.
    for (curr_size=1; curr_size<=n-1; curr_size = 2*curr_size)
    {
        // Pick starting point of different subarrays of current size
        for (left_start=0; left_start<n-1; left_start += 2*curr_size)
        {
            //TODO
        }
        printArray(arr, n);
    }
}

/* 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)
{
    //TODO
}

 

/* Driver program to test above functions */
int main()
{
    int a;
    cin>>a;
    int arr[a];
    for (int i = 0;i<a;i++)
        cin>>arr[i];
    mergeSort(arr, a);
    return 0;
}
// This code is contributed shivanisinghss2110

Hint :

  1. Assume that all input parameters would be valid.

  2. Implement the functions below:

    1. mergeSort() : 

    2. merge() : 

  3. You can’t use STL.

  4. You must use 2-way merge to implement iterative merge sort

  5. You must use merge sort to sort the array and output the results of each round of merge sort in the sorting process.

  6. You don't have to use template code (you can use it or not), as long as you can output the correct result

  7. The numbers to be sorted will be integers and will not repeat

 

Input

1.First, you have to input the number of integers you want to sort

 

2.Second, you have to enter the sequence to be sorted

 

3.Suppose the number sequence we want to sort is 11, 5, 8, 3, 7

First, you have to input 5 (because the number of integers is 5)

Second, you have to input 11 5 8 3 7(the sequence we want to sort)

So, the actual input format is as follows:

5

11 5 8 3 7

 

Output

1.Output the results of each round of merge sort.

2.Taking the above Input example as an example, the results shown below will be output (take 2-way merge as an example) : 

5 11 3 8 7

3 5 8 11 7

3 5 7 8 11

 

Explanation : 

(For the output format, please refer to printarray)

5 11 3 8 7            ----->  pass1 of the merge sort

3 5 8 11 7            ----->  pass2 of the merge sort

3 5 7 8 11            ----->  pass3 of the merge sort (final result)

 

Sample Input  Download

Sample Output  Download

Tags




Discuss