Merge Sort

Algorithm:

The merge sort algorithm works as follows:

Estimation:

Code and Explanation:

The difference from the default imperative implementation is the way of splitting a list into two parts. Usually this is solved by using the length of the list, whose cost is O(n), since it requires traversing from beginning to end. This implementation splits the list into two equal parts using the halve function. This function does a simple thing — it takes the first two elements of the list and adds them to two separate lists that accumulate the result. In other words, the function adds all even-positioned nodes to the first part, and odd-positioned nodes to the second. This can be done in O(n).

def mergeSort[A: Ordering](list: List[A]): List[A] =
  def halveAndSort(as: List[A]) = sort(halve(as))

  def sort(p: (List[A], List[A])): List[A] = p match
    case (Nil, Nil)       => Nil
    case (ha :: Nil, Nil) => ha :: Nil
    case (Nil, hb :: Nil) => hb :: Nil
    case (as, bs)         => merge(halveAndSort(as), halveAndSort(bs))

  def halve(as: List[A]): (List[A], List[A]) =
    @tailrec
    def loop(bs: List[A], fs: List[A], ss: List[A]): (List[A], List[A]) =
      bs match
        case f :: s :: r => loop(r, f :: fs, s :: ss)
        case f :: Nil    => (f :: fs, ss)
        case Nil         => (fs, ss)

    loop(as, Nil, Nil)

  def merge(as: List[A], bs: List[A]): List[A] =
    @tailrec
    def loop(cs: List[A], ds: List[A], r: List[A]): List[A] = (cs, ds) match
      case (Nil, Nil) => r.reverse
      case (_, Nil)   => r.reverse ++ cs
      case (Nil, _)   => r.reverse ++ ds
      case (ha :: ta, hb :: tb) =>
        if ha < hb then loop(ta, ds, ha :: r)
        else loop(cs, tb, hb :: r)

    loop(as, bs, Nil)

  halveAndSort(list)
end mergeSort

mergeSort(List(3, 1, 4, 2))
// List(1, 2, 3, 4)

Visualization:

Let's see how the algorithm works on the example of the list 3, 1, 4, 2. First, the list is recursively divided in half by the halve function, until each part has one element, then the sorted parts are joined by the merge function:

Step Action Result
1 halve(3, 1, 4, 2) left: 1, 3, right: 2, 4
2 halve(1, 3) left: 3, right: 1
3 halve(2, 4) left: 4, right: 2
4 merge(3, 1) 1, 3
5 merge(4, 2) 2, 4
6 merge(1, 3 and 2, 4) 1, 2, 3, 4
flowchart TD
    S0["3, 1, 4, 2"]

    subgraph step1[Step 1 — halving]
        H1["1, 3"]
        H2["2, 4"]
    end

    subgraph step2[Step 2 — splitting into single elements]
        D1["3"]
        D2["1"]
        D3["4"]
        D4["2"]
    end

    subgraph step3[Steps 4, 5 — merging pairs]
        M1["1, 3"]
        M2["2, 4"]
    end

    subgraph step4[Step 6 — final merge]
        M3["1, 2, 3, 4"]
    end

    S0 --> H1
    S0 --> H2
    H1 --> D1
    H1 --> D2
    H2 --> D3
    H2 --> D4
    D1 --> M1
    D2 --> M1
    D3 --> M2
    D4 --> M2
    M1 --> M3
    M2 --> M3

    style M1 fill:#d9ead3,stroke:#333
    style M2 fill:#d9ead3,stroke:#333
    style M3 fill:#c9daf8,stroke:#333

Green highlights sorted parts obtained by merging pairs, and the final sorted list is marked in blue.


Links: