{
  "tag": "3NS7RT",
  "kind": "algorithm",
  "slug": "insertion-sort",
  "name": "Insertion sort",
  "area": "sorting/comparison-sorts",
  "level": "intro",
  "summary": "Sort an array one item at a time. Take the next item and move it left until the item before it is not larger.",
  "solves": [
    {
      "problem": "sorting-problem"
    }
  ],
  "model": "comparison-model",
  "complexity": {
    "vars": {
      "d": "the number of inversions in the input",
      "n": "the number of items"
    },
    "time": {
      "average": "Θ(n²)",
      "best": "Θ(n)",
      "total": "Θ(n + d)",
      "worst": "Θ(n²)"
    },
    "space": {
      "extra": "Θ(1)"
    }
  },
  "properties": {
    "adaptive": true,
    "comparison": true,
    "deterministic": true,
    "in_place": true,
    "online": true,
    "recursive": false,
    "stable": true
  },
  "prereqs": [
    "loop-invariant",
    "asymptotic-notation"
  ],
  "sources": [
    {
      "book": "isbn:9780262046305",
      "title": "Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, fourth edition",
      "locator": "Section 2.1"
    },
    {
      "book": "isbn:9780201896855",
      "title": "Donald E. Knuth, The Art of Computer Programming, Volume 3, second edition",
      "locator": "Section 5.2.1, Algorithm S"
    }
  ],
  "see_also": [
    "merge-sort",
    "binary-search"
  ],
  "url": "https://algorithm.tamnd.com/insertion-sort/",
  "path": "nodes/sorting/comparison-sorts/insertion-sort",
  "status": {
    "slug": "insertion-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.total",
        "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": [
          "the native tests did not run in this build",
          "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": [
          "the native tests did not run in this build",
          "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": [
          "the native tests did not run in this build",
          "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": "insertion-sort",
        "lang": "c",
        "pass": true,
        "output": "ok",
        "ms": 843
      },
      "cpp": {
        "slug": "insertion-sort",
        "lang": "cpp",
        "pass": true,
        "output": "ok",
        "ms": 9684
      },
      "go": {
        "slug": "insertion-sort",
        "lang": "go",
        "pass": true,
        "output": "ok  \texample.com/impl\t0.419s",
        "ms": 3467
      },
      "javascript": {
        "slug": "insertion-sort",
        "lang": "javascript",
        "pass": true,
        "output": "✔ example (10.68075ms)\n✔ edges (1.573042ms)\n✔ random against Array.prototype.sort (52.639ms)\nℹ tests 3\nℹ suites 0\nℹ pass 3\nℹ fail 0\nℹ cancelled 0\nℹ skipped 0\nℹ todo 0\nℹ duration_ms 1361.5065",
        "ms": 1766
      },
      "python": {
        "slug": "insertion-sort",
        "lang": "python",
        "pass": true,
        "output": "....                                                                     [100%]\n4 passed in 0.44s",
        "ms": 6788
      },
      "rust": {
        "slug": "insertion-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.01s\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": 6521
      },
      "typescript": {
        "slug": "insertion-sort",
        "lang": "typescript",
        "pass": true,
        "output": "✔ example (103.680084ms)\n✔ edges (0.485ms)\n✔ random against Array.prototype.sort (35.11475ms)\nℹ tests 3\nℹ suites 0\nℹ pass 3\nℹ fail 0\nℹ cancelled 0\nℹ skipped 0\nℹ todo 0\nℹ duration_ms 2466.518916",
        "ms": 3508
      }
    }
  },
  "langs": [
    "c",
    "cpp",
    "csharp",
    "go",
    "java",
    "javascript",
    "kotlin",
    "python",
    "rust",
    "typescript"
  ],
  "proofs": [
    "correctness"
  ],
  "scene": true
}
