Q:

Write a C Program to Merge Sort of two different arrays

0

Write a C Program to Merge Sort of two different arrays. Here’s simple program to Merge Sort of two different arrays in C Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

What is an Array ?


Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

 
 

Instead of declaring individual variables, such as number0, number1, …, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to represent individual variables. A specific element in an array is accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.


Here is source code of the C Program to Merge Sort of two different arrays. The C program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.

 

SOURCE CODE : :

/*  C Program to Merge Sort of two different arrays */

#include <stdio.h>

void Merge(int * , int , int , int );

void MergeSort(int *array, int left, int right)
{
    int middle = (left+right)/2;
    /* We have to sort only when left<right because when left=right it is anyhow sorted*/
    if(left<right)
    {
        /* Sort the left part */
        MergeSort(array, left, middle);
        /* Sort the right part */
        MergeSort(array, middle + 1, right);
        /* Merge the two sorted parts */
        Merge(array, left, middle, right);
    }
}
/* Merge functions merges the two sorted parts */
void Merge(int *array, int left, int middle, int right)
{
    /*to store sorted array*/
    int tmp[right - left + 1];
    int pos = 0, leftposition = left, rightposition = middle + 1;
    while (leftposition <= middle && rightposition <= right)
    {
        if (array[leftposition] < array[rightposition])
        {
            tmp[pos++] = array[leftposition++];
        }
        else
        {
            tmp[pos++] = array[rightposition++];
        }
    }
    while (leftposition <= middle)
        tmp[pos++] = array[leftposition++];
    while (rightposition <= right)
        tmp[pos++] = array[rightposition++];
    int i;
    /* Copy back the sorted array to the original array */
    for (i = 0; i < pos; i++)
    {
        array[i + left] = tmp[i];
    }
    return;
}
int main()
{
    int size;
    printf("Enter the size of an array :: ");
    scanf("%d", &size);
    int array[size];
    int i, j, k;
    printf("\nEnter the array elements :: \n");
    for (i = 0; i < size; i++)
    {
        printf("\nEnter %d array element :: ",i+1);
        scanf("%d", &array[i]);
    }
    /* Calling this functions sorts the array */
    MergeSort(array, 0, size - 1);
    printf("\nAfter Merge Sort :: ");
    for (i = 0; i < size; i++)
    {
        printf("%d ", array[i]);
    }
    printf("\n");
    return 0;
}

Output : :


/* C Program to Merge Sort of two different arrays */

Enter the size of an array :: 7

Enter the array elements ::

Enter 1 array element :: 5

Enter 2 array element :: 3

Enter 3 array element :: 6

Enter 4 array element :: 8

Enter 5 array element :: 2

Enter 6 array element :: 8

Enter 7 array element :: 1

After Merge Sort :: 1 2 3 5 6 8 8

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Arrays Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a C Program to find Mean, Variance and Stand... >>
<< Write a C Program to Calculate Addition of All Ele...