Merge Sort Algorithm C Program

By | December 12, 2015

C Program For Merge Sort Algorithm in Data Structure

Learn How To Sort Two Integer Arrays using Merge Sort Algorithm in C Programming Language. It is important that we should know about How A For Loop Works before getting further with the C Program Code. The merge sort program in C language takes in two arrays as input, sorts them and stores it in the third array.

What is Merge Sort Algorithm?

Merge Sort Technique was designed by Jon Von Neumann in 1945. The objective of this algorithm is to merge Two already Sorted Lists and combine them in a Single Sorted List. The Merge Sort Algorithm works on Divide and Conquer Algorithm where a given problem is divided into smaller subdivisions.

One element from each of the arrays is taken and compared. The element that is the smaller is copied into the third array. This process is continued till the elements of one array gets copied into the third array. Later, the remaining elements of the array are copied into the third array.

Note: The first and second arrays should consist of sorted elements. If the elements are not in sorted order, then the merge sort in c will not work.

There are two types of Merge Sort techniques:

  • Top Down Merge Sort
  • Bottom Up Merge Sort

Merge Sort Example

Array 1: 10 20 30 40 50

Array 2: 60 70 80 90 100 120 140

Sorted Array: 10 20 30 40 50 60 70 80 90 100 120 140

Merge Sort Algorithm Analysis

Merge Sort is a Stable Sort. Merge Sorting Algorithm is also not an In-Place Sort. It requires O(n) Extra Space. In every pass, there will be merging of n Elements and therefore the performance of the Algorithm is O(n log2n). It has O(n log n) performance in both Worst Case Scenario and Average Case Scenario.

 

C Program To Sort Two Arrays using Merge Sort Algorithm

 

Output

Sort Two Arrays using Merge Sort Algorithm in C Programming

If you have any compilation errors or doubts in this code to sort array using Merge Sort C Program in Data Structure, let us know about in the comment section below.

Sorting Algorithms
C Program For Quick Sort
C Program For Shell Sort
C Program For Address Calculation Sort
C Program For Insertion Sort
C Program For Selection Sort
C Program For Banker’s Algorithm
C Program For Bubble Sort
C Program For Heap Sort Algorithm
C Program For Topological Sorting
C Program To For Radix Sort
C Program For Counting Sort

2 thoughts on “Merge Sort Algorithm C Program

  1. Vaibhavi Shirke

    The merge sort in c programming algorithm is quite easy actually. I was confused on comparison of elements. Nice code.

    Reply
  2. Vinayak

    There are a fee disadvantages with Merge Sorting algorithm.
    1. This algorithm makes too many recursive attempts.
    2. It is an in-place sorting algorithm that needs 0(n) extra memory for sorting the elements.

    Reply

Let's Discuss