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:

Other Related C Programs :


* C Program to find whether the given number is Odd or Even

* C Program to find the Sum of Odd Digits

* C Program to find the Sum of Even Digits

* C Program to find the Sum of Even and Odd Digits,

* C Program to Display Odd digits

* C Program to Display even digits

* C Program to Display Odd Numbers,

* C Program to Display Even Numbers,

* C Program to Check whether the given number is Armstrong number

* C Program to display the Armstrong numbers in Range

* C Program to display the Armstrong numbers between 1 and 1000

* C Program to check perfect number, perfect numbers in range

* C Program to Check Prime number, Prime numbers between 1 and 100

* C Program to display the Prime numbers in a Range

* C Program to display the Sum of Prime numbers

* C Program to Check Strong number

* C Program to display the Strong number in Range

* C Program to Check whether the given number is polindrome number or not

* C Program to Check polindrome number using for loop

* C Program to Check polindrome number using Recursion

* C Program to  check whether the given string is polindrome or not

* C Program to find the Roots of Quadratic equation

* C Program to display Fibonacci Series

* C Program to display the Fibonacci Series using an array

* C Program to display the Fibonacci Series using while loop

* C Program to find the Sum of Fibonacci Series, Factorial of Number

* C Program to find the Factorial without using Recursion

* C Program to find the Factorial using Pointers

* C Program to find the Factorial using Function

Bookmark and Share