Merge Sort

From this lesson on, we'll study some key sorting algorithms that are used in real-life projects—the first of which is merge sort!

We'll cover the following

Merge Sort

Merge sort is a recursive Divide & Conquer algorithm that essentially divides a given array into two halves, sorts those halves, and merges them together in order. The base case is to merge two arrays of size 1 so, eventually, single elements are merged in order; the merge part is where most of the heavy lifting happens.

Pseudocode

mergeSort(arr)
  if arr.length == 1
    return arr
  else:
    s1 = mergeSort(arr[0,n/2])
    s2 = mergeSort(arr[n/2+1,n])
 return merge(s1,s2)

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.