{"commit":"d49f147257af058c24e26685e6bc5943782b6014","nodes":[{"tag":"8ASYMP","kind":"definition","slug":"asymptotic-notation","name":"Asymptotic notation","aliases":["big O notation","Landau notation","big Theta","big Omega"],"area":"foundations/asymptotic-analysis","level":"intro","summary":"The notations O, Ω and Θ compare the growth of two functions for large inputs and ignore constant factors.","sources":[{"book":"isbn:9780262046305","title":"Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, fourth edition","locator":"Chapter 3"},{"paper":"doi:10.1145/1008328.1008329","title":"Donald E. Knuth, Big Omicron and big Omega and big Theta (1976)"}],"url":"https://algorithm.tamnd.com/asymptotic-notation/","path":"nodes/foundations/asymptotic-analysis/asymptotic-notation","status":{"slug":"asymptotic-notation","h":{"level":0,"max":3,"label":"","name":"","next":["add a proof in proofs/ (06.5)"]},"l":{"level":0,"max":4,"label":"","name":"","next":["name a Lean module in the lean field (07)"]},"i":{},"v":{"level":0,"max":3,"label":"","name":"","next":["add viz/scene.ts or a figure (10)"]},"w":{"level":0,"max":5,"label":"","name":"","next":["the lead paragraph"]}},"scene":false},{"tag":"2B7QDX","kind":"algorithm","slug":"binary-search","name":"Binary search","aliases":["half-interval search","logarithmic search","bisection search"],"area":"searching/sequential-and-binary-search","level":"intro","summary":"Find a key in a sorted array. Compare the key with the middle item, and keep only the half that can hold the key.","solves":[{"problem":"search-in-sorted-array"}],"model":"comparison-model","complexity":{"vars":{"n":"the number of items in the array"},"time":{"average":"Θ(log n)","best":"Θ(1)","worst":"Θ(log n)"},"space":{"extra":"Θ(1)"}},"properties":{"comparison":true,"deterministic":true,"recursive":false},"history":{"year":1946,"people":["John Mauchly","Derrick Henry Lehmer"],"note":"Mauchly described the method in the Moore School Lectures in 1946. Lehmer gave a version for any n in 1960."},"prereqs":["loop-invariant","asymptotic-notation"],"sources":[{"book":"isbn:9780201896855","title":"Donald E. Knuth, The Art of Computer Programming, Volume 3, second edition","locator":"Section 6.2.1, Algorithm B"},{"book":"isbn:9780262046305","title":"Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, fourth edition","locator":"Exercise 2.3-6"},{"url":"https://research.google/blog/extra-extra-read-all-about-it-nearly-all-binary-searches-and-mergesorts-are-broken/","title":"Joshua Bloch, Nearly all binary searches and mergesorts are broken (2006)","note":"the overflow of (lo + hi) / 2"}],"see_also":["insertion-sort","merge-sort"],"url":"https://algorithm.tamnd.com/binary-search/","path":"nodes/searching/sequential-and-binary-search/binary-search","status":{"slug":"binary-search","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":["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 fail","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":"binary-search","lang":"c","pass":true,"output":"ok","ms":1639},"cpp":{"slug":"binary-search","lang":"cpp","pass":true,"output":"ok","ms":8986},"go":{"slug":"binary-search","lang":"go","pass":true,"output":"ok  \texample.com/impl\t0.378s","ms":3502},"java":{"slug":"binary-search","lang":"java","pass":false,"output":"The operation couldn’t be completed. Unable to locate a Java Runtime.\nPlease visit http://www.java.com for information on installing Java.","ms":772},"javascript":{"slug":"binary-search","lang":"javascript","pass":true,"output":"✔ examples (1.445333ms)\n✔ edges (0.249958ms)\n✔ random against a linear scan (100.703042ms)\nℹ tests 3\nℹ suites 0\nℹ pass 3\nℹ fail 0\nℹ cancelled 0\nℹ skipped 0\nℹ todo 0\nℹ duration_ms 493.931125","ms":759},"python":{"slug":"binary-search","lang":"python","pass":true,"output":".....                                                                    [100%]\n5 passed in 0.34s","ms":3627},"rust":{"slug":"binary-search","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":2717},"typescript":{"slug":"binary-search","lang":"typescript","pass":true,"output":"✔ examples (1.262291ms)\n✔ edges (1.571208ms)\n✔ random against a linear scan (13.226458ms)\nℹ tests 3\nℹ suites 0\nℹ pass 3\nℹ fail 0\nℹ cancelled 0\nℹ skipped 0\nℹ todo 0\nℹ duration_ms 904.868792","ms":1202}}},"langs":["c","cpp","csharp","go","java","javascript","kotlin","python","rust","typescript"],"proofs":["correctness"],"scene":true},{"tag":"9CMPMD","kind":"model","slug":"comparison-model","name":"Comparison model","aliases":["decision tree model","comparison-based model"],"area":"foundations/models-of-computation","level":"intro","summary":"A model where an algorithm learns about its input only by comparing two items. The cost is the number of comparisons.","sources":[{"book":"isbn:9780262046305","title":"Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, fourth edition","locator":"Section 8.1"},{"book":"isbn:9780201896855","title":"Donald E. Knuth, The Art of Computer Programming, Volume 3, second edition","locator":"Section 5.3.1"}],"url":"https://algorithm.tamnd.com/comparison-model/","path":"nodes/foundations/models-of-computation/comparison-model","status":{"slug":"comparison-model","h":{"level":0,"max":3,"label":"","name":"","next":["add a proof in proofs/ (06.5)"]},"l":{"level":0,"max":4,"label":"","name":"","next":["name a Lean module in the lean field (07)"]},"i":{},"v":{"level":0,"max":3,"label":"","name":"","next":["add viz/scene.ts or a figure (10)"]},"w":{"level":0,"max":5,"label":"","name":"","next":["the lead paragraph"]}},"scene":false},{"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},{"tag":"7NVRNT","kind":"concept","slug":"loop-invariant","name":"Loop invariant","aliases":["invariant"],"area":"foundations/correctness-and-invariants","level":"intro","summary":"A statement about the variables of a loop that is true before each step. It is the main tool to prove that a loop is correct.","sources":[{"book":"isbn:9780262046305","title":"Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, fourth edition","locator":"Section 2.1"},{"paper":"doi:10.1145/363235.363259","title":"C. A. R. Hoare, An axiomatic basis for computer programming (1969)"}],"url":"https://algorithm.tamnd.com/loop-invariant/","path":"nodes/foundations/correctness-and-invariants/loop-invariant","status":{"slug":"loop-invariant","h":{"level":0,"max":3,"label":"","name":"","next":["add a proof in proofs/ (06.5)"]},"l":{"level":0,"max":4,"label":"","name":"","next":["name a Lean module in the lean field (07)"]},"i":{},"v":{"level":0,"max":3,"label":"","name":"","next":["add viz/scene.ts or a figure (10)"]},"w":{"level":0,"max":5,"label":"","name":"","next":["the lead paragraph"]}},"scene":false},{"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":["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":"merge-sort","lang":"c","pass":true,"output":"ok","ms":3090},"cpp":{"slug":"merge-sort","lang":"cpp","pass":true,"output":"ok","ms":13802},"go":{"slug":"merge-sort","lang":"go","pass":true,"output":"ok  \texample.com/impl\t0.325s","ms":1531},"javascript":{"slug":"merge-sort","lang":"javascript","pass":true,"output":"✔ example (2.866167ms)\n✔ edges (0.436458ms)\n✔ random against Array.prototype.sort (22.242167ms)\nℹ tests 3\nℹ suites 0\nℹ pass 3\nℹ fail 0\nℹ cancelled 0\nℹ skipped 0\nℹ todo 0\nℹ duration_ms 745.520917","ms":1106},"python":{"slug":"merge-sort","lang":"python","pass":true,"output":"....                                                                     [100%]\n4 passed in 0.45s","ms":4806},"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.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":2538},"typescript":{"slug":"merge-sort","lang":"typescript","pass":true,"output":"✔ example (103.628334ms)\n✔ edges (0.324458ms)\n✔ random against Array.prototype.sort (13.558209ms)\nℹ tests 3\nℹ suites 0\nℹ pass 3\nℹ fail 0\nℹ cancelled 0\nℹ skipped 0\nℹ todo 0\nℹ duration_ms 831.408041","ms":1258}}},"langs":["c","cpp","csharp","go","java","javascript","kotlin","python","rust","typescript"],"proofs":["correctness"],"scene":true},{"tag":"6SRCHA","kind":"problem","slug":"search-in-sorted-array","name":"Search in a sorted array","aliases":["sorted search","ordered search"],"area":"searching/sequential-and-binary-search","level":"intro","summary":"Given a sorted array and a key, find an index of the key, or report that the key is not in the array.","model":"comparison-model","complexity":{"vars":{"n":"the number of items"},"time":{"worst":"Ω(log n)"}},"sources":[{"book":"isbn:9780201896855","title":"Donald E. Knuth, The Art of Computer Programming, Volume 3, second edition","locator":"Section 6.2.1"}],"url":"https://algorithm.tamnd.com/search-in-sorted-array/","path":"nodes/searching/sequential-and-binary-search/search-in-sorted-array","status":{"slug":"search-in-sorted-array","h":{"level":0,"max":3,"label":"","name":"","next":["add a proof in proofs/ (06.5)"]},"l":{"level":0,"max":4,"label":"","name":"","next":["name a Lean module in the lean field (07)"]},"i":{},"v":{"level":0,"max":3,"label":"","name":"","next":["add viz/scene.ts or a figure (10)"]},"w":{"level":0,"max":5,"label":"","name":"","next":["the lead paragraph"]}},"scene":false},{"tag":"5SRTPB","kind":"problem","slug":"sorting-problem","name":"Sorting","aliases":["sort","the sorting problem"],"area":"sorting/comparison-sorts","areas":["sorting"],"level":"intro","summary":"Put the items of a sequence in non-decreasing order by a key.","model":"comparison-model","complexity":{"vars":{"n":"the number of items"},"time":{"worst":"Ω(n log n)"}},"sources":[{"book":"isbn:9780201896855","title":"Donald E. Knuth, The Art of Computer Programming, Volume 3, second edition","locator":"Chapter 5"},{"book":"isbn:9780262046305","title":"Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, fourth edition","locator":"Part II"}],"url":"https://algorithm.tamnd.com/sorting-problem/","path":"nodes/sorting/comparison-sorts/sorting-problem","status":{"slug":"sorting-problem","h":{"level":0,"max":3,"label":"","name":"","next":["add a proof in proofs/ (06.5)"]},"l":{"level":0,"max":4,"label":"","name":"","next":["name a Lean module in the lean field (07)"]},"i":{},"v":{"level":0,"max":3,"label":"","name":"","next":["add viz/scene.ts or a figure (10)"]},"w":{"level":0,"max":5,"label":"","name":"","next":["the lead paragraph"]}},"scene":false}],"v":1}