Merge Sort in C

Merge sort in C is a divide and conquer algorithm usually used to sort an array. So in this tutorial the code and algorithm for merge sort in C is discussed to make you understand what is merge sort?

Merge Sort Algorithm

merge sort algorithm in c

  1. Partition the array from the half into left and right sub-arrays.
  2. Repeat step-1 for both left and right sub-arrays, until the subarray contains only one element (one element cannot be partitioned :P).
  3. Merge left and right subarray by sorting the elements into one.
  4. Repeat step-3 until you get all the elements sorted in the final array.

Merge Sort Code in C

Online Compiler

Output

Overview of Merge Sort

  • Not an in-place algorithm (requres two extra arrays for splitting into two halves).
  • O(nlogn)-base 2 complexity. We are repeatedly dividing the array.
  • Stable algorithm (No interchange of duplicates values).

Also check Counting Sort in C++.

admin

I am a B.tech IT Student from India. Technology and programming is the most enthusiastic thing for me in this world. I like learning new techniques and use them for real-world application because I feel tech is future. Let's learn and grow TOGETHER.