**Merge Sort** is one of the sorting techniques in the data structure. It is a simple technique to sort the element. merge sort is also used for sorting two individual unsorted lists to the sorted list. It merges the list. and we will see **Merge sort program in c** with an explanation.

## Merge sort technique

In this technique we have an N number of an unsorted list in this, We divide each element into a separate partition.

First, We divide the whole list into two arrays then that two arrays are divided separately and sort each element after that we merge the partitioned set to one single array. Finally, We will get our sorted array by merge sort technique.

## Merge sort algorithm with example

- Take a set of number an array.
- Divide the array into equal parts.
- Consider these two parts separately
- Now, Take the first part and partition the number until we have only one element left then start combining each number by comparing and arrange in order.
- For the second part also, Partition each number until one number left then start combining each number by comparing and arranging in order.
- After this, Compare the two sorted array and arrange into a one sorted list.
- Finish

**Example:**

**Output:**

**Sorted list – **3 9 10 27 38 43 82

In this way, we will get the sorted list.

The passes or steps required for implementing merge sort is N -1.

The **time complexity** for merge sort is **O(n log n)** for the worst case.

## Advantages of merge sort

- It is very simple and easy sorting technique.
- Works very good for large data.

## Disadvantage of merge sort

- This technique requires extra space for implementation.
- It is less efficient.

## Merge sort program in c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#include<stdio.h> void mergesort(int a[],int i,int j); void merge(int a[],int i1,int j1,int i2,int j2); int main() { int a[30],n,i; printf("Enter no of elements:"); scanf("%d",&n); printf("Enter array elements:"); for(i=0;i<n;i++) scanf("%d",&a[i]); mergesort(a,0,n-1); printf("\nSorted array is :"); for(i=0;i<n;i++) printf("%d ",a[i]); return 0; } void mergesort(int a[],int i,int j) { int mid; if(i<j) { mid=(i+j)/2; mergesort(a,i,mid); //left recursion mergesort(a,mid+1,j); //right recursion merge(a,i,mid,mid+1,j); //merging of two sorted sub-arrays }} void merge(int a[],int i1,int j1,int i2,int j2) { int temp[50]; //array used for merging int i,j,k; i=i1; //beginning of the first list j=i2; //beginning of the second list k=0; while(i<=j1 && j<=j2) //while elements in both lists { if(a[i]<a[j]) temp[k++]=a[i++]; else temp[k++]=a[j++]; } while(i<=j1) //copy remaining elements of the first list temp[k++]=a[i++]; while(j<=j2) //copy remaining elements of the second list temp[k++]=a[j++]; //Transfer elements from temp[] back to a[] for(i=i1,j=0;i<=j2;i++,j++) a[i]=temp[j]; } |

**Output:**

**Enter number of elements:**

**5**

**Enter array elements**

**10 2 30 4 1**

**Sorted array is: 1 2 4 10 30**

## Leave a Reply