Merge sort is another sorting technique that works on the divide
and conquer technique. In this technique, the complete list is
divided into sublists and each sublist is having one element. The
sublists are then merged to produce a new sorted sublist, and
this process is repeated until we get one complete sorted list.
Example
Let's take an example to understand how merge sort works.
Consider an array with nine elements that we want to sort in
ascending order:
10
4
2
1
5
6
7
8
15
The first step is to divide the given list into sublists. We divide the
list into two equal subarrays, and each sublist is further divided
into two halves until we get sublists with only one element. The
second step is to merge the sublists to get a complete sorted list.
Merge Sort Algorithm Code
void mergeSort(int arr[], int l, int r) { if
(l < r) { int m = l+(r-l)/2;