## brief introduction

Merge sort is a recursive sort algorithm. The idea of this algorithm is to divide the array to be sorted into many small parts until these small parts are sorted arrays (arrays with only one element).

These sorted arrays are then combined to form a larger array. Then merge these larger merged arrays until the entire array is sorted.

## Examples of merge sort

If we have an array: 29,10,14,37,20,25,44,15, how can we merge and sort it?

First look at an animation:

Let’s analyze the running process of the above example in detail

First, the array is divided into two parts, [29,10,14,37] and [20,25,44,15].

[29,10,14,37] is further divided into two parts [29,10] and [14,37].

[29,10] is divided into two parts [29] and [10], and then [29] and [10] are merged and sorted to generate [10,29].

Similarly, merge and sort [14,37] to get [14,37].

Merge [10,29] and [14,37] again to get [10,14,29,37], and so on to get the final result.

## The thought of merge sort algorithm

Merge sort mainly uses the idea of divide and rule. Divide a large array into many sorted small arrays, and then merge the small arrays.

This divide process can use recursive algorithm, because the divide logic is the same for both large and small arrays.

And the logical part of sorting is actually merging this piece.

## Java implementation of merge sort

Let’s take a look at the core of merge

```
/**
*Merge two sorted arrays
*@ param array array array to be merged
*The starting point of the first part of the @ param low array
*The end of the first part of the @ param mid array is also the starting point of the second part - 1
*The end of the second part of the @ param high array
*/
private void merge(int[] array, int low, int mid, int high) {
//The length of the array to sort
int length = high-low+1;
//We need an extra array to store the sorted results
int[] temp= new int[length];
//Divided into left and right arrays
int left = low, right = mid+1, tempIdx = 0;
//Merge arrays
while (left <= mid && right <= high) {
temp[tempIdx++] = (array[left] <= array[right]) ? array[left++] : array[right++];
}
//One array is merged, and the remaining one is merged
while (left <= mid) temp[tempIdx++] = array[left++];
while (right <= high) temp[tempIdx++] = array[right++];
//Copy the sorted array back to the original array
for (int k = 0; k < length; k++) array[low+k] = temp[k];
}
```

You need to note that our elements exist in the original array, and the first parameter of the method is the original array.

The next three parameters are the indexes in the array that need to be merged and sorted. Three indexes divide the array into two parts: array [low to mid], array [mid + 1 to high].

The logic of merge is to merge the two arrays.

Because our array itself contains primitive data, we need an additional array space int [] temp to merge and sort.

By comparing the size of the elements in array [low to mid] and array [mid + 1 to high], insert the elements one by one into int [] temp, and finally copy the sorted array back to the original array, and merge is completed.

Then let’s take a look at the divide part. The divide part is actually a recursive call. At the end of the recursion, we need to call the merge method

```
public void doMergeSort(int[] array, int low, int high){
//Array to sort [low.. high]
//Use dichotomy for recursion, when the value of low is greater than or equal to the value of high, stop recursion
if (low < high) {
//Get the index of the intermediate value
int mid = (low+high) / 2;
//The first half of recursion
doMergeSort(array, low , mid );
//The last half of recursion
doMergeSort(array, mid+1, high);
//After recursion, merge the two parts of the sorted array
merge(array, low, mid, high);
log.info ("array after merge: {}", array));
}
}
```

Array is the original array. Low and high mark the starting position of the array to be sorted recursively.

Run the above results:

We can see that the output is consistent with the result of our animation.

## Time complexity of merge sort

Let’s look at the time complexity of merge sort.

First, look at the merge method. The merge method actually traverses two arrays, so the time complexity of the merge method is O (n).

Let’s look at the divide method

The divide method divides the sorting into logn layers, and each layer can be regarded as a combined sorting of n elements, so the time complexity of each layer is O (n).

All in all, the total time complexity is O (n logn).

Code address of this article:

This article has been included inhttp://www.flydean.com/algorithm-merge-sort/

The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you don’t know are waiting for you to discover!

Welcome to my official account: “those things in procedure”, understand technology, know you better!