# C Program to Sort a given N numbers using Merge Sort

## C Program to Sort a given N numbers using Merge Sort

This page has a C Program to Sort a given N numbers using Merge Sort. It is clear that, this program has to read N numbers as input and then sort them using Merge sort algorithm. Merge sort is a sorting method which based on divide and conquer technique. In the Merge sort algorithm, first divides the array into equal halves and then combines them in a sorted order.  The below given C program is used to Sort a given N numbers using Merge Sort.  Kindly check out the program to Sort the given N numbers using Merge Sort.

## How can ask to Sort a given N numbers using Merge Sort

The questions to Sort a given N numbers using Merge Sort can be asked as,  1). C program to Sort N numbers using Merge Sort.  2). C Program to Sort any given N numbers using Merge Sort.   3). C Program to Sort a set of numbers using Merge Sort.  4). C Program to pair of numbers using Merge Sort.  5). C program to arrange the given N numbers using Merge Sort.  6). C program to list the given N numbers using Merge Sort.  7). C program to print the given N numbers in an order using Merge Sort

## Program to Sort a given N numbers using Merge Sort

#include<stdio.h>
void Sub_Divide(int array[], int min, int max);
void Merge_Sort(int arrays[], int min, int mid_element, int max);
int main(){
int ar[30],a,total;
clrscr();
printf("Enter number of elements: ");
scanf("%d",&total);
printf("\n Enter the elements one by one : \n");
for(a=1;a<=total;a++){
scanf("%d",&ar[a]);
}
Sub_Divide(ar,1,total);
printf("\n The Sorted elements using merge sort are: \n");
for(a=1;a<=total;a++){
printf(" %d ",ar[a]);
}
getch();
return 0;
}

void Sub_Divide(int array[], int min, int max){
int middle;
if(min<max){
middle=(min+max)/2;
Sub_Divide(array, min, middle);
Sub_Divide(array, middle+1, max);
Merge_Sort(array, min, middle, max);
}
}

void Merge_Sort(int arrays[], int min, int mid_element, int max){
int a,b,mid,lows,temp_array[30];
a=min;
lows=min;
mid=mid_element+1;
do {
if(arrays[lows]<=arrays[mid]){
temp_array[a]=arrays[lows];
lows++;
}
else {
temp_array[a]=arrays[mid];
mid++;
}
a++;
}while((lows<=mid_element)&&(mid<=max));
if(lows>mid_element){
for(b=mid;b<=max;b++){
temp_array[a]=arrays[b];
a++;
}
}
else{
for(b=lows;b<=mid_element;b++){
temp_array[a]=arrays[b];
a++;
}
}
for(b=min;b<=max;b++){
arrays[b]=temp_array[b];
}
}

Sample Outputs:

Enter number of elements: 6
Enter the elements one by one :
4 6 3 8 2 7

The Sorted elements using merge sort are:
2 3 4 6 7 8

Related Popular Posts: