{
  "tag": "4MRG7S",
  "kind": "algorithm",
  "slug": "merge-sort",
  "name": "Merge sort",
  "aliases": [
    "mergesort"
  ],
  "area": "sorting/comparison-sorts",
  "level": "intro",
  "summary": "Sort an array by divide and conquer. Sort each half, then merge the two sorted halves into one sorted array.",
  "solves": [
    {
      "problem": "sorting-problem"
    }
  ],
  "model": "comparison-model",
  "complexity": {
    "vars": {
      "n": "the number of items"
    },
    "time": {
      "average": "Θ(n log n)",
      "best": "Θ(n log n)",
      "worst": "Θ(n log n)"
    },
    "space": {
      "extra": "Θ(n)"
    }
  },
  "properties": {
    "adaptive": false,
    "comparison": true,
    "deterministic": true,
    "in_place": false,
    "online": false,
    "recursive": true,
    "stable": true
  },
  "history": {
    "year": 1945,
    "people": [
      "John von Neumann"
    ],
    "note": "Von Neumann wrote a merge sort program for the EDVAC in 1945."
  },
  "prereqs": [
    "insertion-sort",
    "loop-invariant",
    "asymptotic-notation"
  ],
  "sources": [
    {
      "book": "isbn:9780262046305",
      "title": "Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, fourth edition",
      "locator": "Section 2.3"
    },
    {
      "book": "isbn:9780201896855",
      "title": "Donald E. Knuth, The Art of Computer Programming, Volume 3, second edition",
      "locator": "Section 5.2.4"
    }
  ],
  "see_also": [
    "insertion-sort",
    "binary-search"
  ],
  "url": "https://algorithm.tamnd.com/merge-sort/",
  "path": "nodes/sorting/comparison-sorts/merge-sort",
  "status": {
    "slug": "merge-sort",
    "h": {
      "level": 1,
      "max": 3,
      "label": "",
      "name": "",
      "next": [
        "a full proof of time.average",
        "a full proof of time.best",
        "a full proof of time.worst",
        "a full proof of space.extra"
      ]
    },
    "l": {
      "level": 0,
      "max": 4,
      "label": "",
      "name": "",
      "next": [
        "name a Lean module in the lean field (07)"
      ]
    },
    "i": {
      "c": {
        "level": 0,
        "max": 4,
        "label": "",
        "name": "",
        "next": [
          "I1 needs the shared cases of the contract and the JSON adapter (09.2), which are M0 work"
        ]
      },
      "cpp": {
        "level": 0,
        "max": 4,
        "label": "",
        "name": "",
        "next": [
          "I1 needs the shared cases of the contract and the JSON adapter (09.2), which are M0 work"
        ]
      },
      "csharp": {
        "level": 0,
        "max": 4,
        "label": "",
        "name": "",
        "next": [
          "I1 needs the shared cases of the contract and the JSON adapter (09.2), which are M0 work"
        ]
      },
      "go": {
        "level": 0,
        "max": 4,
        "label": "",
        "name": "",
        "next": [
          "I1 needs the shared cases of the contract and the JSON adapter (09.2), which are M0 work"
        ]
      },
      "java": {
        "level": 0,
        "max": 4,
        "label": "",
        "name": "",
        "next": [
          "I1 needs the shared cases of the contract and the JSON adapter (09.2), which are M0 work"
        ]
      },
      "javascript": {
        "level": 0,
        "max": 4,
        "label": "",
        "name": "",
        "next": [
          "I1 needs the shared cases of the contract and the JSON adapter (09.2), which are M0 work"
        ]
      },
      "kotlin": {
        "level": 0,
        "max": 4,
        "label": "",
        "name": "",
        "next": [
          "I1 needs the shared cases of the contract and the JSON adapter (09.2), which are M0 work"
        ]
      },
      "python": {
        "level": 0,
        "max": 4,
        "label": "",
        "name": "",
        "next": [
          "I1 needs the shared cases of the contract and the JSON adapter (09.2), which are M0 work"
        ]
      },
      "rust": {
        "level": 0,
        "max": 4,
        "label": "",
        "name": "",
        "next": [
          "I1 needs the shared cases of the contract and the JSON adapter (09.2), which are M0 work"
        ]
      },
      "typescript": {
        "level": 0,
        "max": 4,
        "label": "",
        "name": "",
        "next": [
          "I1 needs the shared cases of the contract and the JSON adapter (09.2), which are M0 work"
        ]
      }
    },
    "v": {
      "level": 1,
      "max": 3,
      "label": "",
      "name": "",
      "next": [
        "V2 needs the oracle check V03, line-trace agreement V06, the counter check V07 and the budgets V08 (10.8.2)"
      ]
    },
    "w": {
      "level": 0,
      "max": 5,
      "label": "",
      "name": "",
      "next": [
        "the lead paragraph"
      ]
    },
    "tests": {
      "c": {
        "slug": "merge-sort",
        "lang": "c",
        "pass": true,
        "output": "ok",
        "ms": 109
      },
      "cpp": {
        "slug": "merge-sort",
        "lang": "cpp",
        "pass": true,
        "output": "ok",
        "ms": 1919
      },
      "csharp": {
        "slug": "merge-sort",
        "lang": "csharp",
        "pass": true,
        "output": "Test run for /tmp/algo-test-csharp-4122288070/bin/Debug/net10.0/Impl.Tests.dll (.NETCoreApp,Version=v10.0)\nA total of 1 test files matched the specified pattern.\n\nPassed!  - Failed:     0, Passed:     3, Skipped:     0, Total:     3, Duration: 24 ms - Impl.Tests.dll (net10.0)",
        "ms": 2722
      },
      "go": {
        "slug": "merge-sort",
        "lang": "go",
        "pass": true,
        "output": "ok  \texample.com/impl\t0.004s",
        "ms": 318
      },
      "java": {
        "slug": "merge-sort",
        "lang": "java",
        "pass": true,
        "output": "Test run finished after 77 ms\n[         4 containers found      ]\n[         0 containers skipped    ]\n[         4 containers started    ]\n[         0 containers aborted    ]\n[         4 containers successful ]\n[         0 containers failed     ]\n[         4 tests found           ]\n[         0 tests skipped         ]\n[         4 tests started         ]\n[         0 tests aborted         ]\n[         4 tests successful      ]\n[         0 tests failed          ]",
        "ms": 1103
      },
      "javascript": {
        "slug": "merge-sort",
        "lang": "javascript",
        "pass": true,
        "output": "✔ example (1.555335ms)\n✔ edges (0.300893ms)\n✔ random against Array.prototype.sort (9.86972ms)\nℹ tests 3\nℹ suites 0\nℹ pass 3\nℹ fail 0\nℹ cancelled 0\nℹ skipped 0\nℹ todo 0\nℹ duration_ms 77.225863",
        "ms": 116
      },
      "kotlin": {
        "slug": "merge-sort",
        "lang": "kotlin",
        "pass": true,
        "output": "Test run finished after 113 ms\n[         4 containers found      ]\n[         0 containers skipped    ]\n[         4 containers started    ]\n[         0 containers aborted    ]\n[         4 containers successful ]\n[         0 containers failed     ]\n[         3 tests found           ]\n[         0 tests skipped         ]\n[         3 tests started         ]\n[         0 tests aborted         ]\n[         3 tests successful      ]\n[         0 tests failed          ]",
        "ms": 4853
      },
      "python": {
        "slug": "merge-sort",
        "lang": "python",
        "pass": true,
        "output": "....                                                                     [100%]\n4 passed in 0.02s",
        "ms": 607
      },
      "rust": {
        "slug": "merge-sort",
        "lang": "rust",
        "pass": true,
        "output": "running 0 tests\n\ntest result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s\n\n\nrunning 3 tests\n...\ntest result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s\n\n\nrunning 0 tests\n\ntest result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s",
        "ms": 662
      },
      "typescript": {
        "slug": "merge-sort",
        "lang": "typescript",
        "pass": true,
        "output": "✔ example (1.513493ms)\n✔ edges (0.212798ms)\n✔ random against Array.prototype.sort (8.915699ms)\nℹ tests 3\nℹ suites 0\nℹ pass 3\nℹ fail 0\nℹ cancelled 0\nℹ skipped 0\nℹ todo 0\nℹ duration_ms 121.591624",
        "ms": 159
      }
    }
  },
  "langs": [
    "c",
    "cpp",
    "csharp",
    "go",
    "java",
    "javascript",
    "kotlin",
    "python",
    "rust",
    "typescript"
  ],
  "proofs": [
    "correctness"
  ],
  "scene": true
}
