Merge sort is a recursive sorting algorithm and, luckily for us, it’s quite a bit faster than bubble sort. Merge sort is a  divide and conquer algorithm.

Divide

  • Divide the input slice into two (equal) halves
  • Recursively sort the two halves

Conquer

  • Merge the two halves to form a sorted array

merge sort gif

Full example of the merge sort algorithm

Merge sort actually has two functions involved, the recursive mergeSort function, and the merge function.

Let’s write the mergeSort() function first. It’s a recursive function, which means it calls itself, and in this case, it actually calls itself twice. The point of the mergeSort function is to split the array into two roughly equal parts, call itself on those parts, then call merge() to fit those halves back together.

#golang #go

Merge Sort in Golang with Examples
1.75 GEEK