{"id":22352523,"url":"https://github.com/bgoonz/recursion-practice-website","last_synced_at":"2025-03-26T12:12:00.304Z","repository":{"id":43211124,"uuid":"341652085","full_name":"bgoonz/Recursion-Practice-Website","owner":"bgoonz","description":"Recursion Prompts With Solutions and Dependency-Free Live Mocha Spec Updates.... rendered to the webpage","archived":false,"fork":false,"pushed_at":"2021-10-14T19:31:52.000Z","size":48059,"stargazers_count":2,"open_issues_count":2,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-01-31T13:16:32.898Z","etag":null,"topics":["algorithm","algorithms","javascript","recursion","website"],"latest_commit_sha":null,"homepage":"https://bgoonz.github.io/Recursion-Practice-Website","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bgoonz.png","metadata":{"files":{"readme":"README.html","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-02-23T18:33:10.000Z","updated_at":"2022-03-13T13:59:10.000Z","dependencies_parsed_at":"2022-09-22T13:55:04.544Z","dependency_job_id":null,"html_url":"https://github.com/bgoonz/Recursion-Practice-Website","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bgoonz%2FRecursion-Practice-Website","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bgoonz%2FRecursion-Practice-Website/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bgoonz%2FRecursion-Practice-Website/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bgoonz%2FRecursion-Practice-Website/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bgoonz","download_url":"https://codeload.github.com/bgoonz/Recursion-Practice-Website/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245650495,"owners_count":20650105,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","algorithms","javascript","recursion","website"],"created_at":"2024-12-04T12:19:08.829Z","updated_at":"2025-03-26T12:12:00.264Z","avatar_url":"https://github.com/bgoonz.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\"\u003e\n\n\u003chead\u003e\n    \u003cmeta charset=\"UTF-8\"\u003e\n    \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"\u003e\n    \u003ctitle\u003eDocument\u003c/title\u003e\n  \u003clink href='http://fonts.googleapis.com/css?family=Droid+Sans:700' rel='stylesheet'\u003e\n    \u003clink rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css\"\n        integrity=\"sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z\" crossorigin=\"anonymous\"\u003e\n    \u003clink rel=\"stylesheet\" href=\"./prism.css\"\u003e\n    \u003cscript async defer src=\"./prism.js\"\u003e\u003c/script\u003e\n\n    \u003clink rel=\"stylesheet\" href=\"./style.css\"\u003e\n    \u003clink rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css\"\n        integrity=\"sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z\" crossorigin=\"anonymous\"\u003e\n    \u003clink href=\"//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css\" rel=\"stylesheet\"\u003e\n    \u003cscript src=\"//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js\"\u003e\u003c/script\u003e\n    \u003cscript src=\"//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js\"\u003e\u003c/script\u003e\n    \u003clink rel=\"stylesheet\" href=\"./prism.css\"\u003e\n    \u003cscript async defer src=\"./prism.js\"\u003e\u003c/script\u003e\n    \u003clink rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css\"\n        integrity=\"sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN\" crossorigin=\"anonymous\"\u003e\n\n    \u003cscript src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js\"\u003e\u003c/script\u003e\n\n    \n\n    \u003c/style\u003e\n\u003c/head\u003e\n\n\u003cbody\u003e\n\n    \u003cdiv class=\"container\" id=\"body\"\u003e\n\n \u003ch1 id=\"clarify-the-concept-of-recursion\"\u003eClarify the concept of recursion\u003c/h1\u003e\n \u003cp\u003eWhat's the difference and connections between recursion, divide-and-conquer algorithm, dynamic programming, and\n     greedy algorithm? If you haven't made it clear. Doesn't matter! I would give you a brief introduction to kick\n     off this section.\u003c/p\u003e\n \u003cp\u003eRecursion is a programming technique. It's a way of thinking about solving problems. There're two algorithmic\n     ideas to solve specific problems: divide-and-conquer algorithm and dynamic programming. They're largely based on\n     recursive thinking (although the final version of dynamic programming is rarely recursive, the problem-solving\n     idea is still inseparable from recursion). There's also an algorithmic idea called greedy algorithm which can\n     efficiently solve some more special problems. And it's a subset of dynamic programming algorithms.\u003c/p\u003e\n \u003cp\u003eThe divide-and-conquer algorithm will be explained in this section. Taking the most classic merge sort as an\n     example, it continuously divides the unsorted array into smaller sub-problems. This is the origin of the word\n     \u003cstrong\u003edivide and conquer\u003c/strong\u003e. Obviously, the sub-problems decomposed by the ranking problem are\n     non-repeating. If some of the sub-problems after decomposition are duplicated (the nature of overlapping\n     sub-problems), then the dynamic programming algorithm is used to solve them!\n \u003c/p\u003e\n \u003ch2 id=\"recursion-in-detail\"\u003eRecursion in detail\u003c/h2\u003e\n \u003cp\u003eBefore introducing divide and conquer algorithm, we must first understand the concept of recursion.\u003c/p\u003e\n \u003cp\u003eThe basic idea of recursion is that a function calls itself directly or indirectly, which transforms the solution\n     of the original problem into many smaller sub-problems of the same nature. All we need is to focus on how to\n     divide the original problem into qualified sub-problems, rather than study how this sub-problem is solved. The\n     difference between recursion and enumeration is that enumeration divides the problem horizontally and then\n     solves the sub-problems one by one, but recursion divides the problem vertically and then solves the\n     sub-problems hierarchily.\u003c/p\u003e\n \u003cp\u003eThe following illustrates my understanding of recursion. \u003cstrong\u003eIf you don't want to read, please just remember\n         how to answer these questions:\u003c/strong\u003e\u003c/p\u003e\n \u003col type=\"1\"\u003e\n     \u003cli\u003eHow to sort a bunch of numbers? Answer: Divided into two halves, first align the left half, then the right\n         half, and finally merge. As for how to arrange the left and right half, please read this sentence again.\n     \u003c/li\u003e\n     \u003cli\u003eHow many hairs does Monkey King have? Answer: One plus the rest.\u003c/li\u003e\n     \u003cli\u003eHow old are you this year? Answer: One year plus my age of last year, I was born in 1999.\u003c/li\u003e\n \u003c/ol\u003e\n \u003cp\u003eTwo of the most important characteristics of recursive code: \u003cstrong\u003eend conditions and self-invocation\u003c/strong\u003e.\n     Self-invocation is aimed at solving sub-problems, and the end condition defines the answer to the simplest\n     sub-problem.\u003c/p\u003e\n \u003cdiv class=\"sourceCode\" id=\"cb1\"\u003e\n     \u003cpre data-filter-output=\"(out)\" class=\"sourceCode cpp\"\u003e\u003ccode class=\"sourceCode cpp\"\u003e\u003ca class=\"sourceLine\" id=\"cb1-1\" title=\"1\"\u003e\u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e func(How old are you \u003cspan class=\"lang-js kw\"\u003ethis\u003c/span\u003e year) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-2\" title=\"2\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-3\" title=\"3\"\u003e    \u003cspan class=\"lang-js co\"\u003e// simplest sub-problem, end condition\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-4\" title=\"4\"\u003e    \u003cspan class=\"lang-js cf\"\u003eif\u003c/span\u003e (\u003cspan class=\"lang-js kw\"\u003ethis\u003c/span\u003e year equals \u003cspan class=\"lang-js dv\"\u003e1999\u003c/span\u003e) \u003cspan class=\"lang-js cf\"\u003ereturn\u003c/span\u003e my age \u003cspan class=\"lang-js dv\"\u003e0\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-5\" title=\"5\"\u003e    \u003cspan class=\"lang-js co\"\u003e// self-calling to decompose problem\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-6\" title=\"6\"\u003e    \u003cspan class=\"lang-js cf\"\u003ereturn\u003c/span\u003e func(How old are you last year) + \u003cspan class=\"lang-js dv\"\u003e1\u003c/span\u003e;   \u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-7\" title=\"7\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-8\" title=\"8\"\u003e}\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n \u003c/div\u003e\n \u003cp\u003eActually think about it, \u003cstrong\u003ewhat is the most successful application of recursion? I think it's mathematical\n         induction\u003c/strong\u003e. Most of us learned mathematical induction in high school. The usage scenario is\n     probably: we can't figure out a summation formula, but we tried a few small numbers which seemed containing a\n     kinda law, and then we compiled a formula. We ourselves think it shall be the correct answer. However,\n     mathematics is very rigorous. Even if you've tried 10,000 cases which are correct, can you guarantee the 10001th\n     correct? This requires mathematical induction to exert its power. Assuming that the formula we compiled is true\n     at the kth number, furthermore if it is proved correct at the k + 1th, then the formula we have compiled is\n     verified correct.\u003c/p\u003e\n \u003cp\u003eSo what is the connection between mathematical induction and recursion? We just said that the recursive code must\n     have an end condition. If not, it will fall into endless self-calling hell until the memory exhausted. The\n     difficulty of mathematical proof is that you can try to have a finite number of cases, but it is difficult to\n     extend your conclusion to infinity. Here you can see the connection-infinite.\u003c/p\u003e\n \u003cp\u003eThe essence of recursive code is to call itself to solve smaller sub-problems until the end condition is reached.\n     The reason why mathematical induction is useful is to continuously increase our guess by one, and expand the\n     size of the conclusion, without end condition. So by extending the conclusion to infinity, the proof of the\n     correctness of the guess is completed.\u003c/p\u003e\n \u003ch3 id=\"why-learn-recursion\"\u003eWhy learn recursion\u003c/h3\u003e\n \u003cp\u003eFirst to train the ability to think reversely. Recursive thinking is the thinking of normal people, always\n     looking at the problems in front of them and thinking about solutions, and the solution is the future tense;\n     Recursive thinking forces us to think reversely, see the end of the problem, and treat the problem-solving\n     process as the past tense.\u003c/p\u003e\n \u003cp\u003eSecond, practice analyzing the structure of the problem. When the problem can be broken down into sub problems of\n     the same structure, you can acutely find this feature, and then solve it efficiently.\u003c/p\u003e\n \u003cp\u003eThird, go beyond the details and look at the problem as a whole. Let's talk about merge and sort. In fact, you\n     can divide the left and right areas without recursion, but the cost is that the code is extremely difficult to\n     understand. Take a look at the code below (merge sorting will be described later. You can understand the meaning\n     here, and appreciate the beauty of recursion).\u003c/p\u003e\n \u003cdiv class=\"sourceCode\" id=\"cb2\"\u003e\n     \u003cpre data-filter-output=\"(out)\" class=\"sourceCode java\"\u003e\u003ccode class=\"sourceCode java\"\u003e\u003ca class=\"sourceLine\" id=\"cb2-1\" title=\"1\"\u003e\u003cspan class=\"lang-js dt\"\u003evoid\u003c/span\u003e \u003cspan class=\"lang-js fu\"\u003esort\u003c/span\u003e(\u003cspan class=\"lang-js bu\"\u003eComparable\u003c/span\u003e[] a){    \u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-2\" title=\"2\"\u003e    \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e N = a.\u003cspan class=\"lang-js fu\"\u003elength\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-3\" title=\"3\"\u003e    \u003cspan class=\"lang-js co\"\u003e// So complicated! It shows disrespect for sorting. I refuse to study such code.\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-4\" title=\"4\"\u003e    \u003cspan class=\"lang-js kw\"\u003efor\u003c/span\u003e (\u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e sz = \u003cspan class=\"lang-js dv\"\u003e1\u003c/span\u003e; sz \u0026lt; N; sz = sz + sz)\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-5\" title=\"5\"\u003e        \u003cspan class=\"lang-js kw\"\u003efor\u003c/span\u003e (\u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e lo = \u003cspan class=\"lang-js dv\"\u003e0\u003c/span\u003e; lo \u0026lt; N - sz; lo += sz + sz)\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-6\" title=\"6\"\u003e            \u003cspan class=\"lang-js fu\"\u003emerge\u003c/span\u003e(a, lo, lo + sz - \u003cspan class=\"lang-js dv\"\u003e1\u003c/span\u003e, \u003cspan class=\"lang-js bu\"\u003eMath\u003c/span\u003e.\u003cspan class=\"lang-js fu\"\u003emin\u003c/span\u003e(lo + sz + sz - \u003cspan class=\"lang-js dv\"\u003e1\u003c/span\u003e, N - \u003cspan class=\"lang-js dv\"\u003e1\u003c/span\u003e));\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-7\" title=\"7\"\u003e}\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-8\" title=\"8\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-9\" title=\"9\"\u003e\u003cspan class=\"lang-js co\"\u003e/* I prefer recursion, simple and beautiful */\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-10\" title=\"10\"\u003e\u003cspan class=\"lang-js dt\"\u003evoid\u003c/span\u003e \u003cspan class=\"lang-js fu\"\u003esort\u003c/span\u003e(\u003cspan class=\"lang-js bu\"\u003eComparable\u003c/span\u003e[] a, \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e lo, \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e hi) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-11\" title=\"11\"\u003e    \u003cspan class=\"lang-js kw\"\u003eif\u003c/span\u003e (lo \u0026gt;= hi) \u003cspan class=\"lang-js kw\"\u003ereturn\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-12\" title=\"12\"\u003e    \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e mid = lo + (hi - lo) / \u003cspan class=\"lang-js dv\"\u003e2\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-13\" title=\"13\"\u003e    \u003cspan class=\"lang-js fu\"\u003esort\u003c/span\u003e(a, lo, mid); \u003cspan class=\"lang-js co\"\u003e// soft left part\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-14\" title=\"14\"\u003e    \u003cspan class=\"lang-js fu\"\u003esort\u003c/span\u003e(a, mid + \u003cspan class=\"lang-js dv\"\u003e1\u003c/span\u003e, hi); \u003cspan class=\"lang-js co\"\u003e// soft right part\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-15\" title=\"15\"\u003e    \u003cspan class=\"lang-js fu\"\u003emerge\u003c/span\u003e(a, lo, mid, hi); \u003cspan class=\"lang-js co\"\u003e// merge the two sides\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-16\" title=\"16\"\u003e}\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n \u003c/div\u003e\n \u003cp\u003eLooks simple and beautiful is one aspect, the key is \u003cstrong\u003every interpretable\u003c/strong\u003e: sort the left half,\n     sort the right half, and finally merge the two sides. The non-recursive version looks unintelligible, full of\n     various incomprehensible boundary calculation details, is particularly prone to bugs and difficult to debug.\n     Life is short, i prefer the recursive version.\u003c/p\u003e\n \u003cp\u003eObviously, sometimes recursive processing is efficient, such as merge sort, \u003cstrong\u003esometimes\n         inefficient\u003c/strong\u003e, such as counting the hair of Monkey King, because the stack consumes extra space but\n     simple inference does not consume space. Example below gives a linked list header and calculate its length:\u003c/p\u003e\n \u003cdiv class=\"sourceCode\" id=\"cb3\"\u003e\n     \u003cpre data-filter-output=\"(out)\" class=\"sourceCode java\"\u003e\u003ccode class=\"sourceCode java\"\u003e\u003ca class=\"sourceLine\" id=\"cb3-1\" title=\"1\"\u003e\u003cspan class=\"lang-js co\"\u003e/* Typical recursive traversal framework requires extra space O(1) */\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-2\" title=\"2\"\u003e\u003cspan class=\"lang-js kw\"\u003epublic\u003c/span\u003e \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e \u003cspan class=\"lang-js fu\"\u003esize\u003c/span\u003e(\u003cspan class=\"lang-js bu\"\u003eNode\u003c/span\u003e head) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-3\" title=\"3\"\u003e    \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e size = \u003cspan class=\"lang-js dv\"\u003e0\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-4\" title=\"4\"\u003e    \u003cspan class=\"lang-js kw\"\u003efor\u003c/span\u003e (\u003cspan class=\"lang-js bu\"\u003eNode\u003c/span\u003e p = head; p != \u003cspan class=\"lang-js kw\"\u003enull\u003c/span\u003e; p = p.\u003cspan class=\"lang-js fu\"\u003enext\u003c/span\u003e) size++;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-5\" title=\"5\"\u003e    \u003cspan class=\"lang-js kw\"\u003ereturn\u003c/span\u003e size;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-6\" title=\"6\"\u003e}\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-7\" title=\"7\"\u003e\u003cspan class=\"lang-js co\"\u003e/* I insist on recursion facing every problem. I need extra space O(N) */\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-8\" title=\"8\"\u003e\u003cspan class=\"lang-js kw\"\u003epublic\u003c/span\u003e \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e \u003cspan class=\"lang-js fu\"\u003esize\u003c/span\u003e(\u003cspan class=\"lang-js bu\"\u003eNode\u003c/span\u003e head) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-9\" title=\"9\"\u003e    \u003cspan class=\"lang-js kw\"\u003eif\u003c/span\u003e (head == \u003cspan class=\"lang-js kw\"\u003enull\u003c/span\u003e) \u003cspan class=\"lang-js kw\"\u003ereturn\u003c/span\u003e \u003cspan class=\"lang-js dv\"\u003e0\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-10\" title=\"10\"\u003e    \u003cspan class=\"lang-js kw\"\u003ereturn\u003c/span\u003e \u003cspan class=\"lang-js fu\"\u003esize\u003c/span\u003e(head.\u003cspan class=\"lang-js fu\"\u003enext\u003c/span\u003e) + \u003cspan class=\"lang-js dv\"\u003e1\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-11\" title=\"11\"\u003e}\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n \u003c/div\u003e\n \u003ch3 id=\"tips-for-writing-recursion\"\u003eTips for writing recursion\u003c/h3\u003e\n \u003cp\u003eMy point of view: \u003cstrong\u003eUnderstand what a function does and believe it can accomplish this task. Don't try to\n         jump into the details.\u003c/strong\u003e Do not jump into this function to try to explore more details, otherwise you\n     will fall into infinite details and cannot extricate yourself. The human brain carries tiny sized stack!\u003c/p\u003e\n \u003cp\u003eLet's start with the simplest example: traversing a binary tree.\u003c/p\u003e\n \u003cdiv class=\"sourceCode\" id=\"cb4\"\u003e\n     \u003cpre data-filter-output=\"(out)\" class=\"sourceCode cpp\"\u003e\u003ccode class=\"sourceCode cpp\"\u003e\u003ca class=\"sourceLine\" id=\"cb4-1\" title=\"1\"\u003e\u003cspan class=\"lang-js dt\"\u003evoid\u003c/span\u003e traverse(TreeNode* root) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb4-2\" title=\"2\"\u003e    \u003cspan class=\"lang-js cf\"\u003eif\u003c/span\u003e (root == \u003cspan class=\"lang-js kw\"\u003enullptr\u003c/span\u003e) \u003cspan class=\"lang-js cf\"\u003ereturn\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb4-3\" title=\"3\"\u003e    traverse(root-\u0026gt;left);\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb4-4\" title=\"4\"\u003e    traverse(root-\u0026gt;right);\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb4-5\" title=\"5\"\u003e}\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n \u003c/div\u003e\n \u003cp\u003eAbove few lines of code are enough to wipe out any binary tree. What I want to say is that for the recursive\n     function \u003ccode class=\"language-javascript\"\u003etraverse (root)\u003c/code\u003e , we just need to believe: give it a root node\n     \u003ccode class=\"language-javascript\"\u003eroot\u003c/code\u003e , and it\n     can traverse the whole tree. Since this function is written for this specific purpose, so we just need to dump\n     the left and right nodes of this node to this function, because I believe it can surely complete the task. What\n     about traversing an N-fork tree? It's too simple, exactly the same as a binary tree!\n \u003c/p\u003e\n \u003cdiv class=\"sourceCode\" id=\"cb5\"\u003e\n     \u003cpre data-filter-output=\"(out)\" class=\"sourceCode cpp\"\u003e\u003ccode class=\"sourceCode cpp\"\u003e\u003ca class=\"sourceLine\" id=\"cb5-1\" title=\"1\"\u003e\u003cspan class=\"lang-js dt\"\u003evoid\u003c/span\u003e traverse(TreeNode* root) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb5-2\" title=\"2\"\u003e    \u003cspan class=\"lang-js cf\"\u003eif\u003c/span\u003e (root == \u003cspan class=\"lang-js kw\"\u003enullptr\u003c/span\u003e) \u003cspan class=\"lang-js cf\"\u003ereturn\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb5-3\" title=\"3\"\u003e    \u003cspan class=\"lang-js cf\"\u003efor\u003c/span\u003e (child : root-\u0026gt;children)\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb5-4\" title=\"4\"\u003e        traverse(child);\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb5-5\" title=\"5\"\u003e}\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n \u003c/div\u003e\n \u003cp\u003eAs for pre-order, mid-order, post-order traversal, they are all obvious. For N-fork tree, there is obviously no\n     in-order traversal.\u003c/p\u003e\n \u003cp\u003eThe following \u003cstrong\u003eexplains a problem from LeetCode in detail\u003c/strong\u003e: Given a binary tree and a target\n     value, the values in every node is positive or negative, return the number of paths in the tree that are equal\n     to the target value, let you write the pathSum function:\u003c/p\u003e\n \u003cpre data-filter-output=\"(out)\" data-role=\"codeBlock\" data-info=\"js\"\n     class=\"language-javascript data-line line-numbers data-user data-host data-prompt data-output\"\n     data-prismjs-copy=\"Copy !\" data-download-link /\u003e\n \u003ccode class=\"language-javascript\"\u003e/* from LeetCode PathSum III： https://leetcode.com/problems/path-sum-iii/ */\n     root = [10,5,-3,3,2,null,11,3,-2,null,1],\n     sum = 8\n\n     10\n     / \\\n     5 -3\n     / \\ \\\n     3 2 11\n     / \\ \\\n     3 -2 1\n\n     Return 3. The paths that sum to 8 are:\n\n     1. 5 -\u0026gt; 3\n     2. 5 -\u0026gt; 2 -\u0026gt; 1\n     3. -3 -\u0026gt; 11\n \u003c/code\u003e\u003c/pre\u003e\n \u003cdiv class=\"sourceCode\" id=\"cb7\"\u003e\n     \u003cpre data-filter-output=\"(out)\" class=\"sourceCode cpp\"\u003e\u003ccode class=\"sourceCode cpp\"\u003e\u003ca class=\"sourceLine\" id=\"cb7-1\" title=\"1\"\u003e\u003cspan class=\"lang-js co\"\u003e/* It doesn\u0026#39;t matter if you don\u0026#39;t understand, there is a more detailed analysis version below, which highlights the conciseness and beauty of recursion. */\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb7-2\" title=\"2\"\u003e\u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e pathSum(TreeNode root, \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e sum) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb7-3\" title=\"3\"\u003e    \u003cspan class=\"lang-js cf\"\u003eif\u003c/span\u003e (root == null) \u003cspan class=\"lang-js cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"lang-js dv\"\u003e0\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb7-4\" title=\"4\"\u003e    \u003cspan class=\"lang-js cf\"\u003ereturn\u003c/span\u003e count(root, sum) + \u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb7-5\" title=\"5\"\u003e        pathSum(root.left, sum) + pathSum(root.right, sum);\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb7-6\" title=\"6\"\u003e}\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb7-7\" title=\"7\"\u003e\u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e count(TreeNode node, \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e sum) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb7-8\" title=\"8\"\u003e    \u003cspan class=\"lang-js cf\"\u003eif\u003c/span\u003e (node == null) \u003cspan class=\"lang-js cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"lang-js dv\"\u003e0\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb7-9\" title=\"9\"\u003e    \u003cspan class=\"lang-js cf\"\u003ereturn\u003c/span\u003e (node.val == sum) + \u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb7-10\" title=\"10\"\u003e        count(node.left, sum - node.val) + count(node.right, sum - node.val);\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb7-11\" title=\"11\"\u003e}\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n \u003c/div\u003e\n \u003cp\u003eThe problem may seem complicated, but the code is extremely concise, which is the charm of recursion. Let me\n     briefly summarize the \u003cstrong\u003esolution process\u003c/strong\u003e of this problem:\u003c/p\u003e\n \u003cp\u003eFirst of all, it is clear that to solve the problem of recursive tree, you must traverse the entire tree. So the\n     traversal framework of the binary tree (recursively calling the function itself on the left and right children)\n     must appear in the main function pathSum. And then, what should they do for each node? They should see how many\n     eligible paths they and their little children have under their feet. Well, this question is clear.\u003c/p\u003e\n \u003cp\u003eAccording to the techniques mentioned earlier, define what each recursive function should do based on the\n     analysis just now:\u003c/p\u003e\n \u003cp\u003ePathSum function: Give it a node and a target value. It returns the total number of paths in the tree rooted at\n     this node and the target value.\u003c/p\u003e\n \u003cp\u003eCount function: Give it a node and a target value. It returns a tree rooted at this node, and can make up the\n     total number of paths starting with the node and the target value.\u003c/p\u003e\n \u003cdiv class=\"sourceCode\" id=\"cb8\"\u003e\n     \u003cpre data-filter-output=\"(out)\" class=\"sourceCode cpp\"\u003e\u003ccode class=\"sourceCode cpp\"\u003e\u003ca class=\"sourceLine\" id=\"cb8-1\" title=\"1\"\u003e\u003cspan class=\"lang-js co\"\u003e/* With above tips, comment out the code in detail */\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-2\" title=\"2\"\u003e\u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e pathSum(TreeNode root, \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e sum) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-3\" title=\"3\"\u003e    \u003cspan class=\"lang-js cf\"\u003eif\u003c/span\u003e (root == null) \u003cspan class=\"lang-js cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"lang-js dv\"\u003e0\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-4\" title=\"4\"\u003e    \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e pathImLeading = count(root, sum); \u003cspan class=\"lang-js co\"\u003e// Number of paths beginning with itself\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-5\" title=\"5\"\u003e    \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e leftPathSum = pathSum(root.left, sum); \u003cspan class=\"lang-js co\"\u003e// The total number of paths on the left (Believe he can figure it out)\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-6\" title=\"6\"\u003e    \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e rightPathSum = pathSum(root.right, sum); \u003cspan class=\"lang-js co\"\u003e// The total number of paths on the right (Believe he can figure it out)\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-7\" title=\"7\"\u003e    \u003cspan class=\"lang-js cf\"\u003ereturn\u003c/span\u003e leftPathSum + rightPathSum + pathImLeading;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-8\" title=\"8\"\u003e}\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-9\" title=\"9\"\u003e\u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e count(TreeNode node, \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e sum) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-10\" title=\"10\"\u003e    \u003cspan class=\"lang-js cf\"\u003eif\u003c/span\u003e (node == null) \u003cspan class=\"lang-js cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"lang-js dv\"\u003e0\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-11\" title=\"11\"\u003e    \u003cspan class=\"lang-js co\"\u003e// Can I stand on my own as a separate path?\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-12\" title=\"12\"\u003e    \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e isMe = (node.val == sum) ? \u003cspan class=\"lang-js dv\"\u003e1\u003c/span\u003e : \u003cspan class=\"lang-js dv\"\u003e0\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-13\" title=\"13\"\u003e    \u003cspan class=\"lang-js co\"\u003e// Left brother, how many sum-node.val can you put together?\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-14\" title=\"14\"\u003e    \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e leftBrother = count(node.left, sum - node.val); \u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-15\" title=\"15\"\u003e    \u003cspan class=\"lang-js co\"\u003e// Right brother, how many sum-node.val can you put together?\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-16\" title=\"16\"\u003e    \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e rightBrother = count(node.right, sum - node.val);\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-17\" title=\"17\"\u003e    \u003cspan class=\"lang-js cf\"\u003ereturn\u003c/span\u003e  isMe + leftBrother + rightBrother; \u003cspan class=\"lang-js co\"\u003e// all count i can make up\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-18\" title=\"18\"\u003e}\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n \u003c/div\u003e\n \u003cp\u003eAgain, understand what each function can do and trust that they can do it.\u003c/p\u003e\n \u003cp\u003eIn summary, the binary tree traversal framework provided by the PathSum function calls the count function for\n     each node during the traversal. Can you see the pre-order traversal (the order is the same for this question)?\n     The count function is also a binary tree traversal, used to find the target value path starting with this node.\n     Understand it deeply!\u003c/p\u003e\n \u003ch2 id=\"divide-and-conquer-algorithm\"\u003eDivide and conquer algorithm\u003c/h2\u003e\n \u003cp\u003e\u003cstrong\u003eMerge and sort\u003c/strong\u003e, typical divide-and-conquer algorithm; divide-and-conquer, typical recursive\n     structure.\u003c/p\u003e\n \u003cp\u003eThe divide-and-conquer algorithm can go in three steps: decomposition-\u0026gt; solve-\u0026gt; merge\u003c/p\u003e\n \u003col type=\"1\"\u003e\n     \u003cli\u003eDecompose the original problem into sub-problems with the same structure.\u003c/li\u003e\n     \u003cli\u003eAfter decomposing to an easy-to-solve boundary, perform a recursive solution.\u003c/li\u003e\n     \u003cli\u003eCombine the solutions of the subproblems into the solutions of the original problem.\u003c/li\u003e\n \u003c/ol\u003e\n \u003cp\u003eTo merge and sort, let's call this function \u003ccode class=\"language-javascript\"\u003emerge_sort\u003c/code\u003e . According to\n     what we said above, we must\n     clarify the responsibility of the function, that is, \u003cstrong\u003esort an incoming array\u003c/strong\u003e. OK, can this\n     problem be solved? Of course! Sorting an array is just the same to sorting the two halves of the array\n     separately, and then merging the two halves.\u003c/p\u003e\n \u003cdiv class=\"sourceCode\" id=\"cb9\"\u003e\n     \u003cpre data-filter-output=\"(out)\" class=\"sourceCode cpp\"\u003e\u003ccode class=\"sourceCode cpp\"\u003e\u003ca class=\"sourceLine\" id=\"cb9-1\" title=\"1\"\u003e\u003cspan class=\"lang-js dt\"\u003evoid\u003c/span\u003e merge_sort(an array) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-2\" title=\"2\"\u003e    \u003cspan class=\"lang-js cf\"\u003eif\u003c/span\u003e (some tiny array easy to solve) \u003cspan class=\"lang-js cf\"\u003ereturn\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-3\" title=\"3\"\u003e    merge_sort(left half array);\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-4\" title=\"4\"\u003e    merge_sort(right half array);\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-5\" title=\"5\"\u003e    merge(left half array, right half array);\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-6\" title=\"6\"\u003e}\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n \u003c/div\u003e\n \u003cp\u003eWell, this algorithm is like this, there is no difficulty at all. Remember what I said before, believe in the\n     function's ability, and pass it to him half of the array, then the half of the array is already sorted. Have you\n     found it's a binary tree traversal template? Why it is postorder traversal? Because the routine of our\n     divide-and-conquer algorithm is \u003cstrong\u003edecomposition-\u0026gt; solve (bottom)-\u0026gt; merge (backtracking)\u003c/strong\u003e Ah,\n     first left and right decomposition, and then processing merge, backtracking is popping stack, which is\n     equivalent to post-order traversal. As for the \u003ccode class=\"language-javascript\"\u003emerge\u003c/code\u003e function,\n     referring to the merging of two\n     ordered linked lists, they are exactly the same, and the code is directly posted below.\u003c/p\u003e\n \u003cp\u003eLet's refer to the Java code in book \u003ccode class=\"language-javascript\"\u003eAlgorithm 4\u003c/code\u003e below, which is pretty.\n     This shows that not only\n     algorithmic thinking is important, but coding skills are also very important! Think more and imitate more.\u003c/p\u003e\n \u003cdiv class=\"sourceCode\" id=\"cb10\"\u003e\n     \u003cpre data-filter-output=\"(out)\" class=\"sourceCode java\"\u003e\u003ccode class=\"sourceCode java\"\u003e\u003ca class=\"sourceLine\" id=\"cb10-1\" title=\"1\"\u003e\u003cspan class=\"lang-js kw\"\u003epublic\u003c/span\u003e \u003cspan class=\"lang-js kw\"\u003eclass\u003c/span\u003e Merge {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-2\" title=\"2\"\u003e    \u003cspan class=\"lang-js co\"\u003e// Do not construct new arrays in the merge function, because the merge function will be called multiple times, affecting performance.Construct a large enough array directly at once, concise and efficient.\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-3\" title=\"3\"\u003e    \u003cspan class=\"lang-js kw\"\u003eprivate\u003c/span\u003e \u003cspan class=\"lang-js dt\"\u003estatic\u003c/span\u003e \u003cspan class=\"lang-js bu\"\u003eComparable\u003c/span\u003e[] aux;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-4\" title=\"4\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-5\" title=\"5\"\u003e     \u003cspan class=\"lang-js kw\"\u003epublic\u003c/span\u003e \u003cspan class=\"lang-js dt\"\u003estatic\u003c/span\u003e \u003cspan class=\"lang-js dt\"\u003evoid\u003c/span\u003e \u003cspan class=\"lang-js fu\"\u003esort\u003c/span\u003e(\u003cspan class=\"lang-js bu\"\u003eComparable\u003c/span\u003e[] a) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-6\" title=\"6\"\u003e        aux = \u003cspan class=\"lang-js kw\"\u003enew\u003c/span\u003e \u003cspan class=\"lang-js bu\"\u003eComparable\u003c/span\u003e[a.\u003cspan class=\"lang-js fu\"\u003elength\u003c/span\u003e];\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-7\" title=\"7\"\u003e        \u003cspan class=\"lang-js fu\"\u003esort\u003c/span\u003e(a, \u003cspan class=\"lang-js dv\"\u003e0\u003c/span\u003e, a.\u003cspan class=\"lang-js fu\"\u003elength\u003c/span\u003e - \u003cspan class=\"lang-js dv\"\u003e1\u003c/span\u003e);\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-8\" title=\"8\"\u003e    }\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-9\" title=\"9\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-10\" title=\"10\"\u003e    \u003cspan class=\"lang-js kw\"\u003eprivate\u003c/span\u003e \u003cspan class=\"lang-js dt\"\u003estatic\u003c/span\u003e \u003cspan class=\"lang-js dt\"\u003evoid\u003c/span\u003e \u003cspan class=\"lang-js fu\"\u003esort\u003c/span\u003e(\u003cspan class=\"lang-js bu\"\u003eComparable\u003c/span\u003e[] a, \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e lo, \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e hi) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-11\" title=\"11\"\u003e        \u003cspan class=\"lang-js kw\"\u003eif\u003c/span\u003e (lo \u0026gt;= hi) \u003cspan class=\"lang-js kw\"\u003ereturn\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-12\" title=\"12\"\u003e        \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e mid = lo + (hi - lo) / \u003cspan class=\"lang-js dv\"\u003e2\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-13\" title=\"13\"\u003e        \u003cspan class=\"lang-js fu\"\u003esort\u003c/span\u003e(a, lo, mid);\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-14\" title=\"14\"\u003e        \u003cspan class=\"lang-js fu\"\u003esort\u003c/span\u003e(a, mid + \u003cspan class=\"lang-js dv\"\u003e1\u003c/span\u003e, hi);\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-15\" title=\"15\"\u003e        \u003cspan class=\"lang-js fu\"\u003emerge\u003c/span\u003e(a, lo, mid, hi);\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-16\" title=\"16\"\u003e    }\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-17\" title=\"17\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-18\" title=\"18\"\u003e    \u003cspan class=\"lang-js kw\"\u003eprivate\u003c/span\u003e \u003cspan class=\"lang-js dt\"\u003estatic\u003c/span\u003e \u003cspan class=\"lang-js dt\"\u003evoid\u003c/span\u003e \u003cspan class=\"lang-js fu\"\u003emerge\u003c/span\u003e(\u003cspan class=\"lang-js bu\"\u003eComparable\u003c/span\u003e[] a, \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e lo, \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e mid, \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e hi) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-19\" title=\"19\"\u003e        \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e i = lo, j = mid + \u003cspan class=\"lang-js dv\"\u003e1\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-20\" title=\"20\"\u003e        \u003cspan class=\"lang-js kw\"\u003efor\u003c/span\u003e (\u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e k = lo; k \u0026lt;= hi; k++)\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-21\" title=\"21\"\u003e            aux[k] = a[k];\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-22\" title=\"22\"\u003e        \u003cspan class=\"lang-js kw\"\u003efor\u003c/span\u003e (\u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e k = lo; k \u0026lt;= hi; k++) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-23\" title=\"23\"\u003e            \u003cspan class=\"lang-js kw\"\u003eif\u003c/span\u003e      (i \u0026gt; mid)              { a[k] = aux[j++]; }\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-24\" title=\"24\"\u003e            \u003cspan class=\"lang-js kw\"\u003eelse\u003c/span\u003e \u003cspan class=\"lang-js kw\"\u003eif\u003c/span\u003e (j \u0026gt; hi)               { a[k] = aux[i++]; }\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-25\" title=\"25\"\u003e            \u003cspan class=\"lang-js kw\"\u003eelse\u003c/span\u003e \u003cspan class=\"lang-js kw\"\u003eif\u003c/span\u003e (\u003cspan class=\"lang-js fu\"\u003eless\u003c/span\u003e(aux[j], aux[i])) { a[k] = aux[j++]; }\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-26\" title=\"26\"\u003e            \u003cspan class=\"lang-js kw\"\u003eelse\u003c/span\u003e                           { a[k] = aux[i++]; }\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-27\" title=\"27\"\u003e        }\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-28\" title=\"28\"\u003e    }\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-29\" title=\"29\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-30\" title=\"30\"\u003e    \u003cspan class=\"lang-js kw\"\u003eprivate\u003c/span\u003e \u003cspan class=\"lang-js dt\"\u003estatic\u003c/span\u003e \u003cspan class=\"lang-js dt\"\u003eboolean\u003c/span\u003e \u003cspan class=\"lang-js fu\"\u003eless\u003c/span\u003e(\u003cspan class=\"lang-js bu\"\u003eComparable\u003c/span\u003e v, \u003cspan class=\"lang-js bu\"\u003eComparable\u003c/span\u003e w) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-31\" title=\"31\"\u003e        \u003cspan class=\"lang-js kw\"\u003ereturn\u003c/span\u003e v.\u003cspan class=\"lang-js fu\"\u003ecompareTo\u003c/span\u003e(w) \u0026lt; \u003cspan class=\"lang-js dv\"\u003e0\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-32\" title=\"32\"\u003e    }\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-33\" title=\"33\"\u003e}\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n \u003c/div\u003e\n \u003cp\u003eLeetCode has a special exercise of the divide-and-conquer algorithm. Copy the link below to web browser and have\n     a try:\u003c/p\u003e\n \u003cp\u003ehttps://leetcode.com/tag/divide-and-conquer/\u003c/p\u003e\n\n\n \u003cp\u003ePrompt: write a function that will reverse a string:\u003c/p\u003e\n \u003cp\u003evar reverse = function(string){\u003cbr /\u003e\n     if(string.length \u0026lt; 2){\u003c/p\u003e\n \u003cpre data-filter-output=\"(out)\" data-role=\"codeBlock\" data-info=\"js\"\n     class=\"language-javascript data-line line-numbers data-user data-host data-prompt data-output\"\n     data-prismjs-copy=\"Copy !\" data-download-link /\u003e\n \u003ccode class=\"language-javascript\"\u003ereturn string; \u003c/code\u003e\u003c/pre\u003e\n \u003cp\u003e}\u003cbr /\u003e\n     var first = string[0]\u003cbr /\u003e\n     var last = string[string.length-1]; return last +reverse(string.slice(1, string.length-1)) + first; };\n     reverse('abcdef'); //returns 'fedcba'\u003c/p\u003e\n \u003cp\u003e\u003cstrong\u003e//explain what a recursive function is\u003c/strong\u003e\u003c/p\u003e\n \u003cp\u003e\u003cstrong\u003e\u003cem\u003eA function that calls itself\u003c/em\u003e\u003c/strong\u003e is a recursive function.\u003c/p\u003e\n \u003cp\u003eIf a function calls itself… then that function calls itself… then that function calls itself… well… then we\n     have\n     fallen into an infinite loop (a very unproductive place to be). To benefit from recursive calls, we need to\n     be\n     careful to include to give our interpreter a way to break out of the cycle of recursive function calls; we\n     call\n     this a \u003cstrong\u003e\u003cem\u003ebase case\u003c/em\u003e\u003c/strong\u003e.\u003c/p\u003e\n \u003cp\u003eThe base case in the solution code above is as simple as testing that the length of the argument is less than\n     2…\n     and if it is, returning the the value of that argument.\u003c/p\u003e\n \u003cp\u003eNotice how each time we recursively call the reverse function, we are passing it a shorter string argument…\n     so\n     each recursive call is getting us closer to hitting our \u003cstrong\u003e\u003cem\u003ebase case\u003c/em\u003e\u003c/strong\u003e.\u003c/p\u003e\n \u003cp\u003e\u003cstrong\u003e//visualize the interpreter's path through recursive function calls\u003c/strong\u003e\u003c/p\u003e\n \u003cfigure\u003e\n     \u003cimg src=\"https://miro.medium.com/max/60/1*J4FL6LpLY1AXy_KPFdREKw.png?q=20\" alt=\"Image for post\" /\u003e\n     \u003cfigcaption\u003eImage for post\u003c/figcaption\u003e\n \u003c/figure\u003e\n \u003cfigure\u003e\n     \u003cimg src=\"https://miro.medium.com/max/1810/1*J4FL6LpLY1AXy_KPFdREKw.png\" alt=\"Image for post\" /\u003e\n     \u003cfigcaption\u003eImage for post\u003c/figcaption\u003e\n \u003c/figure\u003e\n \u003cp\u003eSlow down and follow the interpreter through its execution of your algorithm (thanks to PythonTutor.com)\u003c/p\u003e\n \u003cp\u003ePython Tutor is an excellent resource for learning to visualize and trace variable values through the\n     multiple\n     execution contexts of a recursive function's invocation.\u003c/p\u003e\n \u003cp\u003e\u003cem\u003eTry it now with these simple steps:\u003c/em\u003e\u003c/p\u003e\n \u003col type=\"1\"\u003e\n     \u003cli\u003e\u003cem\u003ecopy the solution code from above\u003c/em\u003e\u003c/li\u003e\n     \u003cli\u003e\u003cem\u003ego over to\u003c/em\u003e \u003ca\n             href=\"http://pythontutor.com/javascript.html#mode=edit\"\u003e\u003cem\u003ehttp://pythontutor.com/javascript.html#mode=edit\u003c/em\u003e\u003c/a\u003e\n     \u003c/li\u003e\n     \u003cli\u003e\u003cem\u003epaste the solution code into the editor\u003c/em\u003e\u003c/li\u003e\n     \u003cli\u003e\u003cem\u003eclick the \"Visualize Execution\" button\u003c/em\u003e\u003c/li\u003e\n     \u003cli\u003e\u003cem\u003eprogress through the execution with the \"forward\" button\u003c/em\u003e\u003c/li\u003e\n \u003c/ol\u003e\n \u003cp\u003e\u003cstrong\u003e//when can a recursive function help me?\u003c/strong\u003e\u003c/p\u003e\n \u003cp\u003eSo if I hope that at this point that you are thinking: there is a \u003cstrong\u003e\u003cem\u003ebetter\u003c/em\u003e\u003c/strong\u003e way to\n     reverse\n     a function, or there is a \u003cstrong\u003e\u003cem\u003esimpler\u003c/em\u003e\u003c/strong\u003e way to reverse a string…\u003c/p\u003e\n \u003cp\u003eFirst off… \u003cstrong\u003e\u003cem\u003esimpler is better.\u003c/em\u003e\u003c/strong\u003e Writing good code isn't about being clever or fancy;\n     good\n     code is about writing code that works, that makes sense to as many other minds as possible, that is time\n     efficient, and that is memory efficient (in order of importance). As new programers, the first of these\n     criteria\n     is obvious, and the last two are given way too much weight. It's the second of these criteria that needs to\n     carry much more weight in our minds and deserves the most attention. Recursive functions can be a powerful\n     tool\n     in helping us write clear and simple solutions.\u003c/p\u003e\n \u003cp\u003eTo be clear: recursion is not about being fancy or clever… it is an important skill to wrestle with early\n     because\n     there will be many scenarios when employing recursion will allow for a simpler and more reliable solution\n     than\n     would be possible without recursive functions.\u003c/p\u003e\n \u003cp\u003e\u003cstrong\u003e//more useful example\u003c/strong\u003e\u003c/p\u003e\n \u003cp\u003ePrompt: check to see if a binary-search-tree contains a value\u003c/p\u003e\n \u003cp\u003evar searchBST = function(tree, num){\u003cbr /\u003e\n     if(tree.val === num){\u003c/p\u003e\n \u003cpre data-filter-output=\"(out)\" data-role=\"codeBlock\" data-info=\"js\"\n     class=\"language-javascript data-line line-numbers data-user data-host data-prompt data-output\"\n     data-prismjs-copy=\"Copy !\" data-download-link /\u003e\n \u003ccode class=\"language-javascript\"\u003ereturn true \u003c/code\u003e\u003c/pre\u003e\n \u003cp\u003e} else if(num \u0026gt; tree.val){\u003c/p\u003e\n \u003cpre data-filter-output=\"(out)\" data-role=\"codeBlock\" data-info=\"js\"\n     class=\"language-javascript data-line line-numbers data-user data-host data-prompt data-output\"\n     data-prismjs-copy=\"Copy !\" data-download-link /\u003e\n \u003ccode class=\"language-javascript\"\u003eif(tree.right === null){\n     return false;\n     } else{\n     return searchBST(tree.right, num);\n     } \u003c/code\u003e\u003c/pre\u003e\n \u003cp\u003e} else{\u003c/p\u003e\n \u003cpre data-filter-output=\"(out)\" data-role=\"codeBlock\" data-info=\"js\"\n     class=\"language-javascript data-line line-numbers data-user data-host data-prompt data-output\"\n     data-prismjs-copy=\"Copy !\" data-download-link /\u003e\n \u003ccode class=\"language-javascript\"\u003eif(tree.left === null){\n     return false;\n     } else{\n     return searchBST(tree.left, num);\n     } \u003c/code\u003e\u003c/pre\u003e\n \u003cp\u003e}\u003cbr /\u003e\n     }; var tree = {val: 9,\u003c/p\u003e\n \u003cpre data-filter-output=\"(out)\" data-role=\"codeBlock\" data-info=\"js\"\n     class=\"language-javascript data-line line-numbers data-user data-host data-prompt data-output\"\n     data-prismjs-copy=\"Copy !\" data-download-link /\u003e\n \u003ccode class=\"language-javascript\"\u003e left: {val: 5,\n     left: null,\n     right: {val: 7,\n     left: null,\n     right: null}\n     },\n     right: {val: 20,\n     left: {val: 16,\n     left: null,\n     right: {val: 18,\n     left: null,\n     right: null}\n     },\n     right: null}\n     };searchBST(tree, 18) // return true \u003c/code\u003e\u003c/pre\u003e\n \u003cp\u003esearchBST(tree, 4) // return false\u003c/p\u003e\n \u003cp\u003eWhen traversing trees and many other other non-primative data structures, recursion allows us to define a\n     clear\n     algorithm that elegantly handles uncertainty and complexity. Without recursion, it would be impossible to\n     write\n     a single function that could search a binary search tree of any size and state… yet by employing recursion,\n     we\n     can write a concise algorithm that will traverse any binary search tree and determine if it contains a value\n     or\n     not.\u003c/p\u003e\n \u003cp\u003eTake a moment to analyze how recursion is used in this example by tracing the interpreters path through this\n     solution. Just as we did for the reverse function above, paste this binary search tree code snippet into the\n     editor at \u003ca\n         href=\"http://pythontutor.com/javascript.html#mode=display\"\u003ehttp://pythontutor.com/javascript.html#mode=display\u003c/a\u003e\n \u003c/p\u003e\n \u003cp\u003eIn this function definition, there are three base cases that will return a value instead of recursively\n     calling\n     the searchBST function… can you find them?\u003c/p\u003e\n \u003cp\u003e//now go practice using recursion\u003c/p\u003e\n\n        \u003ch1 id=\"data-structures-and-algorithms\" data-ignore=\"true\"\u003e\u003cbr\u003e\u003cem\u003eData Structures and\n                Algorithms\u003c/em\u003e\n        \u003c/h1\u003e\n        \u003chr /\u003e\n        \u003c!-- code_chunk_output --\u003e\n\n        \u003cp\u003e\u003ca href=\"#big-o-\"\u003e\u003cstrong\u003eBig O \u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#memoization-and-tabulation-\"\u003e\u003cstrong\u003eMemoization And\n                    Tabulation \u003c/strong\u003e\u003c/a\u003e\n            - \u003ca href=\"#recursion-videos\"\u003eRecursion Videos\u003c/a\u003e - \u003ca\n                href=\"#curating-complexity-a-guide-to-big-o-notation\"\u003eCurating Complexity: A Guide to Big-O Notation\u003c/a\u003e\n            -\n            \u003ca href=\"#why-big-o\"\u003eWhy Big-O?\u003c/a\u003e - \u003ca href=\"#big-o-notation\"\u003eBig-O Notation\u003c/a\u003e - \u003ca\n                href=\"#common-complexity-classes\"\u003eCommon Complexity Classes\u003c/a\u003e - \u003ca href=\"#the-seven-major-classes\"\u003eThe\n                seven major classes\u003c/a\u003e - \u003ca href=\"#memoization\"\u003eMemoization\u003c/a\u003e - \u003ca\n                href=\"#memoizing-factorial\"\u003eMemoizing\n                factorial\u003c/a\u003e - \u003ca href=\"#memoizing-the-fibonacci-generator\"\u003eMemoizing the Fibonacci generator\u003c/a\u003e - \u003ca\n                href=\"#the-memoization-formula\"\u003eThe memoization formula\u003c/a\u003e - \u003ca href=\"#tabulation\"\u003eTabulation\u003c/a\u003e - \u003ca\n                href=\"#tabulating-the-fibonacci-number\"\u003eTabulating the Fibonacci number\u003c/a\u003e - \u003ca\n                href=\"#aside-refactoring-for-o1-space\"\u003eAside: Refactoring for O(1) Space\u003c/a\u003e - \u003ca\n                href=\"#analysis-of-linear-search\"\u003eAnalysis of Linear Search\u003c/a\u003e - \u003ca\n                href=\"#analysis-of-binary-search\"\u003eAnalysis of Binary Search\u003c/a\u003e - \u003ca\n                href=\"#analysis-of-the-merge-sort\"\u003eAnalysis of the Merge Sort\u003c/a\u003e - \u003ca\n                href=\"#analysis-of-bubble-sort\"\u003eAnalysis of Bubble Sort\u003c/a\u003e - \u003ca href=\"#leetcodecom\"\u003eLeetCode.com\u003c/a\u003e -\n            \u003ca href=\"#memoization-problems\"\u003eMemoization Problems\u003c/a\u003e - \u003ca href=\"#tabulation-problems\"\u003eTabulation\n                Problems\u003c/a\u003e\n        \u003c/p\u003e\n        \u003cp\u003e\u003ca href=\"#sorting-algorithms-\"\u003e\u003cstrong\u003eSorting Algorithms \u003c/strong\u003e\u003c/a\u003e - \u003ca href=\"#bubble-sort\"\u003eBubble\n                Sort\u003c/a\u003e - \u003ca href=\"#_butthenwhy-are-we_\"\u003e\u003cem\u003e\"But…then…why are we…\"\u003c/em\u003e\u003c/a\u003e\n            -\n            \u003ca href=\"#the-algorithm-bubbles-up\"\u003eThe algorithm bubbles up\u003c/a\u003e - \u003ca\n                href=\"#how-does-a-pass-of-bubble-sort-work\"\u003eHow does a pass of Bubble Sort work?\u003c/a\u003e - \u003ca\n                href=\"#ending-the-bubble-sort\"\u003eEnding the Bubble Sort\u003c/a\u003e - \u003ca\n                href=\"#pseudocode-for-bubble-sort\"\u003ePseudocode\n                for Bubble Sort\u003c/a\u003e - \u003ca href=\"#selection-sort\"\u003eSelection Sort\u003c/a\u003e - \u003ca\n                href=\"#the-algorithm-select-the-next-smallest\"\u003eThe algorithm: select the next smallest\u003c/a\u003e - \u003ca\n                href=\"#the-pseudocode\"\u003eThe pseudocode\u003c/a\u003e - \u003ca href=\"#insertion-sort\"\u003eInsertion Sort\u003c/a\u003e - \u003ca\n                href=\"#the-algorithm-insert-into-the-sorted-region\"\u003eThe algorithm: insert into the sorted region\u003c/a\u003e -\n            \u003ca href=\"#the-steps\"\u003eThe Steps\u003c/a\u003e - \u003ca href=\"#the-pseudocode-1\"\u003eThe pseudocode\u003c/a\u003e - \u003ca\n                href=\"#merge-sort\"\u003eMerge Sort\u003c/a\u003e - \u003ca href=\"#the-algorithm-divide-and-conquer\"\u003eThe algorithm: divide\n                and\n                conquer\u003c/a\u003e - \u003ca href=\"#quick-sort\"\u003eQuick Sort\u003c/a\u003e - \u003ca href=\"#how-does-it-work\"\u003eHow does it work?\u003c/a\u003e -\n            \u003ca href=\"#the-algorithm-divide-and-conquer-1\"\u003eThe algorithm: divide and conquer\u003c/a\u003e - \u003ca\n                href=\"#the-pseudocode-2\"\u003eThe pseudocode\u003c/a\u003e - \u003ca href=\"#binary-search\"\u003eBinary Search\u003c/a\u003e - \u003ca\n                href=\"#the-algorithm-check-the-middle-and-half-the-search-space\"\u003eThe Algorithm: \"check the middle and\n                half\n                the search space\"\u003c/a\u003e - \u003ca href=\"#the-pseudocode-3\"\u003eThe pseudocode\u003c/a\u003e - \u003ca\n                href=\"#bubble-sort-analysis\"\u003eBubble Sort Analysis\u003c/a\u003e - \u003ca href=\"#time-complexity-onsup2sup\"\u003eTime\n                Complexity: O(n2)\u003c/a\u003e - \u003ca href=\"#space-complexity-o1\"\u003eSpace Complexity: O(1)\u003c/a\u003e - \u003ca\n                href=\"#when-should-you-use-bubble-sort\"\u003eWhen should you use Bubble Sort?\u003c/a\u003e - \u003ca\n                href=\"#selection-sort-analysis\"\u003eSelection Sort Analysis\u003c/a\u003e - \u003ca\n                href=\"#selection-sort-js-implementation\"\u003eSelection Sort JS Implementation\u003c/a\u003e - \u003ca\n                href=\"#time-complexity-analysis\"\u003eTime Complexity Analysis\u003c/a\u003e - \u003ca\n                href=\"#space-complexity-analysis-o1\"\u003eSpace Complexity Analysis: O(1)\u003c/a\u003e - \u003ca\n                href=\"#when-should-we-use-selection-sort\"\u003eWhen should we use Selection Sort?\u003c/a\u003e - \u003ca\n                href=\"#insertion-sort-analysis\"\u003eInsertion Sort Analysis\u003c/a\u003e - \u003ca\n                href=\"#time-and-space-complexity-analysis\"\u003eTime and Space Complexity Analysis\u003c/a\u003e - \u003ca\n                href=\"#when-should-you-use-insertion-sort\"\u003eWhen should you use Insertion Sort?\u003c/a\u003e - \u003ca\n                href=\"#merge-sort-analysis\"\u003eMerge Sort Analysis\u003c/a\u003e - \u003ca href=\"#full-code\"\u003eFull code\u003c/a\u003e - \u003ca\n                href=\"#merging-two-sorted-arrays\"\u003eMerging two sorted arrays\u003c/a\u003e - \u003ca\n                href=\"#divide-and-conquer-step-by-step\"\u003eDivide and conquer, step-by-step\u003c/a\u003e - \u003ca\n                href=\"#time-and-space-complexity-analysis-1\"\u003eTime and Space Complexity Analysis\u003c/a\u003e - \u003ca\n                href=\"#quick-sort-analysis\"\u003eQuick Sort Analysis\u003c/a\u003e - \u003ca\n                href=\"#time-and-space-complexity-analysis-2\"\u003eTime\n                and Space Complexity Analysis\u003c/a\u003e - \u003ca href=\"#binary-search-analysis\"\u003eBinary Search Analysis\u003c/a\u003e - \u003ca\n                href=\"#time-and-space-complexity-analysis-3\"\u003eTime and Space Complexity Analysis\u003c/a\u003e - \u003ca\n                href=\"#practice-bubble-sort\"\u003ePractice: Bubble Sort\u003c/a\u003e - \u003ca href=\"#practice-selection-sort\"\u003ePractice:\n                Selection Sort\u003c/a\u003e - \u003ca href=\"#practice-insertion-sort\"\u003ePractice: Insertion Sort\u003c/a\u003e - \u003ca\n                href=\"#practice-merge-sort\"\u003ePractice: Merge Sort\u003c/a\u003e - \u003ca href=\"#practice-quick-sort-2\"\u003ePractice: Quick\n                Sort\u003c/a\u003e - \u003ca href=\"#practice-binary-search\"\u003ePractice: Binary Search\u003c/a\u003e\n        \u003c/p\u003e\n        \u003cp\u003e\u003ca href=\"#lists-stacks-and-queues-\"\u003e\u003cstrong\u003eLists, Stacks, and Queues \u003c/strong\u003e\u003c/a\u003e - \u003ca\n                href=\"#linked-lists\"\u003eLinked Lists\u003c/a\u003e - \u003ca href=\"#what-is-a-linked-list\"\u003eWhat is a Linked List?\u003c/a\u003e - \u003ca\n                href=\"#types-of-linked-lists\"\u003eTypes of Linked Lists\u003c/a\u003e - \u003ca href=\"#linked-list-methods\"\u003eLinked List\n                Methods\u003c/a\u003e - \u003ca href=\"#time-and-space-complexity-analysis-4\"\u003eTime and Space Complexity Analysis\u003c/a\u003e -\n            \u003ca href=\"#time-complexity-access-and-search\"\u003eTime Complexity - Access and Search\u003c/a\u003e - \u003ca\n                href=\"#time-complexity-insertion-and-deletion\"\u003eTime Complexity - Insertion and Deletion\u003c/a\u003e - \u003ca\n                href=\"#space-complexity-1\"\u003eSpace Complexity\u003c/a\u003e - \u003ca href=\"#stacks-and-queues\"\u003eStacks and Queues\u003c/a\u003e -\n            \u003ca href=\"#what-is-a-stack\"\u003eWhat is a Stack?\u003c/a\u003e - \u003ca href=\"#what-is-a-queue\"\u003eWhat is a Queue?\u003c/a\u003e - \u003ca\n                href=\"#stack-and-queue-properties\"\u003eStack and Queue Properties\u003c/a\u003e - \u003ca href=\"#stack-methods\"\u003eStack\n                Methods\u003c/a\u003e - \u003ca href=\"#queue-methods\"\u003eQueue Methods\u003c/a\u003e - \u003ca\n                href=\"#time-and-space-complexity-analysis-5\"\u003eTime and Space Complexity Analysis\u003c/a\u003e - \u003ca\n                href=\"#when-should-we-use-stacks-and-queues\"\u003eWhen should we use Stacks and Queues?\u003c/a\u003e - \u003ca \u003c/p\u003e\n                \u003cp\u003e\u003ca href=\"#graphs-and-heaps-\"\u003e\u003cstrong\u003eGraphs and Heaps \u003c/strong\u003e\u003c/a\u003e - \u003ca\n                        href=\"#introduction-to-heaps\"\u003eIntroduction to Heaps\u003c/a\u003e - \u003ca\n                        href=\"#binary-heap-implementation\"\u003eBinary\n                        Heap\n                        Implementation\u003c/a\u003e - \u003ca href=\"#heap-sort\"\u003eHeap Sort\u003c/a\u003e - \u003ca href=\"#in-place-heap-sort\"\u003eIn-Place\n                        Heap\n                        Sort\u003c/a\u003e - \u003c/p\u003e\n                \u003c!-- /code_chunk_output --\u003e\n                \u003chr /\u003e\n\n                \u003ch1 id=\"big-o-\"\u003eBig O \u003c/h1\u003e\n                \u003cp\u003e\u003cstrong\u003eThe objective of this lesson\u003c/strong\u003e is get you comfortable with identifying the time and\n                    space\n                    complexity of code you see. Being able to diagnose time complexity for algorithms is an essential\n                    for\n                    interviewing software engineers.\u003c/p\u003e\n                \u003cp\u003eAt the end of this, you will be able to\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eOrder the common complexity classes according to their growth rate\u003c/li\u003e\n                    \u003cli\u003eIdentify the complexity classes of common sort methods\u003c/li\u003e\n                    \u003cli\u003eIdentify complexity classes of codeable with identifying the time and space complexity of code\n                        you see.\n                        Being able to diagnose time complexity for algorithms is an essential for interviewing software\n                        engineers.\n                    \u003c/li\u003e\n                \u003c/ol\u003e\n                \u003cp\u003eAt the end of this, you will be able to\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eOrder the common complexity classes according to their growth rate\u003c/li\u003e\n                    \u003cli\u003eIdentify the complexity classes of common sort methods\u003c/li\u003e\n                    \u003cli\u003eIdentify complexity classes of code\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"memoization-and-tabulation-\"\u003eMemoization And Tabulation \u003c/h1\u003e\n                \u003cp\u003e\u003cstrong\u003eThe objective of this lesson\u003c/strong\u003e is to give you a couple of ways to optimize a\n                    computation\n                    (algorithm) from a higher complexity class to a lower complexity class. Being able to optimize\n                    algorithms is\n                    an\n                    essential for interviewing software engineers.\u003c/p\u003e\n                \u003cp\u003eAt the end of this, you will be able to\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eApply memoization to recursive problems to make them less than polynomial time.\u003c/li\u003e\n                    \u003cli\u003eApply tabulation to iterative problems to make them less than polynomial time.** is to give you\n                        a couple\n                        of\n                        ways to optimize a computation (algorithm) from a higher complexity class to a lower complexity\n                        class.\n                        Being\n                        able to optimize algorithms is an essential for interviewing software engineers.\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003cp\u003eAt the end of this, you will be able to\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eApply memoization to recursive problems to make them less than polynomial time.\u003c/li\u003e\n                    \u003cli\u003eApply tabulation to iterative problems to make them less than polynomial time.\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"recursion-videos\"\u003eRecursion Videos\u003c/h1\u003e\n                \u003cp\u003eA lot of algorithms that we use in the upcoming days will use recursion. The next two videos are just\n                    helpful\n                    reminders about recursion so that you can get that thought process back into your brain.\u003c/p\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"big-o-by-colt-steele\"\u003eBig-O By Colt Steele\u003c/h1\u003e\n                \u003cp\u003eColt Steele provides a very nice, non-mathy introduction to Big-O notation. Please watch this so you\n                    can get\n                    the\n                    easy introduction. Big-O is, by its very nature, math based. It's good to get an understanding\n                    before\n                    jumping in\n                    to math expressions.\u003c/p\u003e\n                \u003cp\u003e\u003ca href=\"https://www.youtube.com/embed/kS_gr2_-ws8\"\u003eComplete Beginner's Guide to Big O Notation\u003c/a\u003e\n                    by Colt\n                    Steele.\u003c/p\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"curating-complexity-a-guide-to-big-o-notation\"\u003eCurating Complexity: A Guide to Big-O Notation\n                \u003c/h1\u003e\n                \u003cp\u003eAs software engineers, our goal is not just to solve problems. Rather, our goal is to solve problems\n                    efficiently\n                    and elegantly. Not all solutions are made equal! In this section we'll explore how to analyze the\n                    efficiency\n                    of\n                    algorithms in terms of their speed (\u003cem\u003etime complexity\u003c/em\u003e) and memory consumption (\u003cem\u003espace\n                        complexity\u003c/em\u003e).\u003c/p\u003e\n                \u003cblockquote\u003e\n                    \u003cp\u003eIn this article, we'll use the word \u003cem\u003eefficiency\u003c/em\u003e to describe the amount of resources a\n                        program\n                        needs\n                        to execute. The two resources we are concerned with are \u003cem\u003etime\u003c/em\u003e and \u003cem\u003espace\u003c/em\u003e. Our\n                        goal is to\n                        \u003cem\u003eminimize\u003c/em\u003e the amount of time and space that our programs use.\n                    \u003c/p\u003e\n                \u003c/blockquote\u003e\n                \u003cp\u003eWhen you finish this article you will be able to:\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003eexplain why computer scientists use Big-O notation\u003c/li\u003e\n                    \u003cli\u003esimplify a mathematical function into Big-O notation\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003ch2 id=\"why-big-o\"\u003eWhy Big-O?\u003c/h2\u003e\n                \u003cp\u003eLet's begin by understanding what method we should \u003cem\u003enot\u003c/em\u003e use when describing the efficiency of\n                    our\n                    algorithms. Most importantly, we'll want to avoid using absolute units of time when describing\n                    speed. When\n                    the\n                    software engineer exclaims, \"My function runs in 0.2 seconds, it's so fast!!!\", the computer\n                    scientist is\n                    not\n                    impressed. Skeptical, the computer scientist asks the following questions:\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eWhat computer did you run it on? \u003cem\u003eMaybe the credit belongs to the hardware and not the\n                            software. Some\n                            hardware architectures will be better for certain operations than others.\u003c/em\u003e\u003c/li\u003e\n                    \u003cli\u003eWere there other background processes running on the computer that could have effected the\n                        runtime?\n                        \u003cem\u003eIt's\n                            hard to control the environment during performance experiments.\u003c/em\u003e\n                    \u003c/li\u003e\n                    \u003cli\u003eWill your code still be performant if we increase the size of the input? \u003cem\u003eFor example,\n                            sorting 3\n                            numbers\n                            is trivial; but how about a million numbers?\u003c/em\u003e\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003cp\u003eThe job of the software engineer is to focus on the software detail and not necessarily the hardware\n                    it will\n                    run\n                    on. Because we can't answer points 1 and 2 with total certainty, we'll want to avoid using concrete\n                    units\n                    like\n                    \"milliseconds\" or \"seconds\" when describing the efficiency of our algorithms. Instead, we'll opt for\n                    a more\n                    abstract approach that focuses on point 3. This means that we should focus on how the performance of\n                    our\n                    algorithm is affected by increasing the size of the input. \u003cstrong\u003eIn other words, how does our\n                        performance\n                        scale?\u003c/strong\u003e\u003c/p\u003e\n                \u003cblockquote\u003e\n                    \u003cp\u003eThe argument above focuses on \u003cem\u003etime\u003c/em\u003e, but a similar argument could also be made for\n                        \u003cem\u003espace\u003c/em\u003e.\n                        For example, we should not analyze our code in terms of the amount of absolute kilobytes of\n                        memory it\n                        uses,\n                        because this is dependent on the programming language.\n                    \u003c/p\u003e\n                \u003c/blockquote\u003e\n                \u003ch2 id=\"big-o-notation\"\u003eBig-O Notation\u003c/h2\u003e\n                \u003cp\u003eIn Computer Science, we use Big-O notation as a tool for describing the efficiency of algorithms with\n                    respect\n                    to\n                    the size of the input argument(s). We use mathematical functions in Big-O notation, so there are a\n                    few big\n                    picture ideas that we'll want to keep in mind:\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eThe function should be defined in terms of the size of the input(s).\u003c/li\u003e\n                    \u003cli\u003eA \u003cem\u003esmaller\u003c/em\u003e Big-O function is more desirable than a larger one. Intuitively, we want our\n                        algorithms\n                        to use a minimal amount of time and space.\u003c/li\u003e\n                    \u003cli\u003eBig-O describes the worst-case scenario for our code, also known as the upper bound. We prepare\n                        our\n                        algorithm for the worst case, because the best case is a luxury that is not guaranteed.\u003c/li\u003e\n                    \u003cli\u003eA Big-O function should be simplified to show only its most dominant mathematical term.\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003cp\u003eThe first 3 points are conceptual, so they are easy to swallow. However, point 4 is typically the\n                    biggest\n                    source\n                    of confusion when learning the notation. Before we apply Big-O to our code, we'll need to first\n                    understand\n                    the\n                    underlying math and simplification process.\u003c/p\u003e\n                \u003ch3 id=\"simplifying-math-terms\"\u003eSimplifying Math Terms\u003c/h3\u003e\n                \u003cp\u003eWe want our Big-O notation to describe the performance of our algorithm with respect to the input\n                    size and\n                    nothing else. Because of this, we should to simplify our Big-O functions using the following rules:\n                \u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003e\u003cstrong\u003eSimplify Products:\u003c/strong\u003e if the function is a product of many terms, we drop the\n                        terms that\n                        \u003cem\u003edon't\u003c/em\u003e depend on the size of the input.\n                    \u003c/li\u003e\n                    \u003cli\u003e\u003cstrong\u003eSimplify Sums:\u003c/strong\u003e if the function is a sum of many terms, we keep the term with\n                        the\n                        \u003cem\u003elargest\u003c/em\u003e growth rate and drop the other terms.\n                    \u003c/li\u003e\n                \u003c/ul\u003e\n                \u003cp\u003eWe'll look at these rules in action, but first we'll define a few things:\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003e\u003cstrong\u003en\u003c/strong\u003e is the size of the input\u003c/li\u003e\n                    \u003cli\u003e\u003cstrong\u003eT(f)\u003c/strong\u003e refers to an unsimplified mathematical \u003cstrong\u003ef\u003c/strong\u003eunction\u003c/li\u003e\n                    \u003cli\u003e\u003cstrong\u003eO(f)\u003c/strong\u003e refers to the Big-O simplified mathematical \u003cstrong\u003ef\u003c/strong\u003eunction\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003ch3 id=\"simplifying-a-product\"\u003eSimplifying a Product\u003c/h3\u003e\n                \u003cp\u003eIf a function consists of a product of many factors, we drop the factors that don't depend on the\n                    size of the\n                    input, n. The factors that we drop are called constant factors because their size remains consistent\n                    as we\n                    increase the size of the input. The reasoning behind this simplification is that we make the input\n                    large\n                    enough,\n                    the non-constant factors will overshadow the constant ones. Below are some examples:\u003c/p\u003e\n                \u003ctable\u003e\n                    \u003cthead\u003e\n                        \u003ctr class=\"header\"\u003e\n                            \u003cth\u003eUnsimplified\u003c/th\u003e\n                            \u003cth\u003eBig-O Simplified\u003c/th\u003e\n                        \u003c/tr\u003e\n                    \u003c/thead\u003e\n                    \u003ctbody\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003eT( 5 * n\u003csup\u003e2\u003c/sup\u003e )\u003c/td\u003e\n                            \u003ctd\u003eO( n\u003csup\u003e2\u003c/sup\u003e )\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003eT( 100000 * n )\u003c/td\u003e\n                            \u003ctd\u003eO( n )\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003eT( n / 12 )\u003c/td\u003e\n                            \u003ctd\u003eO( n )\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003eT( 42 * n * log(n) )\u003c/td\u003e\n                            \u003ctd\u003eO( n * log(n) )\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003eT( 12 )\u003c/td\u003e\n                            \u003ctd\u003eO( 1 )\u003c/td\u003e\n                        \u003c/tr\u003e\n                    \u003c/tbody\u003e\n                \u003c/table\u003e\n                \u003cp\u003eNote that in the third example, we can simplify \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003eT( n / 12 )\u003c/code\u003e to \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO( n\n                        )\u003c/code\u003e\n                    because we\n                    can\n                    rewrite a division into an equivalent multiplication. In other words, \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003eT( n / 12 ) = T( 1/12 *\n                        n ) = O(\n                        n\n                        )\u003c/code\u003e.\u003c/p\u003e\n                \u003ch3 id=\"simplifying-a-sum\"\u003eSimplifying a Sum\u003c/h3\u003e\n                \u003cp\u003eIf the function consists of a sum of many terms, we only need to show the term that grows the\n                    fastest,\n                    relative\n                    to the size of the input. The reasoning behind this simplification is that if we make the input\n                    large\n                    enough,\n                    the fastest growing term will overshadow the other, smaller terms. To understand which term to keep,\n                    you'll\n                    need\n                    to recall the relative size of our common math terms from the previous section. Below are some\n                    examples:\u003c/p\u003e\n                \u003ctable\u003e\n                    \u003cthead\u003e\n                        \u003ctr class=\"header\"\u003e\n                            \u003cth\u003eUnsimplified\u003c/th\u003e\n                            \u003cth\u003eBig-O Simplified\u003c/th\u003e\n                        \u003c/tr\u003e\n                    \u003c/thead\u003e\n                    \u003ctbody\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003eT( n\u003csup\u003e3\u003c/sup\u003e + n\u003csup\u003e2\u003c/sup\u003e + n )\u003c/td\u003e\n                            \u003ctd\u003eO( n\u003csup\u003e3\u003c/sup\u003e )\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003eT( log(n) + 2\u003csup\u003en\u003c/sup\u003e )\u003c/td\u003e\n                            \u003ctd\u003eO( 2\u003csup\u003en\u003c/sup\u003e )\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003eT( n + log(n) )\u003c/td\u003e\n                            \u003ctd\u003eO( n )\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003eT( n! + 10\u003csup\u003en\u003c/sup\u003e )\u003c/td\u003e\n                            \u003ctd\u003eO( n! )\u003c/td\u003e\n                        \u003c/tr\u003e\n                    \u003c/tbody\u003e\n                \u003c/table\u003e\n                \u003ch3 id=\"putting-it-all-together\"\u003ePutting it all together\u003c/h3\u003e\n                \u003cp\u003eThe \u003cem\u003eproduct\u003c/em\u003e and \u003cem\u003esum\u003c/em\u003e rules are all we'll need to Big-O simplify any math functions.\n                    We just\n                    apply the \u003cem\u003eproduct rule\u003c/em\u003e to drop all constants, then apply the \u003cem\u003esum rule\u003c/em\u003e to select\n                    the single\n                    most dominant term.\u003c/p\u003e\n                \u003ctable\u003e\n                    \u003cthead\u003e\n                        \u003ctr class=\"header\"\u003e\n                            \u003cth\u003eUnsimplified\u003c/th\u003e\n                            \u003cth\u003eBig-O Simplified\u003c/th\u003e\n                        \u003c/tr\u003e\n                    \u003c/thead\u003e\n                    \u003ctbody\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003eT( 5n\u003csup\u003e2\u003c/sup\u003e + 99n )\u003c/td\u003e\n                            \u003ctd\u003eO( n\u003csup\u003e2\u003c/sup\u003e )\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003eT( 2n + nlog(n) )\u003c/td\u003e\n                            \u003ctd\u003eO( nlog(n) )\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003eT( 2\u003csup\u003en\u003c/sup\u003e + 5n\u003csup\u003e1000\u003c/sup\u003e)\u003c/td\u003e\n                            \u003ctd\u003eO( 2\u003csup\u003en\u003c/sup\u003e )\u003c/td\u003e\n                        \u003c/tr\u003e\n                    \u003c/tbody\u003e\n                \u003c/table\u003e\n                \u003cblockquote\u003e\n                    \u003cp\u003eAside: We'll often omit the multiplication symbol in expressions as a form of shorthand. For\n                        example,\n                        we'll\n                        write \u003cem\u003eO( 5n\u003csup\u003e2\u003c/sup\u003e )\u003c/em\u003e in place of \u003cem\u003eO( 5 * n\u003csup\u003e2\u003c/sup\u003e )\u003c/em\u003e.\u003c/p\u003e\n                \u003c/blockquote\u003e\n                \u003ch2 id=\"what-youve-learned\"\u003eRECAP\u003c/h2\u003e\n                \u003cp\u003e\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003eexplained why Big-O is the preferred notation used to describe the efficiency of algorithms\u003c/li\u003e\n                    \u003cli\u003eused the product and sum rules to simplify mathematical functions into Big-O notation\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"common-complexity-classes\"\u003eCommon Complexity Classes\u003c/h1\u003e\n                \u003cp\u003eAnalyzing the efficiency of our code seems like a daunting task because there are many different\n                    possibilities in\n                    how we may choose to implement something. Luckily, most code we write can be categorized into one of\n                    a\n                    handful\n                    of common complexity classes. In this reading, we'll identify the common classes and explore some of\n                    the\n                    code\n                    characteristics that will lead to these classes.\u003c/p\u003e\n                \u003cp\u003eWhen you finish this reading, you should be able to:\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003ename \u003cem\u003eand\u003c/em\u003e order the seven common complexity classes\u003c/li\u003e\n                    \u003cli\u003eidentify the time complexity class of a given code snippet\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003ch2 id=\"the-seven-major-classes\"\u003eThe seven major classes\u003c/h2\u003e\n                \u003cp\u003eThere are seven complexity classes that we will encounter most often. Below is a list of each\n                    complexity\n                    class as\n                    well as its Big-O notation. This list is ordered from \u003cem\u003esmallest to largest\u003c/em\u003e. Bear in mind\n                    that a\n                    \"more\n                    efficient\" algorithm is one with a smaller complexity class, because it requires fewer resources.\n                \u003c/p\u003e\n                \u003ctable\u003e\n                    \u003cthead\u003e\n                        \u003ctr class=\"header\"\u003e\n                            \u003cth\u003eBig-O\u003c/th\u003e\n                            \u003cth\u003eComplexity Class Name\u003c/th\u003e\n                        \u003c/tr\u003e\n                    \u003c/thead\u003e\n                    \u003ctbody\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003eO(1)\u003c/td\u003e\n                            \u003ctd\u003econstant\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003eO(log(n))\u003c/td\u003e\n                            \u003ctd\u003elogarithmic\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003eO(n)\u003c/td\u003e\n                            \u003ctd\u003elinear\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003eO(n * log(n))\u003c/td\u003e\n                            \u003ctd\u003eloglinear, linearithmic, quasilinear\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003eO(n\u003csup\u003ec\u003c/sup\u003e) - O(n\u003csup\u003e2\u003c/sup\u003e), O(n\u003csup\u003e3\u003c/sup\u003e), etc.\u003c/td\u003e\n                            \u003ctd\u003epolynomial\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003eO(c\u003csup\u003en\u003c/sup\u003e) - O(2\u003csup\u003en\u003c/sup\u003e), O(3\u003csup\u003en\u003c/sup\u003e), etc.\u003c/td\u003e\n                            \u003ctd\u003eexponential\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003eO(n!)\u003c/td\u003e\n                            \u003ctd\u003efactorial\u003c/td\u003e\n                        \u003c/tr\u003e\n                    \u003c/tbody\u003e\n                \u003c/table\u003e\n                \u003cp\u003eThere are more complexity classes that exist, but these are most common. Let's take a closer look at\n                    each of\n                    these classes to gain some intuition on what behavior their functions define. We'll explore famous\n                    algorithms\n                    that correspond to these classes further in the course.\u003c/p\u003e\n                \u003cp\u003eFor simplicity, we'll provide small, generic code examples that illustrate the complexity, although\n                    they may\n                    not\n                    solve a practical problem.\u003c/p\u003e\n                \u003ch3 id=\"o1---constant\"\u003eO(1) - Constant\u003c/h3\u003e\n                \u003cp\u003eConstant complexity means that the algorithm takes roughly the same number of steps for any size\n                    input. In a\n                    constant time algorithm, there is no relationship between the size of the input and the number of\n                    steps\n                    required. For example, this means performing the algorithm on a input of size 1 takes the same\n                    number of\n                    steps\n                    as performing it on an input of size 128.\u003c/p\u003e\n                \u003ch4 id=\"constant-growth\"\u003eConstant growth\u003c/h4\u003e\n                \u003cp\u003eThe table below shows the growing behavior of a constant function. Notice that the behavior stays\n                    \u003cem\u003econstant\u003c/em\u003e for all values of n.\n                \u003c/p\u003e\n                \u003ctable\u003e\n                    \u003cthead\u003e\n                        \u003ctr class=\"header\"\u003e\n                            \u003cth\u003en\u003c/th\u003e\n                            \u003cth\u003eO(1)\u003c/th\u003e\n                        \u003c/tr\u003e\n                    \u003c/thead\u003e\n                    \u003ctbody\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e1\u003c/td\u003e\n                            \u003ctd\u003e~1\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e2\u003c/td\u003e\n                            \u003ctd\u003e~1\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e3\u003c/td\u003e\n                            \u003ctd\u003e~1\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e…\u003c/td\u003e\n                            \u003ctd\u003e…\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e128\u003c/td\u003e\n                            \u003ctd\u003e~1\u003c/td\u003e\n                        \u003c/tr\u003e\n                    \u003c/tbody\u003e\n                \u003c/table\u003e\n                \u003ch4 id=\"example-constant-code\"\u003eExample Constant code\u003c/h4\u003e\n                \u003cp\u003eBelow is are two examples of functions that have constant runtimes.\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb1\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\" class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode javascript\"\u003e\u003ca class=\"sourceLine\" id=\"cb1-1\" title=\"1\"\u003e\u003cspan class=\"co\"\u003e// O(1)\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-2\" title=\"2\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003econstant1\u003c/span\u003e(n) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-3\" title=\"3\"\u003e  \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e n \u003cspan class=\"op\"\u003e*\u003c/span\u003e \u003cspan class=\"dv\"\u003e2\u003c/span\u003e \u003cspan class=\"op\"\u003e+\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-4\" title=\"4\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-5\" title=\"5\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-6\" title=\"6\"\u003e\u003cspan class=\"co\"\u003e// O(1)\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-7\" title=\"7\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003econstant2\u003c/span\u003e(n) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-8\" title=\"8\"\u003e  \u003cspan class=\"cf\"\u003efor\u003c/span\u003e (\u003cspan class=\"kw\"\u003elet\u003c/span\u003e i \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e i \u003cspan class=\"op\"\u003e\u0026lt;=\u003c/span\u003e \u003cspan class=\"dv\"\u003e100\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e i\u003cspan class=\"op\"\u003e++\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-9\" title=\"9\"\u003e    \u003cspan class=\"va\"\u003econsole\u003c/span\u003e.\u003cspan class=\"at\"\u003elog\u003c/span\u003e(i)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-10\" title=\"10\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-11\" title=\"11\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eThe runtime of the \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003econstant1\u003c/code\u003e function\n                    does not depend on the size of the input, because\n                    only two\n                    arithmetic operations (multiplication and addition) are always performed. The runtime of the\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003econstant2\u003c/code\u003e function also does not\n                    depend on the size of the input because one-hundred\n                    iterations\n                    are\n                    always performed, irrespective of the input.\n                \u003c/p\u003e\n                \u003ch3 id=\"ologn---logarithmic\"\u003eO(log(n)) - Logarithmic\u003c/h3\u003e\n                \u003cp\u003eTypically, the hidden base of O(log(n)) is 2, meaning O(log\u003csub\u003e2\u003c/sub\u003e(n)). Logarithmic complexity\n                    algorithms\n                    will usual display a sense of continually \"halving\" the size of the input. Another tell of a\n                    logarithmic\n                    algorithm is that we don't have to access every element of the input. O(log\u003csub\u003e2\u003c/sub\u003e(n)) means\n                    that every\n                    time we double the size of the input, we only require one additional step. Overall, this means that\n                    a large\n                    increase of input size will increase the number of steps required by a small amount.\u003c/p\u003e\n                \u003ch4 id=\"logarithmic-growth\"\u003eLogarithmic growth\u003c/h4\u003e\n                \u003cp\u003eThe table below shows the growing behavior of a logarithmic runtime function. Notice that doubling\n                    the input\n                    size\n                    will only require only one additional \"step\".\u003c/p\u003e\n                \u003ctable\u003e\n                    \u003cthead\u003e\n                        \u003ctr class=\"header\"\u003e\n                            \u003cth\u003en\u003c/th\u003e\n                            \u003cth\u003eO(log\u003csub\u003e2\u003c/sub\u003e(n))\u003c/th\u003e\n                        \u003c/tr\u003e\n                    \u003c/thead\u003e\n                    \u003ctbody\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e2\u003c/td\u003e\n                            \u003ctd\u003e~1\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e4\u003c/td\u003e\n                            \u003ctd\u003e~2\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e8\u003c/td\u003e\n                            \u003ctd\u003e~3\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e16\u003c/td\u003e\n                            \u003ctd\u003e~4\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e…\u003c/td\u003e\n                            \u003ctd\u003e…\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e128\u003c/td\u003e\n                            \u003ctd\u003e~7\u003c/td\u003e\n                        \u003c/tr\u003e\n                    \u003c/tbody\u003e\n                \u003c/table\u003e\n                \u003ch4 id=\"example-logarithmic-code\"\u003eExample logarithmic code\u003c/h4\u003e\n                \u003cp\u003eBelow is an example of two functions with logarithmic runtimes.\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb2\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\" class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode javascript\"\u003e\u003ca class=\"sourceLine\" id=\"cb2-1\" title=\"1\"\u003e\u003cspan class=\"co\"\u003e// O(log(n))\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-2\" title=\"2\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003elogarithmic1\u003c/span\u003e(n) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-3\" title=\"3\"\u003e  \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (n \u003cspan class=\"op\"\u003e\u0026lt;=\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e) \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-4\" title=\"4\"\u003e  \u003cspan class=\"at\"\u003elogarithmic1\u003c/span\u003e(n / \u003cspan class=\"dv\"\u003e2\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-5\" title=\"5\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-6\" title=\"6\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-7\" title=\"7\"\u003e\u003cspan class=\"co\"\u003e// O(log(n))\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-8\" title=\"8\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003elogarithmic2\u003c/span\u003e(n) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-9\" title=\"9\"\u003e  \u003cspan class=\"kw\"\u003elet\u003c/span\u003e i \u003cspan class=\"op\"\u003e=\u003c/span\u003e n\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-10\" title=\"10\"\u003e  \u003cspan class=\"cf\"\u003ewhile\u003c/span\u003e (i \u003cspan class=\"op\"\u003e\u0026gt;\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-11\" title=\"11\"\u003e    i /\u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"dv\"\u003e2\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-12\" title=\"12\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-13\" title=\"13\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eThe \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003elogarithmic1\u003c/code\u003e function has\n                    O(log(n)) runtime because the recursion will half the\n                    argument, n,\n                    each time. In other words, if we pass 8 as the original argument, then the recursive chain would be\n                    8 -\u0026gt;\n                    4\n                    -\u0026gt; 2 -\u0026gt; 1. In a similar way, the \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003elogarithmic2\u003c/code\u003e function has O(log(n)) runtime\n                    because of\n                    the\n                    number of iterations in the while loop. The while loop depends on the variable \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003ei\u003c/code\u003e, which\n                    will be\n                    divided in half each iteration.\u003c/p\u003e\n                \u003ch3 id=\"on---linear\"\u003eO(n) - Linear\u003c/h3\u003e\n                \u003cp\u003eLinear complexity algorithms will access each item of the input \"once\" (in the Big-O sense).\n                    Algorithms that\n                    iterate through the input without nested loops or recurse by reducing the size of the input by \"one\"\n                    each\n                    time\n                    are typically linear.\u003c/p\u003e\n                \u003ch4 id=\"linear-growth\"\u003eLinear growth\u003c/h4\u003e\n                \u003cp\u003eThe table below shows the growing behavior of a linear runtime function. Notice that a change in\n                    input size\n                    leads\n                    to similar change in the number of steps.\u003c/p\u003e\n                \u003ctable\u003e\n                    \u003cthead\u003e\n                        \u003ctr class=\"header\"\u003e\n                            \u003cth\u003en\u003c/th\u003e\n                            \u003cth\u003eO(n)\u003c/th\u003e\n                        \u003c/tr\u003e\n                    \u003c/thead\u003e\n                    \u003ctbody\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e1\u003c/td\u003e\n                            \u003ctd\u003e~1\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e2\u003c/td\u003e\n                            \u003ctd\u003e~2\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e3\u003c/td\u003e\n                            \u003ctd\u003e~3\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e4\u003c/td\u003e\n                            \u003ctd\u003e~4\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e…\u003c/td\u003e\n                            \u003ctd\u003e…\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e128\u003c/td\u003e\n                            \u003ctd\u003e~128\u003c/td\u003e\n                        \u003c/tr\u003e\n                    \u003c/tbody\u003e\n                \u003c/table\u003e\n                \u003ch4 id=\"example-linear-code\"\u003eExample linear code\u003c/h4\u003e\n                \u003cp\u003eBelow are examples of three functions that each have linear runtime.\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb3\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\" class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode javascript\"\u003e\u003ca class=\"sourceLine\" id=\"cb3-1\" title=\"1\"\u003e\u003cspan class=\"co\"\u003e// O(n)\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-2\" title=\"2\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003elinear1\u003c/span\u003e(n) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-3\" title=\"3\"\u003e  \u003cspan class=\"cf\"\u003efor\u003c/span\u003e (\u003cspan class=\"kw\"\u003elet\u003c/span\u003e i \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e i \u003cspan class=\"op\"\u003e\u0026lt;=\u003c/span\u003e n\u003cspan class=\"op\"\u003e;\u003c/span\u003e i\u003cspan class=\"op\"\u003e++\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-4\" title=\"4\"\u003e    \u003cspan class=\"va\"\u003econsole\u003c/span\u003e.\u003cspan class=\"at\"\u003elog\u003c/span\u003e(i)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-5\" title=\"5\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-6\" title=\"6\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-7\" title=\"7\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-8\" title=\"8\"\u003e\u003cspan class=\"co\"\u003e// O(n), where n is the length of the array\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-9\" title=\"9\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003elinear2\u003c/span\u003e(array) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-10\" title=\"10\"\u003e  \u003cspan class=\"cf\"\u003efor\u003c/span\u003e (\u003cspan class=\"kw\"\u003elet\u003c/span\u003e i \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e i \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e i\u003cspan class=\"op\"\u003e++\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-11\" title=\"11\"\u003e    \u003cspan class=\"va\"\u003econsole\u003c/span\u003e.\u003cspan class=\"at\"\u003elog\u003c/span\u003e(i)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-12\" title=\"12\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-13\" title=\"13\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-14\" title=\"14\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-15\" title=\"15\"\u003e\u003cspan class=\"co\"\u003e// O(n)\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-16\" title=\"16\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003elinear3\u003c/span\u003e(n) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-17\" title=\"17\"\u003e  \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (n \u003cspan class=\"op\"\u003e===\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e) \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-18\" title=\"18\"\u003e  \u003cspan class=\"at\"\u003elinear3\u003c/span\u003e(n \u003cspan class=\"op\"\u003e-\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb3-19\" title=\"19\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eThe \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003elinear1\u003c/code\u003e function has O(n) runtime\n                    because the for loop will iterate n times. The\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003elinear2\u003c/code\u003e function has O(n) runtime\n                    because the for loop iterates through the array\n                    argument. The\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003elinear3\u003c/code\u003e function has O(n) runtime\n                    because each subsequent call in the recursion will\n                    decrease\n                    the\n                    argument by one. In other words, if we pass 8 as the original argument to \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003elinear3\u003c/code\u003e, the\n                    recursive\n                    chain would be 8 -\u0026gt; 7 -\u0026gt; 6 -\u0026gt; 5 -\u0026gt; … -\u0026gt; 1.\n                \u003c/p\u003e\n                \u003ch3 id=\"on-logn---loglinear\"\u003eO(n * log(n)) - Loglinear\u003c/h3\u003e\n                \u003cp\u003eThis class is a combination of both linear and logarithmic behavior, so features from both classes\n                    are\n                    evident.\n                    Algorithms the exhibit this behavior use both recursion and iteration. Typically, this means that\n                    the\n                    recursive\n                    calls will halve the input each time (logarithmic), but iterations are also performed on the input\n                    (linear).\n                \u003c/p\u003e\n                \u003ch4 id=\"loglinear-growth\"\u003eLoglinear growth\u003c/h4\u003e\n                \u003cp\u003eThe table below shows the growing behavior of a loglinear runtime function.\u003c/p\u003e\n                \u003ctable\u003e\n                    \u003cthead\u003e\n                        \u003ctr class=\"header\"\u003e\n                            \u003cth\u003en\u003c/th\u003e\n                            \u003cth\u003eO(n * log\u003csub\u003e2\u003c/sub\u003e(n))\u003c/th\u003e\n                        \u003c/tr\u003e\n                    \u003c/thead\u003e\n                    \u003ctbody\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e2\u003c/td\u003e\n                            \u003ctd\u003e~2\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e4\u003c/td\u003e\n                            \u003ctd\u003e~8\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e8\u003c/td\u003e\n                            \u003ctd\u003e~24\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e…\u003c/td\u003e\n                            \u003ctd\u003e…\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e128\u003c/td\u003e\n                            \u003ctd\u003e~896\u003c/td\u003e\n                        \u003c/tr\u003e\n                    \u003c/tbody\u003e\n                \u003c/table\u003e\n                \u003ch4 id=\"example-loglinear-code\"\u003eExample loglinear code\u003c/h4\u003e\n                \u003cp\u003eBelow is an example of a function with a loglinear runtime.\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb4\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\" class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode javascript\"\u003e\u003ca class=\"sourceLine\" id=\"cb4-1\" title=\"1\"\u003e\u003cspan class=\"co\"\u003e// O(n * log(n))\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb4-2\" title=\"2\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003eloglinear\u003c/span\u003e(n) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb4-3\" title=\"3\"\u003e  \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (n \u003cspan class=\"op\"\u003e\u0026lt;=\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e) \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb4-4\" title=\"4\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb4-5\" title=\"5\"\u003e  \u003cspan class=\"cf\"\u003efor\u003c/span\u003e (\u003cspan class=\"kw\"\u003elet\u003c/span\u003e i \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e i \u003cspan class=\"op\"\u003e\u0026lt;=\u003c/span\u003e n\u003cspan class=\"op\"\u003e;\u003c/span\u003e i\u003cspan class=\"op\"\u003e++\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb4-6\" title=\"6\"\u003e    \u003cspan class=\"va\"\u003econsole\u003c/span\u003e.\u003cspan class=\"at\"\u003elog\u003c/span\u003e(i)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb4-7\" title=\"7\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb4-8\" title=\"8\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb4-9\" title=\"9\"\u003e  \u003cspan class=\"at\"\u003eloglinear\u003c/span\u003e(n / \u003cspan class=\"dv\"\u003e2\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb4-10\" title=\"10\"\u003e  \u003cspan class=\"at\"\u003eloglinear\u003c/span\u003e(n / \u003cspan class=\"dv\"\u003e2\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb4-11\" title=\"11\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eThe \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eloglinear\u003c/code\u003e function has O(n *\n                    log(n)) runtime because the for loop iterates linearly\n                    (n)\n                    through\n                    the input and the recursive chain behaves logarithmically (log(n)).\u003c/p\u003e\n                \u003ch3 id=\"onc---polynomial\"\u003eO(n\u003csup\u003ec\u003c/sup\u003e) - Polynomial\u003c/h3\u003e\n                \u003cp\u003ePolynomial complexity refers to complexity of the form O(n\u003csup\u003ec\u003c/sup\u003e) where \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003en\u003c/code\u003e is the\n                    size of\n                    the\n                    input and \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ec\u003c/code\u003e is some fixed constant.\n                    For example, O(n\u003csup\u003e3\u003c/sup\u003e) is a larger/worse\n                    function\n                    than\n                    O(n\u003csup\u003e2\u003c/sup\u003e), but they belong to the same complexity class. Nested loops are usually the\n                    indicator of\n                    this\n                    complexity class.\u003c/p\u003e\n                \u003ch4 id=\"polynomial-growth\"\u003ePolynomial growth\u003c/h4\u003e\n                \u003cp\u003eBelow are tables showing the growth for O(n\u003csup\u003e2\u003c/sup\u003e) and O(n\u003csup\u003e3\u003c/sup\u003e).\u003c/p\u003e\n                \u003ctable\u003e\n                    \u003cthead\u003e\n                        \u003ctr class=\"header\"\u003e\n                            \u003cth\u003en\u003c/th\u003e\n                            \u003cth\u003eO(n\u003csup\u003e2\u003c/sup\u003e)\u003c/th\u003e\n                        \u003c/tr\u003e\n                    \u003c/thead\u003e\n                    \u003ctbody\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e1\u003c/td\u003e\n                            \u003ctd\u003e~1\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e2\u003c/td\u003e\n                            \u003ctd\u003e~4\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e3\u003c/td\u003e\n                            \u003ctd\u003e~9\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e…\u003c/td\u003e\n                            \u003ctd\u003e…\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e128\u003c/td\u003e\n                            \u003ctd\u003e~16,384\u003c/td\u003e\n                        \u003c/tr\u003e\n                    \u003c/tbody\u003e\n                \u003c/table\u003e\n                \u003ctable\u003e\n                    \u003cthead\u003e\n                        \u003ctr class=\"header\"\u003e\n                            \u003cth\u003en\u003c/th\u003e\n                            \u003cth\u003eO(n\u003csup\u003e3\u003c/sup\u003e)\u003c/th\u003e\n                        \u003c/tr\u003e\n                    \u003c/thead\u003e\n                    \u003ctbody\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e1\u003c/td\u003e\n                            \u003ctd\u003e~1\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e2\u003c/td\u003e\n                            \u003ctd\u003e~8\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e3\u003c/td\u003e\n                            \u003ctd\u003e~27\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e…\u003c/td\u003e\n                            \u003ctd\u003e…\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e128\u003c/td\u003e\n                            \u003ctd\u003e~2,097,152\u003c/td\u003e\n                        \u003c/tr\u003e\n                    \u003c/tbody\u003e\n                \u003c/table\u003e\n                \u003ch4 id=\"example-polynomial-code\"\u003eExample polynomial code\u003c/h4\u003e\n                \u003cp\u003eBelow are examples of two functions with polynomial runtimes.\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb5\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\" class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode javascript\"\u003e\u003ca class=\"sourceLine\" id=\"cb5-1\" title=\"1\"\u003e\u003cspan class=\"co\"\u003e// O(n^2)\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb5-2\" title=\"2\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003equadratic\u003c/span\u003e(n) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb5-3\" title=\"3\"\u003e  \u003cspan class=\"cf\"\u003efor\u003c/span\u003e (\u003cspan class=\"kw\"\u003elet\u003c/span\u003e i \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e i \u003cspan class=\"op\"\u003e\u0026lt;=\u003c/span\u003e n\u003cspan class=\"op\"\u003e;\u003c/span\u003e i\u003cspan class=\"op\"\u003e++\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb5-4\" title=\"4\"\u003e    \u003cspan class=\"cf\"\u003efor\u003c/span\u003e (\u003cspan class=\"kw\"\u003elet\u003c/span\u003e j \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e j \u003cspan class=\"op\"\u003e\u0026lt;=\u003c/span\u003e n\u003cspan class=\"op\"\u003e;\u003c/span\u003e j\u003cspan class=\"op\"\u003e++\u003c/span\u003e) \u003cspan class=\"op\"\u003e{}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb5-5\" title=\"5\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb5-6\" title=\"6\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb5-7\" title=\"7\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb5-8\" title=\"8\"\u003e\u003cspan class=\"co\"\u003e// O(n^3)\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb5-9\" title=\"9\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003ecubic\u003c/span\u003e(n) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb5-10\" title=\"10\"\u003e  \u003cspan class=\"cf\"\u003efor\u003c/span\u003e (\u003cspan class=\"kw\"\u003elet\u003c/span\u003e i \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e i \u003cspan class=\"op\"\u003e\u0026lt;=\u003c/span\u003e n\u003cspan class=\"op\"\u003e;\u003c/span\u003e i\u003cspan class=\"op\"\u003e++\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb5-11\" title=\"11\"\u003e    \u003cspan class=\"cf\"\u003efor\u003c/span\u003e (\u003cspan class=\"kw\"\u003elet\u003c/span\u003e j \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e j \u003cspan class=\"op\"\u003e\u0026lt;=\u003c/span\u003e n\u003cspan class=\"op\"\u003e;\u003c/span\u003e j\u003cspan class=\"op\"\u003e++\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb5-12\" title=\"12\"\u003e      \u003cspan class=\"cf\"\u003efor\u003c/span\u003e (\u003cspan class=\"kw\"\u003elet\u003c/span\u003e k \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e k \u003cspan class=\"op\"\u003e\u0026lt;=\u003c/span\u003e n\u003cspan class=\"op\"\u003e;\u003c/span\u003e k\u003cspan class=\"op\"\u003e++\u003c/span\u003e) \u003cspan class=\"op\"\u003e{}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb5-13\" title=\"13\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb5-14\" title=\"14\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb5-15\" title=\"15\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eThe \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003equadratic\u003c/code\u003e function has\n                    O(n\u003csup\u003e2\u003c/sup\u003e) runtime because there are nested loops. The\n                    outer\n                    loop\n                    iterates n times and the inner loop iterates n times. This leads to n * n total number of\n                    iterations. In a\n                    similar way, the \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ecubic\u003c/code\u003e function has\n                    O(n\u003csup\u003e3\u003c/sup\u003e) runtime because it has triply\n                    nested loops\n                    that lead to a total of n * n * n iterations.\u003c/p\u003e\n                \u003ch3 id=\"ocn---exponential\"\u003eO(c\u003csup\u003en\u003c/sup\u003e) - Exponential\u003c/h3\u003e\n                \u003cp\u003eExponential complexity refers to Big-O functions of the form O(c\u003csup\u003en\u003c/sup\u003e) where \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003en\u003c/code\u003e is\n                    the\n                    size of\n                    the input and \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ec\u003c/code\u003e is some fixed\n                    constant. For example, O(3\u003csup\u003en\u003c/sup\u003e) is a larger/worse\n                    function\n                    than O(2\u003csup\u003en\u003c/sup\u003e), but they both belong to the exponential complexity class. A common indicator\n                    of this\n                    complexity class is recursive code where there is a constant number of recursive calls in each stack\n                    frame.\n                    The\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ec\u003c/code\u003e will be the number of recursive\n                    calls made in each stack frame. Algorithms with this\n                    complexity\n                    are considered quite slow.\n                \u003c/p\u003e\n                \u003ch4 id=\"exponential-growth\"\u003eExponential growth\u003c/h4\u003e\n                \u003cp\u003eBelow are tables showing the growth for O(2\u003csup\u003en\u003c/sup\u003e) and O(3\u003csup\u003en\u003c/sup\u003e). Notice how these grow\n                    large,\n                    quickly.\u003c/p\u003e\n                \u003ctable\u003e\n                    \u003cthead\u003e\n                        \u003ctr class=\"header\"\u003e\n                            \u003cth\u003en\u003c/th\u003e\n                            \u003cth\u003eO(2\u003csup\u003en\u003c/sup\u003e)\u003c/th\u003e\n                        \u003c/tr\u003e\n                    \u003c/thead\u003e\n                    \u003ctbody\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e1\u003c/td\u003e\n                            \u003ctd\u003e~2\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e2\u003c/td\u003e\n                            \u003ctd\u003e~4\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e3\u003c/td\u003e\n                            \u003ctd\u003e~8\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e4\u003c/td\u003e\n                            \u003ctd\u003e~16\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e…\u003c/td\u003e\n                            \u003ctd\u003e…\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e128\u003c/td\u003e\n                            \u003ctd\u003e~3.4028 * 10\u003csup\u003e38\u003c/sup\u003e\u003c/td\u003e\n                        \u003c/tr\u003e\n                    \u003c/tbody\u003e\n                \u003c/table\u003e\n                \u003ctable\u003e\n                    \u003cthead\u003e\n                        \u003ctr class=\"header\"\u003e\n                            \u003cth\u003en\u003c/th\u003e\n                            \u003cth\u003eO(3\u003csup\u003en\u003c/sup\u003e)\u003c/th\u003e\n                        \u003c/tr\u003e\n                    \u003c/thead\u003e\n                    \u003ctbody\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e1\u003c/td\u003e\n                            \u003ctd\u003e~3\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e2\u003c/td\u003e\n                            \u003ctd\u003e~9\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e3\u003c/td\u003e\n                            \u003ctd\u003e~27\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e3\u003c/td\u003e\n                            \u003ctd\u003e~81\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e…\u003c/td\u003e\n                            \u003ctd\u003e…\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e128\u003c/td\u003e\n                            \u003ctd\u003e~1.1790 * 10\u003csup\u003e61\u003c/sup\u003e\u003c/td\u003e\n                        \u003c/tr\u003e\n                    \u003c/tbody\u003e\n                \u003c/table\u003e\n                \u003ch4 id=\"exponential-code-example\"\u003eExponential code example\u003c/h4\u003e\n                \u003cp\u003eBelow are examples of two functions with exponential runtimes.\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb6\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\" class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode javascript\"\u003e\u003ca class=\"sourceLine\" id=\"cb6-1\" title=\"1\"\u003e\u003cspan class=\"co\"\u003e// O(2^n)\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb6-2\" title=\"2\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003eexponential2n\u003c/span\u003e(n) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb6-3\" title=\"3\"\u003e  \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (n \u003cspan class=\"op\"\u003e===\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e) \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb6-4\" title=\"4\"\u003e  \u003cspan class=\"at\"\u003eexponential_2n\u003c/span\u003e(n \u003cspan class=\"op\"\u003e-\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb6-5\" title=\"5\"\u003e  \u003cspan class=\"at\"\u003eexponential_2n\u003c/span\u003e(n \u003cspan class=\"op\"\u003e-\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb6-6\" title=\"6\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb6-7\" title=\"7\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb6-8\" title=\"8\"\u003e\u003cspan class=\"co\"\u003e// O(3^n)\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb6-9\" title=\"9\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003eexponential3n\u003c/span\u003e(n) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb6-10\" title=\"10\"\u003e  \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (n \u003cspan class=\"op\"\u003e===\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e) \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb6-11\" title=\"11\"\u003e  \u003cspan class=\"at\"\u003eexponential_3n\u003c/span\u003e(n \u003cspan class=\"op\"\u003e-\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb6-12\" title=\"12\"\u003e  \u003cspan class=\"at\"\u003eexponential_3n\u003c/span\u003e(n \u003cspan class=\"op\"\u003e-\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb6-13\" title=\"13\"\u003e  \u003cspan class=\"at\"\u003eexponential_3n\u003c/span\u003e(n \u003cspan class=\"op\"\u003e-\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb6-14\" title=\"14\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eThe \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eexponential2n\u003c/code\u003e function has\n                    O(2\u003csup\u003en\u003c/sup\u003e) runtime because each call will make two\n            ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbgoonz%2Frecursion-practice-website","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbgoonz%2Frecursion-practice-website","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbgoonz%2Frecursion-practice-website/lists"}