Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

marge sort in c language

#include <stdio.h>
void mergesort();
void merge();
void main()
{
    int n;
    printf("
enter size of array : ");
    scanf("%d", &n);
    int a[n], i;
    printf("
enter element :
");
    for (i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    mergesort(a, 0, n - 1);
    printf("
your sorted array on based  merge sort :
");
    for (i = 0; i < n; i++)
    {
        printf("	%d", a[i]);
    }
    printf("

");
}
void mergesort(int a[], int first, int last)
{
    int mid;
    if (first < last)
    {
        mid = (first + last) / 2;
        mergesort(a, first, mid);
        mergesort(a, mid + 1, last);
        merge(a, first, mid, last);
    }
}
void merge(int a[], int first, int mid, int last)
{
    int b[50];
    int i, j, k;
    i = first;
    j = mid+1;
    k = first;
    while (i <= mid && j <= last)
    {
        if (a[i] <= a[j])
        {
            b[k++] = a[i++];
        }
        else
        {
            b[k++] = a[j++];
        }
    }
    if (i > mid)
    {
        while (j <= last)
        {
            b[k++] = a[j++];
        }
    }
    else
    {
        while (i <= mid)
        {
            b[k++] = a[i++];
        }
    }
    for (i = first; i <= last; i++)
    {
        a[i] = b[i];
    }
}
 
PREVIOUS NEXT
Tagged: #marge #sort #language
ADD COMMENT
Topic
Name
8+2 =