Merge Sort in C

Last Updated Nov 20, 2015, 07:00:14 PM





What is is sorting?

Sorting is an algorithm that is used to sort the elements in a particular order by moving the wrong elements away from the queue. This technique sorts the adjacent elements and keeps them in the right order.

How Sorting Works?

Sorting algorithms work by moving elements to their final position, one at a time. You sort an array of size N, put 1 item in place, and continue sorting an array of size N – 1

Let us say there are N elements which are not in order. To sort those elements we need to perform N-1 iterations. At each iteration the largest elements will be moved to the last position and the smallest element will be moved to the first position.

Merge Sort in C

Merge-sort is very easy algorithm, it is purely based one theory that is divide-and-conquer.

Well, how merge-sort works? Take an array and divide it into two seperate parts or two subarrays, then sort each subarrays, after all just merge them into one array.

merge sort in c

How Merge Sort Works?

First it divides the input array into two seperate halves, and then divdes the the two seperate subarrays into other halves and then it merges back the two subarrays. You can use the The merge() function to merge two subarrays

Implementation of Merge Sort Let us say, we have an arrray called myarray[], where left,right are the left and right elements in the array Example