Merge Sort

Merge Sort is an $$O(n log n)$$ sort that is based on divide and conquer. it is Stable, but is not Adaptive.

= Explanation =

The Merge
A Merge is an operation that takes 2 sorted arrays and merges them into 1 sorted array.

One implementation works like this:

Compare the first elements of both arrays.

If the beginning of the first array is smaller, move it to the output.

If not, then move beginning of the second array to the output.

if one array is empty, copy the other one to output.

Pseudocode for merge
procedure merge(array first, array second) do array out = [] while length(first) > 0 and length(second) > 0 do if first[0] < second[0] do out.push(first[0]) first = first.slice(1) else out.push(second[0]) second = second.slice(1) end if end while while length(first) > 0 do out.push(first[0]) first = first.slice(1) end while while length(second) > 0 do out.push(second[0]) second = second.slice(1) end while return out end