{"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","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                    more\n                    recursive calls. The \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eexponential3n\u003c/code\u003e\n                    function has O(3\u003csup\u003en\u003c/sup\u003e) runtime because each\n                    call will\n                    make three more recursive calls.\u003c/p\u003e\n                \u003ch3 id=\"on---factorial\"\u003eO(n!) - Factorial\u003c/h3\u003e\n                \u003cp\u003eRecall that \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003en! = (n) * (n - 1) * (n - 2) *\n                        ... * 1\u003c/code\u003e. This complexity is typically the\n                    largest/worst\n                    that we will end up implementing. An indicator of this complexity class is recursive code that has a\n                    variable\n                    number of recursive calls in each stack frame. Note that \u003cem\u003efactorial\u003c/em\u003e is worse than\n                    \u003cem\u003eexponential\u003c/em\u003e\n                    because \u003cem\u003efactorial\u003c/em\u003e algorithms have a \u003cem\u003evariable\u003c/em\u003e amount of recursive calls in each\n                    stack\n                    frame,\n                    whereas \u003cem\u003eexponential\u003c/em\u003e algorithms have a \u003cem\u003econstant\u003c/em\u003e amount of recursive calls in each\n                    frame.\n                \u003c/p\u003e\n                \u003ch4 id=\"factorial-growth\"\u003eFactorial growth\u003c/h4\u003e\n                \u003cp\u003eBelow is a table showing the growth for O(n!). Notice how this has a more aggressive growth than\n                    exponential\n                    behavior.\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~6\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd\u003e4\u003c/td\u003e\n                            \u003ctd\u003e~24\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.8562 * 10\u003csup\u003e215\u003c/sup\u003e\u003c/td\u003e\n                        \u003c/tr\u003e\n                    \u003c/tbody\u003e\n                \u003c/table\u003e\n                \u003ch4 id=\"factorial-code-example\"\u003eFactorial code example\u003c/h4\u003e\n                \u003cp\u003eBelow is an example of a function with factorial runtime.\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb7\"\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=\"cb7-1\" title=\"1\"\u003e\u003cspan class=\"co\"\u003e// O(n!)\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb7-2\" title=\"2\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003efactorial\u003c/span\u003e(n) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb7-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=\"cb7-4\" title=\"4\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb7-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=\"cb7-6\" title=\"6\"\u003e    \u003cspan class=\"at\"\u003efactorial\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=\"cb7-7\" title=\"7\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb7-8\" title=\"8\"\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\"\u003efactorial\u003c/code\u003e function has O(n!)\n                    runtime because the code is \u003cem\u003erecursive\u003c/em\u003e but the\n                    number\n                    of\n                    recursive calls made in a single stack frame depends on the input. This contrasts with an\n                    \u003cem\u003eexponential\u003c/em\u003e\n                    function because exponential functions have a \u003cem\u003efixed\u003c/em\u003e number of calls in each stack frame.\n                \u003c/p\u003e\n                \u003cp\u003eYou may it difficult to identify the complexity class of a given code snippet, especially if the code\n                    falls\n                    into\n                    the loglinear, exponential, or factorial classes. In the upcoming videos, we'll explain the analysis\n                    of\n                    these\n                    functions in greater detail. For now, you should focus on the \u003cem\u003erelative order\u003c/em\u003e of these seven\n                    complexity\n                    classes!\u003c/p\u003e\n                \u003ch2 id=\"what-youve-learned-1\"\u003eRECAP\u003c/h2\u003e\n                \u003cp\u003eIn this reading, we listed the seven common complexity classes and saw some example code for each. In\n                    order\n                    of\n                    ascending growth, the seven classes are:\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eConstant\u003c/li\u003e\n                    \u003cli\u003eLogarithmic\u003c/li\u003e\n                    \u003cli\u003eLinear\u003c/li\u003e\n                    \u003cli\u003eLoglinear\u003c/li\u003e\n                    \u003cli\u003ePolynomial\u003c/li\u003e\n                    \u003cli\u003eExponential\u003c/li\u003e\n                    \u003cli\u003eFactorial\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003chr /\u003e\n                \u003chr\u003e\n                \n                \n                \n                \n                  \u003ch2 id=\"linear-recursion\"\u003eSelf-Similarity\u003c/h2\u003e\n                  \u003cblockquote\u003e\n                      \u003cp\u003eRecursion is the root of computation since it trades description for time.—Alan Perlis, \u003ca\n                              href=\"http://www.cs.yale.edu/homes/perlis-alan/quotes.html\"\u003eEpigrams in Programming\u003c/a\u003e\n                      \u003c/p\u003e\n                  \u003c/blockquote\u003e\n                  \u003cp\u003eIn \u003ca href=\"#arraysanddestructuring\"\u003eArrays and Destructuring Arguments\u003c/a\u003e, we worked with the\n                      basic idea that\n                      putting an array together with a literal array expression was the reverse or opposite of taking it\n                      apart with a\n                      destructuring assignment.\u003c/p\u003e\n                  \u003cp\u003eWe saw that the basic idea that putting an array together with a literal array expression was the\n                      reverse or\n                      opposite of taking it apart with a destructuring assignment.\u003c/p\u003e\n                  \u003cp\u003eLet's be more specific. Some data structures, like lists, can obviously be seen as a collection of\n                      items. Some\n                      are empty, some have three items, some forty-two, some contain numbers, some contain strings, some\n                      a mixture of\n                      elements, there are all kinds of lists.\u003c/p\u003e\n                  \u003cp\u003eBut we can also define a list by describing a rule for building lists. One of the simplest, and\n                      longest-standing\n                      in computer science, is to say that a list is:\u003c/p\u003e\n                  \u003col start=\"0\" type=\"1\"\u003e\n                      \u003cli\u003eEmpty, or;\u003c/li\u003e\n                      \u003cli\u003eConsists of an element concatenated with a list .\u003c/li\u003e\n                  \u003c/ol\u003e\n                  \u003cp\u003eLet's convert our rules to array literals. The first rule is simple: \u003ccode\n                          class=\"language-javascript\"\u003e[]\u003c/code\u003e\n                      is a list. How about the\n                      second rule? We can express that using a spread. Given an element \u003ccode\n                          class=\"language-javascript\"\u003ee\u003c/code\u003e and\n                      a list \u003ccode class=\"language-javascript\"\u003elist\u003c/code\u003e ,\n                      \u003ccode class=\"language-javascript\"\u003e[e, ...list]\u003c/code\u003e is a list. We can test this manually by\n                      building up a\n                      list:\n                  \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[]\n                      //=\u0026gt; []\n\n                      [\u0026quot;baz\u0026quot;, ...[]]\n                      //=\u0026gt; [\u0026quot;baz\u0026quot;]\n\n                      [\u0026quot;bar\u0026quot;, ...[\u0026quot;baz\u0026quot;]]\n                      //=\u0026gt; [\u0026quot;bar\u0026quot;,\u0026quot;baz\u0026quot;]\n\n                      [\u0026quot;foo\u0026quot;, ...[\u0026quot;bar\u0026quot;, \u0026quot;baz\u0026quot;]]\n                      //=\u0026gt; [\u0026quot;foo\u0026quot;,\u0026quot;bar\u0026quot;,\u0026quot;baz\u0026quot;]\u003c/code\u003e\u003c/pre\u003e\n                  \u003cp\u003eThanks to the parallel between array literals + spreads with destructuring + rests, we can also use\n                      the same\n                      rules to decompose lists:\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\"\u003econst [first, ...rest] = [];\n                      first\n                      //=\u0026gt; undefined\n                      rest\n                      //=\u0026gt; []:\n\n                      const [first, ...rest] = [\u0026quot;foo\u0026quot;];\n                      first\n                      //=\u0026gt; \u0026quot;foo\u0026quot;\n                      rest\n                      //=\u0026gt; []\n\n                      const [first, ...rest] = [\u0026quot;foo\u0026quot;, \u0026quot;bar\u0026quot;];\n                      first\n                      //=\u0026gt; \u0026quot;foo\u0026quot;\n                      rest\n                      //=\u0026gt; [\u0026quot;bar\u0026quot;]\n\n                      const [first, ...rest] = [\u0026quot;foo\u0026quot;, \u0026quot;bar\u0026quot;, \u0026quot;baz\u0026quot;];\n                      first\n                      //=\u0026gt; \u0026quot;foo\u0026quot;\n                      rest\n                      //=\u0026gt; [\u0026quot;bar\u0026quot;,\u0026quot;baz\u0026quot;]\u003c/code\u003e\u003c/pre\u003e\n                  \u003cp\u003eFor the purpose of this exploration, we will presume the following:\u003ca href=\"#fn1\"\n                          class=\"footnote-ref\" id=\"fnref1\"\u003e\u003csup\u003e1\u003c/sup\u003e\u003c/a\u003e\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\"\u003econst isEmpty = ([first, ...rest]) =\u0026gt; first === undefined;\n\n                      isEmpty([])\n                      //=\u0026gt; true\n\n                      isEmpty([0])\n                      //=\u0026gt; false\n\n                      isEmpty([[]])\n                      //=\u0026gt; false\u003c/code\u003e\u003c/pre\u003e\n                  \u003cp\u003eArmed with our definition of an empty list and with what we've already learned, we can build a\n                      great many\n                      functions that operate on arrays. We know that we can get the length of an array using its \u003ccode\n                          class=\"language-javascript\"\u003e.length\u003c/code\u003e\n                      . But as an exercise, how would we write a \u003ccode class=\"language-javascript\"\u003elength\u003c/code\u003e\n                      function using just\n                      what we have already?\u003c/p\u003e\n                  \u003cp\u003eFirst, we pick what we call a \u003cem\u003eterminal case\u003c/em\u003e. What is the length of an empty array? \u003ccode\n                          class=\"language-javascript\"\u003e0\u003c/code\u003e . So\n                      let's start our function with the observation that if an array is empty, the length is \u003ccode\n                          class=\"language-javascript\"\u003e0\u003c/code\u003e :\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\"\u003econst length = ([first, ...rest]) =\u0026gt;\n                      first === undefined\n                      ? 0\n                      : // ???\u003c/code\u003e\u003c/pre\u003e\n                  \u003cp\u003eWe need something for when the array isn't empty. If an array is not empty, and we break it into\n                      two pieces,\n                      \u003ccode class=\"language-javascript\"\u003efirst\u003c/code\u003e and \u003ccode class=\"language-javascript\"\u003erest\u003c/code\u003e ,\n                      the length of\n                      our array is going to be \u003ccode class=\"language-javascript\"\u003elength(first) +\n                          length(rest)\u003c/code\u003e . Well, the length of \u003ccode class=\"language-javascript\"\u003efirst\u003c/code\u003e is\n                      \u003ccode class=\"language-javascript\"\u003e1\u003c/code\u003e , there's just one element at\n                      the front. But we don't know the length of \u003ccode class=\"language-javascript\"\u003erest\u003c/code\u003e . If only\n                      there was a\n                      function we could call… Like\n                      \u003ccode class=\"language-javascript\"\u003elength\u003c/code\u003e !\n                  \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\"\u003econst length = ([first, ...rest]) =\u0026gt;\n                      first === undefined\n                      ? 0\n                      : 1 + length(rest); \u003c/code\u003e\u003c/pre\u003e\n                  \u003cp\u003eLet's try it!\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\"\u003elength([])\n                      //=\u0026gt; 0\n\n\n                      length([\u0026quot;foo\u0026quot;])\n                      //=\u0026gt; 1\n\n\n                      length([\u0026quot;foo\u0026quot;, \u0026quot;bar\u0026quot;, \u0026quot;baz\u0026quot;])\n                      //=\u0026gt; 3\u003c/code\u003e\u003c/pre\u003e\n                  \u003cp\u003eOur \u003ccode class=\"language-javascript\"\u003elength\u003c/code\u003e function is \u003cem\u003erecursive\u003c/em\u003e, it calls\n                      itself. This makes\n                      sense because our definition\n                      of a list is recursive, and if a list is self-similar, it is natural to create an algorithm that\n                      is also\n                      self-similar.\u003c/p\u003e\n                  \u003ch3 id=\"linear-recursion-1\"\u003elinear recursion\u003c/h3\u003e\n                  \u003cp\u003e\"Recursion\" sometimes seems like an elaborate party trick. There's even a joke about this:\u003c/p\u003e\n                  \u003cblockquote\u003e\n                      \u003cp\u003eWhen promising students are trying to choose between pure mathematics and applied engineering,\n                          they are given\n                          a two-part aptitude test. In the first part, they are led to a laboratory bench and told to\n                          follow the\n                          instructions printed on the card. They find a bunsen burner, a sparker, a tap, an empty\n                          beaker, a stand, and\n                          a card with the instructions \"boil water.\"\u003c/p\u003e\n                  \u003c/blockquote\u003e\n                  \u003cblockquote\u003e\n                      \u003cp\u003eOf course, all the students know what to do: They fill the beaker with water, place the stand\n                          on the burner\n                          and the beaker on the stand, then they turn the burner on and use the sparker to ignite the\n                          flame. After a\n                          bit the water boils, and they turn off the burner and are lead to a second bench.\u003c/p\u003e\n                  \u003c/blockquote\u003e\n                  \u003cblockquote\u003e\n                      \u003cp\u003eOnce again, there is a card that reads, \"boil water.\" But this time, the beaker is on the stand\n                          over the\n                          burner, as left behind by the previous student. The engineers light the burner immediately.\n                          Whereas the\n                          mathematicians take the beaker off the stand and empty it, thus reducing the situation to a\n                          problem they\n                          have already solved.\u003c/p\u003e\n                  \u003c/blockquote\u003e\n                  \u003cp\u003eThere is more to recursive solutions that simply functions that invoke themselves. Recursive\n                      algorithms follow\n                      the \"divide and conquer\" strategy for solving a problem:\u003c/p\u003e\n                  \u003col start=\"0\" type=\"1\"\u003e\n                      \u003cli\u003eDivide the problem into smaller problems\u003c/li\u003e\n                      \u003cli\u003eIf a smaller problem is solvable, solve the small problem\u003c/li\u003e\n                      \u003cli\u003eIf a smaller problem is not solvable, divide and conquer that problem\u003c/li\u003e\n                      \u003cli\u003eWhen all small problems have been solved, compose the solutions into one big solution\u003c/li\u003e\n                  \u003c/ol\u003e\n                  \u003cp\u003eThe big elements of divide and conquer are a method for decomposing a problem into smaller\n                      problems, a test for\n                      the smallest possible problem, and a means of putting the pieces back together. Our solutions are\n                      a little\n                      simpler in that we don't really break a problem down into multiple pieces, we break a piece off\n                      the problem that\n                      may or may not be solvable, and solve that before sticking it onto a solution for the rest of the\n                      problem.\u003c/p\u003e\n                  \u003cp\u003eThis simpler form of \"divide and conquer\" is called \u003cem\u003elinear recursion\u003c/em\u003e. It's very useful and\n                      simple to\n                      understand. Let's take another example. Sometimes we want to \u003cem\u003eflatten\u003c/em\u003e an array, that is,\n                      an array of\n                      arrays needs to be turned into one array of elements that aren't arrays.\u003ca href=\"#fn2\"\n                          class=\"footnote-ref\" id=\"fnref2\"\u003e\u003csup\u003e2\u003c/sup\u003e\u003c/a\u003e\u003c/p\u003e\n                  \u003cp\u003eWe already know how to divide arrays into smaller pieces. How do we decide whether a smaller\n                      problem is solvable?\n                      We need a test for the terminal case. Happily, there is something along these lines provided for\n                      us:\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\"\u003eArray.isArray(\u0026quot;foo\u0026quot;)\n                      //=\u0026gt; false\n\n                      Array.isArray([\u0026quot;foo\u0026quot;])\n                      //=\u0026gt; true\n                  \u003c/code\u003e\u003c/pre\u003e\n                  \u003cp\u003eThe usual \"terminal case\" will be that flattening an empty array will produce an empty array. The\n                      next terminal\n                      case is that if an element isn't an array, we don't flatten it, and can put it together with the\n                      rest of our\n                      solution directly. Whereas if an element is an array, we'll flatten it and put it together with\n                      the rest of our\n                      solution.\u003c/p\u003e\n                  \u003cp\u003eSo our first cut at a \u003ccode class=\"language-javascript\"\u003eflatten\u003c/code\u003e function will look like\n                      this:\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\"\u003econst flatten = ([first, ...rest]) =\u0026gt; {\n                      if (first === undefined) {\n                      return [];\n                      }\n                      else if (! Array.isArray(first)) {\n                      return [first, ...flatten(rest)];\n                      }\n                      else {\n                      return [...flatten(first), ...flatten(rest)];\n                      }\n                      }\n\n\n\n                      flatten([\u0026quot;foo\u0026quot;, [3, 4, []]])\n                      //=\u0026gt; [\u0026quot;foo\u0026quot;, 3, 4]\n                  \u003c/code\u003e\u003c/pre\u003e\n                  \u003cp\u003eOnce again, the solution directly displays the important elements: Dividing a problem into\n                      subproblems, detecting\n                      terminal cases, solving the terminal cases, and composing a solution from the solved portions.\u003c/p\u003e\n                  \u003ch3 id=\"mapping\"\u003emapping\u003c/h3\u003e\n                  \u003cp\u003eAnother common problem is applying a function to every element of an array. JavaScript has a\n                      built-in function\n                      for this, but let's write our own using linear recursion.\u003c/p\u003e\n                  \u003cp\u003eIf we want to square each number in a list, we could write:\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\"\u003econst squareAll = ([first, ...rest]) =\u0026gt; first === undefined\n                      ? []\n                      : [first * first, ...squareAll(rest)];\n\n                      squareAll([1, 2, 3, 4, 5])\n                      //=\u0026gt; [1,4,9,16,25]\u003c/code\u003e\u003c/pre\u003e\n                  \u003cp\u003eAnd if we wanted to \"truthify\" each element in a list, we could write:\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\"\u003econst truthyAll = ([first, ...rest]) =\u0026gt; first === undefined\n                      ? []\n                      : [!!first, ...truthyAll(rest)];\n\n                      truthyAll([null, true, 25, false, \u0026quot;foo\u0026quot;])\n                      //=\u0026gt; [false,true,true,false,true]\n                  \u003c/code\u003e\u003c/pre\u003e\n                  \u003cp\u003eThis specific case of linear recursion is called \"mapping,\" and it is not necessary to constantly\n                      write out the\n                      same pattern again and again. Functions can take functions as arguments, so let's \"extract\" the\n                      thing to do to\n                      each element and separate it from the business of taking an array apart, doing the thing, and\n                      putting the array\n                      back together.\u003c/p\u003e\n                  \u003cp\u003eGiven the signature:\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\"\u003econst mapWith = (fn, array) =\u0026gt; // ...\u003c/code\u003e\u003c/pre\u003e\n                  \u003cp\u003eWe can write it out using a ternary operator. Even in this small function, we can identify the\n                      terminal\n                      condition, the piece being broken off, and recomposing the solution.\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\"\u003econst mapWith = (fn, [first, ...rest]) =\u0026gt;\n                      first === undefined\n                      ? []\n                      : [fn(first), ...mapWith(fn, rest)];\n\n                      mapWith((x) =\u0026gt; x * x, [1, 2, 3, 4, 5])\n                      //=\u0026gt; [1,4,9,16,25]\n\n                      mapWith((x) =\u0026gt; !!x, [null, true, 25, false, \u0026quot;foo\u0026quot;])\n                      //=\u0026gt; [false,true,true,false,true]\u003c/code\u003e\u003c/pre\u003e\n                  \u003ch3 id=\"folding\"\u003efolding\u003c/h3\u003e\n                  \u003cp\u003eWith the exception of the \u003ccode class=\"language-javascript\"\u003elength\u003c/code\u003e example at the beginning,\n                      our examples\n                      so far all involve\n                      rebuilding a solution using spreads. But they needn't. A function to compute the sum of the\n                      squares of a list of\n                      numbers might look like this:\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\"\u003econst sumSquares = ([first, ...rest]) =\u0026gt; first === undefined\n                      ? 0\n                      : first * first + sumSquares(rest);\n\n                      sumSquares([1, 2, 3, 4, 5])\n                      //=\u0026gt; 55\u003c/code\u003e\u003c/pre\u003e\n                  \u003cp\u003eThere are two differences between \u003ccode class=\"language-javascript\"\u003esumSquares\u003c/code\u003e and our maps\n                      above:\u003c/p\u003e\n                  \u003col start=\"0\" type=\"1\"\u003e\n                      \u003cli\u003eGiven the terminal case of an empty list, we return a \u003ccode\n                              class=\"language-javascript\"\u003e0\u003c/code\u003e instead of\n                          an empty list, and;\u003c/li\u003e\n                      \u003cli\u003eWe catenate the square of each element to the result of applying \u003ccode\n                              class=\"language-javascript\"\u003esumSquares\u003c/code\u003e to the rest of the\n                          elements.\u003c/li\u003e\n                  \u003c/ol\u003e\n                  \u003cp\u003eLet's rewrite \u003ccode class=\"language-javascript\"\u003emapWith\u003c/code\u003e so that we can use it to sum\n                      squares.\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\"\u003econst foldWith = (fn, terminalValue, [first, ...rest]) =\u0026gt;\n                      first === undefined\n                      ? terminalValue\n                      : fn(first, foldWith(fn, terminalValue, rest));\n                  \u003c/code\u003e\u003c/pre\u003e\n                  \u003cp\u003eAnd now we supply a function that does slightly more than our mapping functions:\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\"\u003efoldWith((number, rest) =\u0026gt; number * number + rest, 0, [1, 2, 3,\n                      4, 5])\n                      //=\u0026gt; 55\u003c/code\u003e\u003c/pre\u003e\n                  \u003cp\u003eOur \u003ccode class=\"language-javascript\"\u003efoldWith\u003c/code\u003e function is a generalization of our \u003ccode\n                          class=\"language-javascript\"\u003emapWith\u003c/code\u003e function. We can represent a\n                      map as a fold, we just need to supply the array rebuilding code:\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\"\u003econst squareAll = (array) =\u0026gt; foldWith((first, rest) =\u0026gt; [first\n                      * first,\n                      ...rest], [], array);\n\n\n\n                      squareAll([1, 2, 3, 4, 5])\n                      //=\u0026gt; [1, 4, 9, 16, 25]\u003c/code\u003e\u003c/pre\u003e\n                  \u003cp\u003eAnd if we like, we can write \u003ccode class=\"language-javascript\"\u003emapWith\u003c/code\u003e using \u003ccode\n                          class=\"language-javascript\"\u003efoldWith\u003c/code\u003e :\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\"\u003econst mapWith = (fn, array) =\u0026gt; foldWith((first, rest) =\u0026gt;\n                      [fn(first),\n                      ...rest], [], array),\n                      squareAll = (array) =\u0026gt; mapWith((x) =\u0026gt; x * x, array);\n\n\n\n                      squareAll([1, 2, 3, 4, 5])\n                      //=\u0026gt; [1, 4, 9, 16, 25]\n                  \u003c/code\u003e\u003c/pre\u003e\n                  \u003cp\u003eAnd to return to our first example, our version of \u003ccode class=\"language-javascript\"\u003elength\u003c/code\u003e\n                      can be written\n                      as a fold:\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\"\u003econst length = (array) =\u0026gt; foldWith((first, rest) =\u0026gt; 1 + rest,\n                      0, array);\n\n\n\n                      length([1, 2, 3, 4, 5])\n                      //=\u0026gt; 5\n                  \u003c/code\u003e\u003c/pre\u003e\n                  \u003ch3 id=\"summary\"\u003esummary\u003c/h3\u003e\n                  \u003cp\u003eLinear recursion is a basic building block of algorithms. Its basic form parallels the way linear\n                      data structures\n                      like lists are constructed: This helps make it understandable. Its specialized cases of mapping\n                      and folding are\n                      especially useful and can be used to build other functions. And finally, while folding is a\n                      special case of\n                      linear recursion, mapping is a special case of folding.\u003c/p\u003e\n                  \u003csection class=\"footnotes\"\u003e\n                      \u003chr /\u003e\n                      \u003col\u003e\n                          \u003cli id=\"fn1\"\u003e\n                              \u003cp\u003eWell, actually, this does not work for arrays that contain \u003ccode\n                                      class=\"language-javascript\"\u003eundefined\u003c/code\u003e as a value, but we\n                                  are not going to see that in our examples. A more robust implementation would be \u003ccode\n                                      class=\"language-javascript\"\u003e(array) =\u0026gt;\n                                      array.length === 0\u003c/code\u003e , but we are doing backflips to keep this within a very\n                                  small and\n                                  contrived playground.\u003ca href=\"#fnref1\" class=\"footnote-back\"\u003e↩\u003c/a\u003e\u003c/p\u003e\n                          \u003c/li\u003e\n                          \u003cli id=\"fn2\"\u003e\n                              \u003cp\u003e\u003ccode class=\"language-javascript\"\u003eflatten\u003c/code\u003e is a very simple \u003ca\n                                      href=\"https://en.wikipedia.org/wiki/Anamorphism\"\u003eunfold\u003c/a\u003e,\n                                  a function that takes a seed value and turns it into an array. Unfolds can be thought\n                                  of a \"path\"\n                                  through a data structure, and flattening a tree is equivalent to a depth-first\n                                  traverse.\u003ca href=\"#fnref2\" class=\"footnote-back\"\u003e↩\u003c/a\u003e\u003c/p\u003e\n                          \u003c/li\u003e\n                      \u003c/ol\u003e\n                  \u003c/section\u003e\n                \n                \n                \n                \n                \n                \n                \u003chr\u003e\n                \n                \u003ch1 id=\"memoization\"\u003eMemoization\u003c/h1\u003e\n                \u003cp\u003e\u003cstrong\u003eMemoization\u003c/strong\u003e is a design pattern used to reduce the overall number of calculations\n                    that can\n                    occur\n                    in algorithms that use recursive strategies to solve.\u003c/p\u003e\n                \u003cp\u003eRecall that recursion solves a large problem by dividing it into smaller sub-problems that are more\n                    manageable.\n                    Memoization will store the results of the sub-problems in some other data structure, meaning that\n                    you avoid\n                    duplicate calculations and only \"solve\" each subproblem once. There are two features that comprise\n                    memoization:\n                \u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003ethe function is recursive\u003c/li\u003e\n                    \u003cli\u003ethe additional data structure used is typically an object (we refer to this as the memo!)\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003cp\u003eThis is a trade-off between the time it takes to run an algorithm (without memoization) and the\n                    memory used\n                    to\n                    run the algorithm (with memoization). Usually memoization is a good trade-off when dealing with\n                    large data\n                    or\n                    calculations.\u003c/p\u003e\n                \u003cp\u003eYou cannot always apply this technique to recursive problems. The problem must have an \"overlapping\n                    subproblem\n                    structure\" for memoization to be effective.\u003c/p\u003e\n                \u003cp\u003eHere's an example of a problem that has such a structure:\u003c/p\u003e\n                \u003cblockquote\u003e\n                    \u003cp\u003eUsing pennies, nickels, dimes, and quarters, what is the smallest combination of coins that total\n                        27\n                        cents?\n                    \u003c/p\u003e\n                \u003c/blockquote\u003e\n                \u003cp\u003eYou'll explore this exact problem in depth later on. For now, here is some food for thought. Along\n                    the way to\n                    calculating the smallest coin combination of 27 cents, you should also calculate the smallest coin\n                    combination\n                    of say, 25 cents as a component of that problem. This is the essence of an overlapping subproblem\n                    structure.\n                \u003c/p\u003e\n                \u003ch2 id=\"memoizing-factorial\"\u003eMemoizing factorial\u003c/h2\u003e\n                \u003cp\u003eHere's an example of a function that computes the factorial of the number passed into it.\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb8\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\n                        class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode javascript\"\u003e\u003ca class=\"sourceLine\" id=\"cb8-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003efactorial\u003c/span\u003e(n) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-2\" title=\"2\"\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=\"dv\"\u003e1\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-3\" title=\"3\"\u003e  \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e n \u003cspan class=\"op\"\u003e*\u003c/span\u003e \u003cspan class=\"at\"\u003efactorial\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=\"cb8-4\" title=\"4\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-5\" title=\"5\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-6\" title=\"6\"\u003e\u003cspan class=\"at\"\u003efactorial\u003c/span\u003e(\u003cspan class=\"dv\"\u003e6\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e       \u003cspan class=\"co\"\u003e// =\u0026gt; 720, requires 6 calls\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-7\" title=\"7\"\u003e\u003cspan class=\"at\"\u003efactorial\u003c/span\u003e(\u003cspan class=\"dv\"\u003e6\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e       \u003cspan class=\"co\"\u003e// =\u0026gt; 720, requires 6 calls\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-8\" title=\"8\"\u003e\u003cspan class=\"at\"\u003efactorial\u003c/span\u003e(\u003cspan class=\"dv\"\u003e5\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e       \u003cspan class=\"co\"\u003e// =\u0026gt; 120, requires 5 calls\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-9\" title=\"9\"\u003e\u003cspan class=\"at\"\u003efactorial\u003c/span\u003e(\u003cspan class=\"dv\"\u003e7\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e       \u003cspan class=\"co\"\u003e// =\u0026gt; 5040, requires 7 calls\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eFrom this plain \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003efactorial\u003c/code\u003e above, it\n                    is clear that every time you call\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003efactorial(6)\u003c/code\u003e\n                    you\n                    should get the same result of \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e720\u003c/code\u003e\n                    each time. The code is somewhat inefficient because\n                    you must\n                    go\n                    down the full recursive stack for each top level call to \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003efactorial(6)\u003c/code\u003e. It would be\n                    great if you\n                    could store the result of \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003efactorial(6)\u003c/code\u003e the first time you calculate it, then on\n                    subsequent\n                    calls to\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003efactorial(6)\u003c/code\u003e you simply fetch the\n                    stored result in constant time. You can accomplish\n                    exactly\n                    this\n                    by memoizing with an object!\n                \u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb9\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\n                        class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode javascript\"\u003e\u003ca class=\"sourceLine\" id=\"cb9-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003elet\u003c/span\u003e memo \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"op\"\u003e{}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-2\" title=\"2\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-3\" title=\"3\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003efactorial\u003c/span\u003e(n) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-4\" title=\"4\"\u003e  \u003cspan class=\"co\"\u003e// if this function has calculated factorial(n) previously,\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-5\" title=\"5\"\u003e  \u003cspan class=\"co\"\u003e// fetch the stored result in memo\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-6\" title=\"6\"\u003e  \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (n \u003cspan class=\"kw\"\u003ein\u003c/span\u003e memo) \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e memo[n]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-7\" title=\"7\"\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=\"dv\"\u003e1\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-8\" title=\"8\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-9\" title=\"9\"\u003e  \u003cspan class=\"co\"\u003e// otherwise, it havs not calculated factorial(n) previously,\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-10\" title=\"10\"\u003e  \u003cspan class=\"co\"\u003e// so calculate it now, but store the result in case it is\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-11\" title=\"11\"\u003e  \u003cspan class=\"co\"\u003e// needed again in the future\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-12\" title=\"12\"\u003e  memo[n] \u003cspan class=\"op\"\u003e=\u003c/span\u003e n \u003cspan class=\"op\"\u003e*\u003c/span\u003e \u003cspan class=\"at\"\u003efactorial\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=\"cb9-13\" title=\"13\"\u003e  \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e memo[n]\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-14\" title=\"14\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-15\" title=\"15\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-16\" title=\"16\"\u003e\u003cspan class=\"at\"\u003efactorial\u003c/span\u003e(\u003cspan class=\"dv\"\u003e6\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e       \u003cspan class=\"co\"\u003e// =\u0026gt; 720, requires 6 calls\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-17\" title=\"17\"\u003e\u003cspan class=\"at\"\u003efactorial\u003c/span\u003e(\u003cspan class=\"dv\"\u003e6\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e       \u003cspan class=\"co\"\u003e// =\u0026gt; 720, requires 1 call\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-18\" title=\"18\"\u003e\u003cspan class=\"at\"\u003efactorial\u003c/span\u003e(\u003cspan class=\"dv\"\u003e5\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e       \u003cspan class=\"co\"\u003e// =\u0026gt; 120, requires 1 call\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-19\" title=\"19\"\u003e\u003cspan class=\"at\"\u003efactorial\u003c/span\u003e(\u003cspan class=\"dv\"\u003e7\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e       \u003cspan class=\"co\"\u003e// =\u0026gt; 5040, requires 2 calls\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-20\" title=\"20\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb9-21\" title=\"21\"\u003ememo\u003cspan class=\"op\"\u003e;\u003c/span\u003e   \u003cspan class=\"co\"\u003e// =\u0026gt; { \u0026#39;2\u0026#39;: 2, \u0026#39;3\u0026#39;: 6, \u0026#39;4\u0026#39;: 24, \u0026#39;5\u0026#39;: 120, \u0026#39;6\u0026#39;: 720, \u0026#39;7\u0026#39;: 5040 }\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\"\u003ememo\u003c/code\u003e object above will map an\n                    argument of \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003efactorial\u003c/code\u003e to its return\n                    value. That\n                    is,\n                    the keys will be arguments and their values will be the corresponding results returned. By using the\n                    memo,\n                    you\n                    are able to avoid duplicate recursive calls!\u003c/p\u003e\n                \u003cp\u003eHere's some food for thought: By the time your first call to \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003efactorial(6)\u003c/code\u003e returns, you\n                    will not\n                    have\n                    just the argument \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e6\u003c/code\u003e stored in the\n                    memo. Rather, you will have \u003cem\u003eall\u003c/em\u003e arguments 2\n                    to 6\n                    stored\n                    in the memo.\u003c/p\u003e\n                \u003cp\u003eHopefully you sense the efficiency you can get by memoizing your functions, but maybe you are not\n                    convinced\n                    by\n                    the last example for two reasons:\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003eYou didn't improve the speed of the algorithm by an order of Big-O (it is still O(n)).\u003c/li\u003e\n                    \u003cli\u003eThe code uses some global variable, so it's kind of ugly.\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003cp\u003eBoth of those points are true, so take a look at a more advanced example that benefits from\n                    memoization.\u003c/p\u003e\n                \u003ch2 id=\"memoizing-the-fibonacci-generator\"\u003eMemoizing the Fibonacci generator\u003c/h2\u003e\n                \u003cp\u003eHere's a \u003cem\u003enaive\u003c/em\u003e implementation of a function that calculates the Fibonacci number for a given\n                    input.\n                \u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb10\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\n                        class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode javascript\"\u003e\u003ca class=\"sourceLine\" id=\"cb10-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003efib\u003c/span\u003e(n) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-2\" title=\"2\"\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=\"op\"\u003e||\u003c/span\u003e n \u003cspan class=\"op\"\u003e===\u003c/span\u003e \u003cspan class=\"dv\"\u003e2\u003c/span\u003e) \u003cspan class=\"cf\"\u003ereturn\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=\"cb10-3\" title=\"3\"\u003e  \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"at\"\u003efib\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 \u003cspan class=\"at\"\u003efib\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\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-4\" title=\"4\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-5\" title=\"5\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-6\" title=\"6\"\u003e\u003cspan class=\"at\"\u003efib\u003c/span\u003e(\u003cspan class=\"dv\"\u003e6\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e     \u003cspan class=\"co\"\u003e// =\u0026gt; 8\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eBefore you optimize this, ask yourself what complexity class it falls into in the first place.\u003c/p\u003e\n                \u003cp\u003eThe time complexity of this function is not super intuitive to describe because the code branches\n                    twice\n                    recursively. Fret not! You'll find it useful to visualize the calls needed to do this with a tree.\n                    When\n                    reasoning about the time complexity for recursive functions, draw a tree that helps you see the\n                    calls. Every\n                    node of the tree represents a call of the recursion:\u003c/p\u003e\n                \u003cfigure\u003e\n                    \u003cimg src=\"images/fib_tree.png\" alt=\"fib_tree\" /\u003e\n                    \u003cfigcaption\u003efib_tree\u003c/figcaption\u003e\n                \u003c/figure\u003e\n                \u003cp\u003eIn general, the height of this tree will be \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003en\u003c/code\u003e. You derive this by following the path\n                    going\n                    straight\n                    down the left side of the tree. You can also see that each internal node leads to two more nodes.\n                    Overall,\n                    this\n                    means that the tree will have roughly 2\u003csup\u003en\u003c/sup\u003e nodes which is the same as saying that the\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003efib\u003c/code\u003e\n                    function has an exponential time complexity of 2\u003csup\u003en\u003c/sup\u003e. That is very slow! See for yourself,\n                    try\n                    running\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003efib(50)\u003c/code\u003e - you'll be waiting for\n                    quite a while (it took 3 minutes on the author's\n                    machine).\n                \u003c/p\u003e\n                \u003cp\u003eOkay. So the \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003efib\u003c/code\u003e function is slow. Is\n                    there anyway to speed it up? Take a look at the\n                    tree\n                    above.\n                    Can you find any repetitive regions of the tree?\u003c/p\u003e\n                \u003cfigure\u003e\n                    \u003cimg src=\"images/fib_tree_duplicates.png\" alt=\"fib_tree_duplicates\" /\u003e\n                    \u003cfigcaption\u003efib_tree_duplicates\u003c/figcaption\u003e\n                \u003c/figure\u003e\n                \u003cp\u003eAs the \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003en\u003c/code\u003e grows bigger, the number of\n                    duplicate sub-trees grows exponentially. Luckily\n                    you can\n                    fix\n                    this using memoization by using a similar object strategy as before. You can use some JavaScript\n                    default\n                    arguments to clean things up:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb11\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\n                        class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode javascript\"\u003e\u003ca class=\"sourceLine\" id=\"cb11-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003efastFib\u003c/span\u003e(n\u003cspan class=\"op\"\u003e,\u003c/span\u003e memo \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"op\"\u003e{}\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb11-2\" title=\"2\"\u003e  \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (n \u003cspan class=\"kw\"\u003ein\u003c/span\u003e memo) \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e memo[n]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb11-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=\"op\"\u003e||\u003c/span\u003e n \u003cspan class=\"op\"\u003e===\u003c/span\u003e \u003cspan class=\"dv\"\u003e2\u003c/span\u003e) \u003cspan class=\"cf\"\u003ereturn\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=\"cb11-4\" title=\"4\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb11-5\" title=\"5\"\u003e  memo[n] \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"at\"\u003efastFib\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 memo) \u003cspan class=\"op\"\u003e+\u003c/span\u003e \u003cspan class=\"at\"\u003efastFib\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 memo)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb11-6\" title=\"6\"\u003e  \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e memo[n]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb11-7\" title=\"7\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb11-8\" title=\"8\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb11-9\" title=\"9\"\u003e\u003cspan class=\"at\"\u003efastFib\u003c/span\u003e(\u003cspan class=\"dv\"\u003e6\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e     \u003cspan class=\"co\"\u003e// =\u0026gt; 8\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb11-10\" title=\"10\"\u003e\u003cspan class=\"at\"\u003efastFib\u003c/span\u003e(\u003cspan class=\"dv\"\u003e50\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e    \u003cspan class=\"co\"\u003e// =\u0026gt; 12586269025\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eThe code above can calculate the 50th Fibonacci number almost instantly! Thanks to the\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ememo\u003c/code\u003e\n                    object,\n                    you only need to explore a subtree fully once. Visually, the \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003efastFib\u003c/code\u003e recursion has this\n                    structure:\n                \u003c/p\u003e\n                \u003cfigure\u003e\n                    \u003cimg src=\"images/fib_memoized.png\" alt=\"fib_memoized\" /\u003e\n                    \u003cfigcaption\u003efib_memoized\u003c/figcaption\u003e\n                \u003c/figure\u003e\n                \u003cp\u003eYou can see the marked nodes (function calls) that access the memo in green. It's easy to see that\n                    this\n                    version\n                    of the Fibonacci generator will do far less computations as \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003en\u003c/code\u003e grows larger! In fact,\n                    this\n                    memoization has brought the time complexity down to linear \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003eO(n)\u003c/code\u003e time because the tree\n                    only\n                    branches\n                    on the left side. This is an enormous gain if you recall the complexity class hierarchy.\u003c/p\u003e\n                \u003ch2 id=\"the-memoization-formula\"\u003eThe memoization formula\u003c/h2\u003e\n                \u003cp\u003eNow that you understand memoization, when should you apply it? Memoization is useful when attacking\n                    recursive\n                    problems that have many overlapping sub-problems. You'll find it most useful to draw out the visual\n                    tree\n                    first.\n                    If you notice duplicate sub-trees, time to memoize. Here are the hard and fast rules you can use to\n                    memoize\n                    a\n                    slow function:\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eWrite the unoptimized, brute force recursion and make sure it works.\u003c/li\u003e\n                    \u003cli\u003eAdd the memo object as an additional argument to the function. The keys will represent unique\n                        arguments\n                        to\n                        the function, and their values will represent the results for those arguments.\u003c/li\u003e\n                    \u003cli\u003eAdd a base case condition to the function that returns the stored value if the function's\n                        argument is in\n                        the\n                        memo.\u003c/li\u003e\n                    \u003cli\u003eBefore you return the result of the recursive case, store it in the memo as a value and make the\n                        function's\n                        argument it's key.\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003ch2 id=\"what-you-learned\"\u003eWhat you learned\u003c/h2\u003e\n                \u003cp\u003eYou learned a secret to possibly changing an algorithm of one complexity class to a lower complexity\n                    class by\n                    using memory to store intermediate results. This is a powerful technique to use to make sure your\n                    programs\n                    that\n                    must do recursive calculations can benefit from running much faster.\u003c/p\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"tabulation\"\u003eTabulation\u003c/h1\u003e\n                \u003cp\u003eNow that you are familiar with \u003cem\u003ememoization\u003c/em\u003e, you can explore a related method of algorithmic\n                    optimization: \u003cstrong\u003eTabulation\u003c/strong\u003e. There are two main features that comprise the Tabulation\n                    strategy:\n                \u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003ethe function is iterative and \u003cem\u003enot\u003c/em\u003e recursive\u003c/li\u003e\n                    \u003cli\u003ethe additional data structure used is typically an array, commonly referred to as the table\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003cp\u003eMany problems that can be solved with memoization can also be solved with tabulation as long as you\n                    convert\n                    the\n                    recursion to iteration. The first example is the canonical example of recursion, calculating the\n                    Fibonacci\n                    number for an input. However, in the example, you'll see the iteration version of it for a fresh\n                    start!\u003c/p\u003e\n                \u003ch2 id=\"tabulating-the-fibonacci-number\"\u003eTabulating the Fibonacci number\u003c/h2\u003e\n                \u003cp\u003eTabulation is all about creating a table (array) and filling it out with elements. In general, you\n                    will\n                    complete\n                    the table by filling entries from \"left to right\". This means that the first entry of the table\n                    (first\n                    element\n                    of the array) will correspond to the smallest subproblem. Naturally, the final entry of the table\n                    (last\n                    element\n                    of the array) will correspond to the largest problem, which is also the final answer.\u003c/p\u003e\n                \u003cp\u003eHere's a way to use tabulation to store the intermediary calculations so that later calculations can\n                    refer\n                    back\n                    to the table.\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb12\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\n                        class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode javascript\"\u003e\u003ca class=\"sourceLine\" id=\"cb12-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003etabulatedFib\u003c/span\u003e(n) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb12-2\" title=\"2\"\u003e  \u003cspan class=\"co\"\u003e// create a blank array with n reserved spots\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb12-3\" title=\"3\"\u003e  \u003cspan class=\"kw\"\u003elet\u003c/span\u003e table \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"kw\"\u003enew\u003c/span\u003e \u003cspan class=\"at\"\u003eArray\u003c/span\u003e(n)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb12-4\" title=\"4\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb12-5\" title=\"5\"\u003e  \u003cspan class=\"co\"\u003e// seed the first two values\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb12-6\" title=\"6\"\u003e  table[\u003cspan class=\"dv\"\u003e0\u003c/span\u003e] \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb12-7\" title=\"7\"\u003e  table[\u003cspan class=\"dv\"\u003e1\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=\"cb12-8\" title=\"8\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb12-9\" title=\"9\"\u003e  \u003cspan class=\"co\"\u003e// complete the table by moving from left to right,\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb12-10\" title=\"10\"\u003e  \u003cspan class=\"co\"\u003e// following the fibonacci pattern\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb12-11\" title=\"11\"\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\"\u003e2\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=\"dv\"\u003e1\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb12-12\" title=\"12\"\u003e    table[i] \u003cspan class=\"op\"\u003e=\u003c/span\u003e table[i \u003cspan class=\"op\"\u003e-\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e] \u003cspan class=\"op\"\u003e+\u003c/span\u003e table[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=\"cb12-13\" title=\"13\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb12-14\" title=\"14\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb12-15\" title=\"15\"\u003e  \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e table[n]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb12-16\" title=\"16\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb12-17\" title=\"17\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb12-18\" title=\"18\"\u003e\u003cspan class=\"va\"\u003econsole\u003c/span\u003e.\u003cspan class=\"at\"\u003elog\u003c/span\u003e(\u003cspan class=\"at\"\u003etabulatedFib\u003c/span\u003e(\u003cspan class=\"dv\"\u003e7\u003c/span\u003e))\u003cspan class=\"op\"\u003e;\u003c/span\u003e      \u003cspan class=\"co\"\u003e// =\u0026gt; 13\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eWhen you initialized the table and seeded the first two values, it looked like this:\u003c/p\u003e\n                \u003ctable\u003e\n                    \u003cthead\u003e\n                        \u003ctr class=\"header\"\u003e\n                            \u003cth\u003ei\u003c/th\u003e\n                            \u003cth\u003e0\u003c/th\u003e\n                            \u003cth\u003e1\u003c/th\u003e\n                            \u003cth\u003e2\u003c/th\u003e\n                            \u003cth\u003e3\u003c/th\u003e\n                            \u003cth\u003e4\u003c/th\u003e\n                            \u003cth\u003e5\u003c/th\u003e\n                            \u003cth\u003e6\u003c/th\u003e\n                            \u003cth\u003e7\u003c/th\u003e\n                        \u003c/tr\u003e\n                    \u003c/thead\u003e\n                    \u003ctbody\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003etable[i]\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e0\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e1\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd\u003e\u003c/td\u003e\n                            \u003ctd\u003e\u003c/td\u003e\n                            \u003ctd\u003e\u003c/td\u003e\n                            \u003ctd\u003e\u003c/td\u003e\n                            \u003ctd\u003e\u003c/td\u003e\n                            \u003ctd\u003e\u003c/td\u003e\n                        \u003c/tr\u003e\n                    \u003c/tbody\u003e\n                \u003c/table\u003e\n                \u003cp\u003eAfter the loop finishes, the final table will be:\u003c/p\u003e\n                \u003ctable\u003e\n                    \u003cthead\u003e\n                        \u003ctr class=\"header\"\u003e\n                            \u003cth\u003ei\u003c/th\u003e\n                            \u003cth\u003e0\u003c/th\u003e\n                            \u003cth\u003e1\u003c/th\u003e\n                            \u003cth\u003e2\u003c/th\u003e\n                            \u003cth\u003e3\u003c/th\u003e\n                            \u003cth\u003e4\u003c/th\u003e\n                            \u003cth\u003e5\u003c/th\u003e\n                            \u003cth\u003e6\u003c/th\u003e\n                            \u003cth\u003e7\u003c/th\u003e\n                        \u003c/tr\u003e\n                    \u003c/thead\u003e\n                    \u003ctbody\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003etable[i]\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e0\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e1\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e1\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e2\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e3\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e5\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e8\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e13\u003c/code\u003e\u003c/td\u003e\n                        \u003c/tr\u003e\n                    \u003c/tbody\u003e\n                \u003c/table\u003e\n                \u003cp\u003eSimilar to the previous \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ememo\u003c/code\u003e, by the\n                    time the function completes, the \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003etable\u003c/code\u003e\n                    will\n                    contain the final solution as well as all sub-solutions calculated along the way.\u003c/p\u003e\n                \u003cp\u003eTo compute the complexity class of this \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003etabulatedFib\u003c/code\u003e is very straightforward since the\n                    code is\n                    iterative. The dominant operation in the function is the loop used to fill out the entire table. The\n                    length\n                    of\n                    the table is roughly \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003en\u003c/code\u003e elements\n                    long, so the algorithm will have an \u003cem\u003eO(n)\u003c/em\u003e\n                    runtime. The\n                    space taken by our algorithm is also \u003cem\u003eO(n)\u003c/em\u003e due to the size of the table. Overall, this\n                    should be a\n                    satisfying solution for the efficiency of the algorithm.\u003c/p\u003e\n                \u003ch2 id=\"aside-refactoring-for-o1-space\"\u003eAside: Refactoring for O(1) Space\u003c/h2\u003e\n                \u003cp\u003eYou may notice that you can cut down on the space used by the function. At any point of the loop, the\n                    calculation\n                    really only need the previous two subproblems' results. There is little utility to storing the full\n                    array.\n                    This\n                    refactor is easy to do by using two variables:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb13\"\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=\"cb13-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003efib\u003c/span\u003e(n) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb13-2\" title=\"2\"\u003e  \u003cspan class=\"kw\"\u003elet\u003c/span\u003e mostRecentCalcs \u003cspan class=\"op\"\u003e=\u003c/span\u003e [\u003cspan class=\"dv\"\u003e0\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=\"cb13-3\" title=\"3\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb13-4\" title=\"4\"\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 mostRecentCalcs[\u003cspan class=\"dv\"\u003e0\u003c/span\u003e]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb13-5\" title=\"5\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb13-6\" title=\"6\"\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\"\u003e2\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=\"cb13-7\" title=\"7\"\u003e    \u003cspan class=\"kw\"\u003econst\u003c/span\u003e [ secondLast\u003cspan class=\"op\"\u003e,\u003c/span\u003e last ] \u003cspan class=\"op\"\u003e=\u003c/span\u003e mostRecentCalcs\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb13-8\" title=\"8\"\u003e    mostRecentCalcs \u003cspan class=\"op\"\u003e=\u003c/span\u003e [ last\u003cspan class=\"op\"\u003e,\u003c/span\u003e secondLast \u003cspan class=\"op\"\u003e+\u003c/span\u003e last ]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb13-9\" title=\"9\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb13-10\" title=\"10\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb13-11\" title=\"11\"\u003e  \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e mostRecentCalcs[\u003cspan class=\"dv\"\u003e1\u003c/span\u003e]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb13-12\" title=\"12\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eBam! You now have O(n) runtime and O(1) space. This is the most optimal algorithm for calculating a\n                    Fibonacci\n                    number. Note that this strategy is a pared down form of tabulation, since it uses only the last two\n                    values.\n                \u003c/p\u003e\n                \u003ch3 id=\"the-tabulation-formula\"\u003eThe Tabulation Formula\u003c/h3\u003e\n                \u003cp\u003eHere are the general guidelines for implementing the tabulation strategy. This is just a general\n                    recipe, so\n                    adjust for taste depending on your problem:\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eCreate the table array based off of the size of the input, which isn't always straightforward if\n                        you\n                        have\n                        multiple input values\u003c/li\u003e\n                    \u003cli\u003eInitialize some values in the table that \"answer\" the trivially small subproblem usually by\n                        initializing\n                        the\n                        first entry (or entries) of the table\u003c/li\u003e\n                    \u003cli\u003eIterate through the array and fill in remaining entries, using previous entries in the table to\n                        perform\n                        the\n                        current calculation\u003c/li\u003e\n                    \u003cli\u003eYour final answer is (usually) the last entry in the table\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003ch2 id=\"what-you-learned-1\"\u003eWhat you learned\u003c/h2\u003e\n                \u003cp\u003eYou learned another way of possibly changing an algorithm of one complexity class to a lower\n                    complexity class\n                    by\n                    using memory to store intermediate results. This is a powerful technique to use to make sure your\n                    programs\n                    that\n                    must do iterative calculations can benefit from running much faster.\u003c/p\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"analysis-of-linear-search\"\u003eAnalysis of Linear Search\u003c/h1\u003e\n                \u003cp\u003eConsider the following search algorithm known as \u003cstrong\u003elinear search\u003c/strong\u003e.\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb14\"\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=\"cb14-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003esearch\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e term) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb14-2\" title=\"2\"\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=\"cb14-3\" title=\"3\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (array[i] \u003cspan class=\"op\"\u003e==\u003c/span\u003e term) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb14-4\" title=\"4\"\u003e      \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e i\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb14-5\" title=\"5\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb14-6\" title=\"6\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb14-7\" title=\"7\"\u003e  \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"dv\"\u003e-1\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb14-8\" title=\"8\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eMost Big-O analysis is done on the \"worst-case scenario\" and provides an upper bound. In the worst\n                    case\n                    analysis,\n                    you calculate the upper bound on running time of an algorithm. You must know the case that causes\n                    the\n                    maximum\n                    number of operations to be executed.\u003c/p\u003e\n                \u003cp\u003eFor \u003cem\u003elinear search\u003c/em\u003e, the worst case happens when the element to be searched (\u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003eterm\u003c/code\u003e\n                    in the\n                    above code) is not present in the array. When \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003eterm\u003c/code\u003e is not present, the\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003esearch\u003c/code\u003e\n                    function\n                    compares it with all the elements of \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003earray\u003c/code\u003e one by one. Therefore, the worst-case time\n                    complexity of\n                    linear search would be O(n).\n                \u003c/p\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"analysis-of-binary-search\"\u003eAnalysis of Binary Search\u003c/h1\u003e\n                \u003cp\u003eConsider the following search algorithm known as the \u003cstrong\u003ebinary search\u003c/strong\u003e. This kind of\n                    search only\n                    works if the array is already sorted.\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb15\"\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=\"cb15-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003ebinarySearch\u003c/span\u003e(arr\u003cspan class=\"op\"\u003e,\u003c/span\u003e x\u003cspan class=\"op\"\u003e,\u003c/span\u003e start\u003cspan class=\"op\"\u003e,\u003c/span\u003e end) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb15-2\" title=\"2\"\u003e  \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (start \u003cspan class=\"op\"\u003e\u0026gt;\u003c/span\u003e end) \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"kw\"\u003efalse\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb15-3\" title=\"3\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb15-4\" title=\"4\"\u003e  \u003cspan class=\"kw\"\u003elet\u003c/span\u003e mid \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003eMath\u003c/span\u003e.\u003cspan class=\"at\"\u003efloor\u003c/span\u003e((start \u003cspan class=\"op\"\u003e+\u003c/span\u003e end) / \u003cspan class=\"dv\"\u003e2\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb15-5\" title=\"5\"\u003e  \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (arr[mid] \u003cspan class=\"op\"\u003e===\u003c/span\u003e x) \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"kw\"\u003etrue\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb15-6\" title=\"6\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb15-7\" title=\"7\"\u003e  \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (arr[mid] \u003cspan class=\"op\"\u003e\u0026gt;\u003c/span\u003e x) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb15-8\" title=\"8\"\u003e    \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"at\"\u003ebinarySearch\u003c/span\u003e(arr\u003cspan class=\"op\"\u003e,\u003c/span\u003e x\u003cspan class=\"op\"\u003e,\u003c/span\u003e start\u003cspan class=\"op\"\u003e,\u003c/span\u003e mid \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=\"cb15-9\" title=\"9\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e \u003cspan class=\"cf\"\u003eelse\u003c/span\u003e \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb15-10\" title=\"10\"\u003e    \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"at\"\u003ebinarySearch\u003c/span\u003e(arr\u003cspan class=\"op\"\u003e,\u003c/span\u003e x\u003cspan class=\"op\"\u003e,\u003c/span\u003e mid \u003cspan class=\"op\"\u003e+\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e end)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb15-11\" title=\"11\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb15-12\" title=\"12\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eFor the \u003cem\u003ebinary search\u003c/em\u003e, you cut the search space in half every time. This means that it\n                    reduces the\n                    number of searches you must do by half, every time. That means the number of steps it takes to get\n                    to the\n                    desired item (if it exists in the array), in the worst case takes the same amount of steps for every\n                    number\n                    within a range defined by the powers of 2.\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003e7 -\u0026gt; 4 -\u0026gt; 2 -\u0026gt; 1\u003c/li\u003e\n                    \u003cli\u003e8 -\u0026gt; 4 -\u0026gt; 2 -\u0026gt; 1\u003c/li\u003e\n                    \u003cli\u003e9 -\u0026gt; 5 -\u0026gt; 3 -\u0026gt; 2 -\u0026gt; 1\u003c/li\u003e\n                    \u003cli\u003e15 -\u0026gt; 8 -\u0026gt; 4 -\u0026gt; 2 -\u0026gt; 1\u003c/li\u003e\n                    \u003cli\u003e16 -\u0026gt; 8 -\u0026gt; 4 -\u0026gt; 2 -\u0026gt; 1\u003c/li\u003e\n                    \u003cli\u003e17 -\u0026gt; 9 -\u0026gt; 5 -\u0026gt; 3 -\u0026gt; 2 -\u0026gt; 1\u003c/li\u003e\n                    \u003cli\u003e31 -\u0026gt; 16 -\u0026gt; 8 -\u0026gt; 4 -\u0026gt; 2 -\u0026gt; 1\u003c/li\u003e\n                    \u003cli\u003e32 -\u0026gt; 16 -\u0026gt; 8 -\u0026gt; 4 -\u0026gt; 2 -\u0026gt; 1\u003c/li\u003e\n                    \u003cli\u003e33 -\u0026gt; 17 -\u0026gt; 9 -\u0026gt; 5 -\u0026gt; 3 -\u0026gt; 2 -\u0026gt; 1\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003cp\u003eSo, for any number of items in the sorted array between 2\u003csup\u003en-1\u003c/sup\u003e and 2\u003csup\u003en\u003c/sup\u003e, it takes\n                    \u003cem\u003en\u003c/em\u003e\n                    number of steps. That means if you have \u003cem\u003ek\u003c/em\u003e items in the array, then it will take\n                    \u003ci\u003elog\u003c/i\u003e\u003csub\u003e\u003ci\u003e2\u003c/i\u003e\u003c/sub\u003e\u003ci\u003ek\u003c/i\u003e.\n                \u003c/p\u003e\n                \u003cp\u003eBinary searches are \u003ci\u003eO\u003c/i\u003e(\u003ci\u003elog\u003c/i\u003e\u003csub\u003e\u003ci\u003e2\u003c/i\u003e\u003c/sub\u003e\u003ci\u003en\u003c/i\u003e).\u003c/p\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"analysis-of-the-merge-sort\"\u003eAnalysis of the Merge Sort\u003c/h1\u003e\n                \u003cp\u003eConsider the following divide-and-conquer sort method known as the \u003cstrong\u003emerge sort\u003c/strong\u003e.\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb16\"\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=\"cb16-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003emerge\u003c/span\u003e(leftArray\u003cspan class=\"op\"\u003e,\u003c/span\u003e rightArray) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-2\" title=\"2\"\u003e  \u003cspan class=\"kw\"\u003econst\u003c/span\u003e sorted \u003cspan class=\"op\"\u003e=\u003c/span\u003e []\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-3\" title=\"3\"\u003e  \u003cspan class=\"cf\"\u003ewhile\u003c/span\u003e (\u003cspan class=\"va\"\u003eleftArray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e\u0026gt;\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e \u003cspan class=\"op\"\u003e\u0026amp;\u0026amp;\u003c/span\u003e \u003cspan class=\"va\"\u003erightArray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e\u0026gt;\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-4\" title=\"4\"\u003e    \u003cspan class=\"kw\"\u003econst\u003c/span\u003e leftItem \u003cspan class=\"op\"\u003e=\u003c/span\u003e leftArray[\u003cspan class=\"dv\"\u003e0\u003c/span\u003e]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-5\" title=\"5\"\u003e    \u003cspan class=\"kw\"\u003econst\u003c/span\u003e rightItem \u003cspan class=\"op\"\u003e=\u003c/span\u003e rightArray[\u003cspan class=\"dv\"\u003e0\u003c/span\u003e]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-6\" title=\"6\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-7\" title=\"7\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (leftItem \u003cspan class=\"op\"\u003e\u0026gt;\u003c/span\u003e rightItem) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-8\" title=\"8\"\u003e      \u003cspan class=\"va\"\u003esorted\u003c/span\u003e.\u003cspan class=\"at\"\u003epush\u003c/span\u003e(rightItem)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-9\" title=\"9\"\u003e      \u003cspan class=\"va\"\u003erightArray\u003c/span\u003e.\u003cspan class=\"at\"\u003eshift\u003c/span\u003e()\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-10\" title=\"10\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e \u003cspan class=\"cf\"\u003eelse\u003c/span\u003e \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-11\" title=\"11\"\u003e      \u003cspan class=\"va\"\u003esorted\u003c/span\u003e.\u003cspan class=\"at\"\u003epush\u003c/span\u003e(leftItem)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-12\" title=\"12\"\u003e      \u003cspan class=\"va\"\u003eleftArray\u003c/span\u003e.\u003cspan class=\"at\"\u003eshift\u003c/span\u003e()\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-13\" title=\"13\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-14\" title=\"14\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-15\" title=\"15\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-16\" title=\"16\"\u003e  \u003cspan class=\"cf\"\u003ewhile\u003c/span\u003e (\u003cspan class=\"va\"\u003eleftArray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e!==\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-17\" title=\"17\"\u003e    \u003cspan class=\"kw\"\u003econst\u003c/span\u003e value \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003eleftArray\u003c/span\u003e.\u003cspan class=\"at\"\u003eshift\u003c/span\u003e()\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-18\" title=\"18\"\u003e    \u003cspan class=\"va\"\u003esorted\u003c/span\u003e.\u003cspan class=\"at\"\u003epush\u003c/span\u003e(value)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-19\" title=\"19\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-20\" title=\"20\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-21\" title=\"21\"\u003e  \u003cspan class=\"cf\"\u003ewhile\u003c/span\u003e (\u003cspan class=\"va\"\u003erightArray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e!==\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-22\" title=\"22\"\u003e    \u003cspan class=\"kw\"\u003econst\u003c/span\u003e value \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003erightArray\u003c/span\u003e.\u003cspan class=\"at\"\u003eshift\u003c/span\u003e()\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-23\" title=\"23\"\u003e    \u003cspan class=\"va\"\u003esorted\u003c/span\u003e.\u003cspan class=\"at\"\u003epush\u003c/span\u003e(value)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-24\" title=\"24\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-25\" title=\"25\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-26\" title=\"26\"\u003e  \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e sorted\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-27\" title=\"27\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-28\" title=\"28\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-29\" title=\"29\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003emergeSort\u003c/span\u003e(array) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-30\" title=\"30\"\u003e  \u003cspan class=\"kw\"\u003econst\u003c/span\u003e length \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-31\" title=\"31\"\u003e  \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (length \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=\"cb16-32\" title=\"32\"\u003e    \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e array\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-33\" title=\"33\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-34\" title=\"34\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-35\" title=\"35\"\u003e  \u003cspan class=\"kw\"\u003econst\u003c/span\u003e middleIndex \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003eMath\u003c/span\u003e.\u003cspan class=\"at\"\u003eceil\u003c/span\u003e(length / \u003cspan class=\"dv\"\u003e2\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-36\" title=\"36\"\u003e  \u003cspan class=\"kw\"\u003econst\u003c/span\u003e leftArray \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eslice\u003c/span\u003e(\u003cspan class=\"dv\"\u003e0\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e middleIndex)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-37\" title=\"37\"\u003e  \u003cspan class=\"kw\"\u003econst\u003c/span\u003e rightArray \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eslice\u003c/span\u003e(middleIndex\u003cspan class=\"op\"\u003e,\u003c/span\u003e length)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-38\" title=\"38\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-39\" title=\"39\"\u003e  leftArray \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"at\"\u003emergeSort\u003c/span\u003e(leftArray)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-40\" title=\"40\"\u003e  rightArray \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"at\"\u003emergeSort\u003c/span\u003e(rightArray)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-41\" title=\"41\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-42\" title=\"42\"\u003e  \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"at\"\u003emerge\u003c/span\u003e(leftArray\u003cspan class=\"op\"\u003e,\u003c/span\u003e rightArray)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb16-43\" title=\"43\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eFor the \u003cem\u003emerge sort\u003c/em\u003e, you cut the sort space in half every time. In each of those halves, you\n                    have to\n                    loop\n                    through the number of items in the array. That means that, for the worst case, you get that same\n                    \u003ci\u003elog\u003c/i\u003e\u003csub\u003e\u003ci\u003e2\u003c/i\u003e\u003c/sub\u003e\u003ci\u003en\u003c/i\u003e but it must be multiplied by the number of elements in the\n                    array,\n                    \u003cem\u003en\u003c/em\u003e.\n                \u003c/p\u003e\n                \u003cp\u003eMerge sorts are \u003ci\u003eO\u003c/i\u003e(\u003ci\u003en*log\u003c/i\u003e\u003csub\u003e\u003ci\u003e2\u003c/i\u003e\u003c/sub\u003e\u003ci\u003en\u003c/i\u003e).\u003c/p\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"analysis-of-bubble-sort\"\u003eAnalysis of Bubble Sort\u003c/h1\u003e\n                \u003cp\u003eConsider the following sort algorithm known as the \u003cstrong\u003ebubble sort\u003c/strong\u003e.\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb17\"\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=\"cb17-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003ebubbleSort\u003c/span\u003e(items) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb17-2\" title=\"2\"\u003e  \u003cspan class=\"kw\"\u003evar\u003c/span\u003e length \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003eitems\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb17-3\" title=\"3\"\u003e  \u003cspan class=\"cf\"\u003efor\u003c/span\u003e (\u003cspan class=\"kw\"\u003evar\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 length\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=\"cb17-4\" title=\"4\"\u003e    \u003cspan class=\"cf\"\u003efor\u003c/span\u003e (\u003cspan class=\"kw\"\u003evar\u003c/span\u003e j \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e j \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e (length \u003cspan class=\"op\"\u003e-\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 j\u003cspan class=\"op\"\u003e++\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb17-5\" title=\"5\"\u003e      \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (items[j] \u003cspan class=\"op\"\u003e\u0026gt;\u003c/span\u003e items[j \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=\"cb17-6\" title=\"6\"\u003e        \u003cspan class=\"kw\"\u003evar\u003c/span\u003e tmp \u003cspan class=\"op\"\u003e=\u003c/span\u003e items[j]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb17-7\" title=\"7\"\u003e        items[j] \u003cspan class=\"op\"\u003e=\u003c/span\u003e items[j \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=\"cb17-8\" title=\"8\"\u003e        items[j \u003cspan class=\"op\"\u003e+\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e] \u003cspan class=\"op\"\u003e=\u003c/span\u003e tmp\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb17-9\" title=\"9\"\u003e      \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb17-10\" title=\"10\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb17-11\" title=\"11\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb17-12\" title=\"12\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eFor the \u003cem\u003ebubble sort\u003c/em\u003e, the worst case is the same as the best case because it always makes\n                    nested\n                    loops.\n                    So, the outer loop loops the number of times of the items in the array. For each one of those loops,\n                    the\n                    inner\n                    loop loops again a number of times for the items in the array. So, if there are \u003cem\u003en\u003c/em\u003e values in\n                    the\n                    array,\n                    then a loop inside a loop is \u003cem\u003en\u003c/em\u003e * \u003cem\u003en\u003c/em\u003e. So, this is O(n\u003csup\u003e2\u003c/sup\u003e). That's\n                    polynomial, which\n                    ain't that good.\u003c/p\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"leetcode.com\"\u003eLeetCode.com\u003c/h1\u003e\n                \u003cp\u003e use the LeetCode platform to check your work rather than\n                    relying\n                    on local mocha tests. If you don't already have an account at LeetCode.com, please click\n                    https://leetcode.com/accounts/signup/ to sign up for a free account.\u003c/p\u003e\n                \u003cp\u003eAfter you sign up for the account, please verify the account with the email address that you used so\n                    that you\n                    can\n                    actually run your solution on LeetCode.com.\u003c/p\u003e\n                \u003cp\u003eIn the projects, you will see files that are named \"leet_code_«number».js\". When you open those, you\n                    will see\n                    a\n                    link in the file that you can use to go directly to the corresponding problem on LeetCode.com.\u003c/p\u003e\n                \u003cp\u003eUse the local JavaScript file in Visual Studio Code to collaborate on the solution. Then, you can run\n                    the\n                    proposed solution in the LeetCode.com code runner to validate its correctness.\u003c/p\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"memoization-problems\"\u003eMemoization Problems\u003c/h1\u003e\n                \u003cp\u003eThis project contains two test-driven problems and one problem on LeetCode.com.\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003eClone the project from https://github.com/appacademy-starters/algorithms-memoization-project.\n                    \u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ecd\u003c/code\u003e into the project folder\u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003enpm install\u003c/code\u003e to install\n                        dependencies in the project root directory\u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003enpx test\u003c/code\u003e to run the specs\u003c/li\u003e\n                    \u003cli\u003eYou can view the test cases in \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003e/test/test.js\u003c/code\u003e. Your job is to write code in the\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e/lib\u003c/code\u003e files to pass all specs.\n                        \u003cul\u003e\n                            \u003cli\u003eIn \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eproblems.js\u003c/code\u003e, you will\n                                write code to make the\n                                \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003elucasNumberMemo\u003c/code\u003e and\n                                \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eminChange\u003c/code\u003e functions\n                                pass.\n                            \u003c/li\u003e\n                            \u003cli\u003eIn \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eleet_code_518.js\u003c/code\u003e, you\n                                will use that file as a scratch pad to work on the\n                                LeetCode.com problem at https://leetcode.com/problems/coin-change-2/.\u003c/li\u003e\n                        \u003c/ul\u003e\n                    \u003c/li\u003e\n                \u003c/ul\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"tabulation-problems\"\u003eTabulation Problems\u003c/h1\u003e\n                \u003cp\u003eThis project contains two test-driven problems and one problem on LeetCode.com.\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003eClone the project from https://github.com/appacademy-starters/algorithms-tabulation-project.\n                    \u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ecd\u003c/code\u003e into the project folder\u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003enpm install\u003c/code\u003e to install\n                        dependencies in the project root directory\u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003enpx test\u003c/code\u003e to run the specs\u003c/li\u003e\n                    \u003cli\u003eYou can view the test cases in \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003e/test/test.js\u003c/code\u003e. Your job is to write code in the\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e/lib\u003c/code\u003e files to pass all specs.\n                        \u003cul\u003e\n                            \u003cli\u003eIn \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eproblems.js\u003c/code\u003e, you will\n                                write code to make the \u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003estepper\u003c/code\u003e,\n                                \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003emaxNonAdjacentSum\u003c/code\u003e, and\n                                \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eminChange\u003c/code\u003e functions\n                                pass.\n                            \u003c/li\u003e\n                            \u003cli\u003eIn \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eleet_code_64.js\u003c/code\u003e, you\n                                will use that file as a scratch pad to work on the\n                                LeetCode.com\n                                problem at https://leetcode.com/problems/minimum-path-sum/.\u003c/li\u003e\n                            \u003cli\u003eIn \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eleet_code_70.js\u003c/code\u003e, you\n                                will use that file as a scratch pad to work on the\n                                LeetCode.com\n                                problem at https://leetcode.com/problems/climbing-stairs/.\u003c/li\u003e\n                        \u003c/ul\u003e\n                    \u003c/li\u003e\n                \u003c/ul\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"week-07-day-3-sorting-algorithms\" data-ignore=\"true\"\u003eWEEK-07 DAY-3\u003cbr\u003e\u003cem\u003eSorting\n                        Algorithms\u003c/em\u003e\u003c/h1\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"sorting-algorithms-\"\u003eSorting Algorithms \u003c/h1\u003e\n                \u003cp\u003e\u003cstrong\u003eThe objective of this lesson\u003c/strong\u003e is for you to get experience implementing common\n                    sorting\n                    algorithms\n                    that will come up during a lot of interviews. It is also important for you to understand how\n                    different\n                    sorting\n                    algorithms behave when given output.\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\u003eExplain the complexity of and write a function that performs \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003ebubble sort\u003c/code\u003e on an\n                        array of\n                        numbers.\u003c/li\u003e\n                    \u003cli\u003eExplain the complexity of and write a function that performs \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003eselection sort\u003c/code\u003e on an\n                        array of\n                        numbers.\u003c/li\u003e\n                    \u003cli\u003eExplain the complexity of and write a function that performs \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003einsertion sort\u003c/code\u003e on an\n                        array of\n                        numbers.\u003c/li\u003e\n                    \u003cli\u003eExplain the complexity of and write a function that performs \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003emerge sort\u003c/code\u003e on an array\n                        of\n                        numbers.\n                    \u003c/li\u003e\n                    \u003cli\u003eExplain the complexity of and write a function that performs \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003equick sort\u003c/code\u003e on an array\n                        of\n                        numbers.\n                    \u003c/li\u003e\n                    \u003cli\u003eExplain the complexity of and write a function that performs a binary search on a sorted array\n                        of\n                        numbers.nce implementing common sorting algorithms that will come up during a lot of interviews.\n                        It is\n                        also\n                        important for you to understand how different sorting algorithms behave when given output.\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\u003eExplain the complexity of and write a function that performs \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003ebubble sort\u003c/code\u003e on an\n                        array of\n                        numbers.\u003c/li\u003e\n                    \u003cli\u003eExplain the complexity of and write a function that performs \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003eselection sort\u003c/code\u003e on an\n                        array of\n                        numbers.\u003c/li\u003e\n                    \u003cli\u003eExplain the complexity of and write a function that performs \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003einsertion sort\u003c/code\u003e on an\n                        array of\n                        numbers.\u003c/li\u003e\n                    \u003cli\u003eExplain the complexity of and write a function that performs \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003emerge sort\u003c/code\u003e on an array\n                        of\n                        numbers.\n                    \u003c/li\u003e\n                    \u003cli\u003eExplain the complexity of and write a function that performs \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003equick sort\u003c/code\u003e on an array\n                        of\n                        numbers.\n                    \u003c/li\u003e\n                    \u003cli\u003eExplain the complexity of and write a function that performs a binary search on a sorted array\n                        of\n                        numbers.\n                    \u003c/li\u003e\n                \u003c/ol\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"bubble-sort\"\u003eBubble Sort\u003c/h1\u003e\n                \u003cp\u003eBubble Sort is generally the first major sorting algorithm to come up in most introductory\n                    programming\n                    courses.\n                    Learning about this algorithm is useful educationally, as it provides a good introduction to the\n                    challenges\n                    you\n                    face when tasked with converting unsorted data into sorted data, such as conducting logical\n                    comparisons,\n                    making\n                    swaps while iterating, and making optimizations. It's also quite simple to implement, and can be\n                    done\n                    quickly.\n                \u003c/p\u003e\n                \u003cp\u003eBubble Sort is \u003cem\u003ealmost never\u003c/em\u003e a good choice in production. simply because:\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003eIt is not efficient\u003c/li\u003e\n                    \u003cli\u003eIt is not commonly used\u003c/li\u003e\n                    \u003cli\u003eThere is a stigma attached to using it\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003ch2 id=\"butthenwhy-are-we\"\u003e\u003cem\u003e\"But…then…why are we…\"\u003c/em\u003e\u003c/h2\u003e\n                \u003cp\u003eIt is \u003cem\u003equite useful\u003c/em\u003e as an educational base for you, and as a conversational base for you\n                    while\n                    interviewing, because you can discuss how other more elegant and efficient algorithms improve upon\n                    it.\n                    Taking\n                    naive code and improving upon it by weighing the technical tradeoffs of your other options is 100%\n                    the name\n                    of\n                    the game when trying to level yourself up from a junior engineer to a senior engineer.\u003c/p\u003e\n                \u003ch2 id=\"the-algorithm-bubbles-up\"\u003eThe algorithm bubbles up\u003c/h2\u003e\n                \u003cp\u003eAs you progress through the algorithms and data structures of this course, you'll eventually notice\n                    that\n                    there\n                    are some recurring funny terms. \"Bubbling up\" is one of those terms.\u003c/p\u003e\n                \u003cp\u003eWhen someone writes that an item in a collection \"bubbles up,\" you should infer that:\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003eThe item is \u003cem\u003ein motion\u003c/em\u003e\u003c/li\u003e\n                    \u003cli\u003eThe item is moving \u003cem\u003ein some direction\u003c/em\u003e\u003c/li\u003e\n                    \u003cli\u003eThe item \u003cem\u003ehas some final resting destination\u003c/em\u003e\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003cp\u003eWhen invoking Bubble Sort to sort an array of integers in ascending order, the largest integers will\n                    \"bubble\n                    up\"\n                    to the \"top\" (the end) of the array, one at a time.\u003c/p\u003e\n                \u003cp\u003eThe largest values are captured, put into motion in the direction defined by the desired sort\n                    (ascending\n                    right\n                    now), and traverse the array until they arrive at their end destination. See if you can observe this\n                    behavior in\n                    the following animation (courtesy http://visualgo.net):\u003c/p\u003e\n                \u003cfigure\u003e\n                    \u003cimg src=\"images/BubbleSort.gif\" alt=\"bubble sort\" /\u003e\n                    \u003cfigcaption\u003ebubble sort\u003c/figcaption\u003e\n                \u003c/figure\u003e\n                \u003cp\u003eAs the algorithm iterates through the array, it compares each element to the element's right\n                    neighbor. If the\n                    current element is larger than its neighbor, the algorithm swaps them. This continues until all\n                    elements in\n                    the\n                    array are sorted.\u003c/p\u003e\n                \u003ch2 id=\"how-does-a-pass-of-bubble-sort-work\"\u003eHow does a pass of Bubble Sort work?\u003c/h2\u003e\n                \u003cp\u003eBubble sort works by performing multiple \u003cem\u003epasses\u003c/em\u003e to move elements closer to their final\n                    positions. A\n                    single pass will iterate through the entire array once.\u003c/p\u003e\n                \u003cp\u003eA pass works by scanning the array from left to right, two elements at a time, and checking if they\n                    are\n                    ordered\n                    correctly. To be ordered correctly the first element must be less than or equal to the second. If\n                    the two\n                    elements are not ordered properly, then we swap them to correct their order. Afterwards, it scans\n                    the next\n                    two\n                    numbers and continue repeat this process until we have gone through the entire array.\u003c/p\u003e\n                \u003cp\u003eSee one pass of bubble sort on the array \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e[2,\n                        8, 5, 2, 6]\u003c/code\u003e. On each step the elements\n                    currently\n                    being\n                    scanned are in \u003cstrong\u003ebold\u003c/strong\u003e.\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003e[\u003cstrong\u003e2\u003c/strong\u003e, \u003cstrong\u003e8\u003c/strong\u003e, 5, 2, 6] - ordered, so leave them alone\u003c/li\u003e\n                    \u003cli\u003e[2, \u003cstrong\u003e8\u003c/strong\u003e, \u003cstrong\u003e5\u003c/strong\u003e, 2, 6] - not ordered, so swap\u003c/li\u003e\n                    \u003cli\u003e[2, 5, \u003cstrong\u003e8\u003c/strong\u003e, \u003cstrong\u003e2\u003c/strong\u003e, 6] - not ordered, so swap\u003c/li\u003e\n                    \u003cli\u003e[2, 5, 2, \u003cstrong\u003e8\u003c/strong\u003e, \u003cstrong\u003e6\u003c/strong\u003e] - not ordered, so swap\u003c/li\u003e\n                    \u003cli\u003e[2, 5, 2, 6, 8] - the first pass is complete\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003cp\u003eBecause at least one swap occurred, the algorithm knows that it wasn't sorted. It needs to make\n                    another pass.\n                    It\n                    starts over again at the first entry and goes to the next-to-last entry doing the comparisons,\n                    again. It\n                    only\n                    needs to go to the next-to-last entry because the previous \"bubbling\" put the largest entry in the\n                    last\n                    position.\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003e[\u003cstrong\u003e2\u003c/strong\u003e, \u003cstrong\u003e5\u003c/strong\u003e, 2, 6, 8] - ordered, so leave them alone\u003c/li\u003e\n                    \u003cli\u003e[2, \u003cstrong\u003e5\u003c/strong\u003e, \u003cstrong\u003e2\u003c/strong\u003e, 6, 8] - not ordered, so swap\u003c/li\u003e\n                    \u003cli\u003e[2, 2, \u003cstrong\u003e5\u003c/strong\u003e, \u003cstrong\u003e6\u003c/strong\u003e, 8] - ordered, so leave them alone\u003c/li\u003e\n                    \u003cli\u003e[2, 2, 5, 6, 8] - the second pass is complete\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003cp\u003eBecause at least one swap occurred, the algorithm knows that it wasn't sorted. Now, it can bubble\n                    from the\n                    first\n                    position to the last-2 position because the last two values are sorted.\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003e[\u003cstrong\u003e2\u003c/strong\u003e, \u003cstrong\u003e2\u003c/strong\u003e, 5, 6, 8] - ordered, so leave them alone\u003c/li\u003e\n                    \u003cli\u003e[2, \u003cstrong\u003e2\u003c/strong\u003e, \u003cstrong\u003e5\u003c/strong\u003e, 6, 8] - ordered, so leave them alone\u003c/li\u003e\n                    \u003cli\u003e[2, 2, 5, 6, 8] - the third pass is complete\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003cp\u003eNo swap occurred, so the Bubble Sort stops.\u003c/p\u003e\n                \u003ch2 id=\"ending-the-bubble-sort\"\u003eEnding the Bubble Sort\u003c/h2\u003e\n                \u003cp\u003eDuring Bubble Sort, you can tell if the array is in sorted order by checking if a swap was made\n                    during the\n                    previous pass performed. If a swap was not performed during the previous pass, then the array must\n                    be\n                    totally\n                    sorted and the algorithm can stop.\u003c/p\u003e\n                \u003cp\u003eYou're probably wondering why that makes sense. Recall that a pass of Bubble Sort checks if any\n                    adjacent\n                    elements\n                    are \u003cstrong\u003eout of order\u003c/strong\u003e and swaps them if they are. If we don't make any swaps during a\n                    pass, then\n                    everything must be already \u003cstrong\u003ein order\u003c/strong\u003e, so our job is done. Let that marinate for a\n                    bit.\u003c/p\u003e\n                \u003ch2 id=\"pseudocode-for-bubble-sort\"\u003ePseudocode for Bubble Sort\u003c/h2\u003e\n                \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\"\u003eBubble Sort: (array)\n  n := length(array)\n  repeat\n  swapped = false\n  for i := 1 to n - 1 inclusive do\n\n      /* if this pair is out of order */\n      if array[i - 1] \u0026gt; array[i] then\n\n        /* swap them and remember something changed */\n        swap(array, i - 1, i)\n        swapped := true\n\n      end if\n    end for\n  until not swapped\u003c/code\u003e\u003c/pre\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"selection-sort\"\u003eSelection Sort\u003c/h1\u003e\n                \u003cp\u003eSelection Sort is very similar to Bubble Sort. The major difference between the two is that Bubble\n                    Sort\n                    bubbles\n                    the \u003cem\u003elargest\u003c/em\u003e elements up to the end of the array, while Selection Sort selects the\n                    \u003cem\u003esmallest\u003c/em\u003e\n                    elements of the array and directly places them at the beginning of the array in sorted position.\n                    Selection\n                    sort\n                    will utilize swapping just as bubble sort did. Let's carefully break this sorting algorithm down.\n                \u003c/p\u003e\n                \u003ch2 id=\"the-algorithm-select-the-next-smallest\"\u003eThe algorithm: select the next smallest\u003c/h2\u003e\n                \u003cp\u003eSelection sort works by maintaining a sorted region on the left side of the input array; this sorted\n                    region\n                    will\n                    grow by one element with every \"pass\" of the algorithm. A single \"pass\" of selection sort will\n                    select the\n                    next\n                    smallest element of unsorted region of the array and move it to the sorted region. Because a single\n                    pass of\n                    selection sort will move an element of the unsorted region into the sorted region, this means a\n                    single pass\n                    will\n                    shrink the unsorted region by 1 element whilst increasing the sorted region by 1 element. Selection\n                    sort is\n                    complete when the sorted region spans the entire array and the unsorted region is empty!\u003c/p\u003e\n                \u003cfigure\u003e\n                    \u003cimg src=\"images/SelectionSort.gif\" alt=\"selection sort\" /\u003e\n                    \u003cfigcaption\u003eselection sort\u003c/figcaption\u003e\n                \u003c/figure\u003e\n                \u003cp\u003eThe algorithm can be summarized as the following:\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eSet MIN to location 0\u003c/li\u003e\n                    \u003cli\u003eSearch the minimum element in the list\u003c/li\u003e\n                    \u003cli\u003eSwap with value at location MIN\u003c/li\u003e\n                    \u003cli\u003eIncrement MIN to point to next element\u003c/li\u003e\n                    \u003cli\u003eRepeat until list is sorted\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003ch2 id=\"the-pseudocode\"\u003eThe pseudocode\u003c/h2\u003e\n                \u003cp\u003eIn pseudocode, the Selection Sort can be written as this.\u003c/p\u003e\n                \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\"\u003eprocedure selection sort\n   list  : array of items\n   n     : size of list\n\n   for i = 1 to n - 1\n   /* set current element as minimum*/\n      min = i\n\n      /* check the element to be minimum */\n\n      for j = i+1 to n\n         if list[j] \u0026lt; list[min] then\n            min = j;\n         end if\n      end for\n\n      /* swap the minimum element with the current element*/\n      if indexMin != i  then\n         swap list[min] and list[i]\n      end if\n   end for\nend procedure\u003c/code\u003e\u003c/pre\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"insertion-sort\"\u003eInsertion Sort\u003c/h1\u003e\n                \u003cp\u003eWith Bubble Sort and Selection Sort now in your tool box, you're starting to get some experience\n                    points under\n                    your belt! Time to learn one more \"naive\" sorting algorithm before you get to the efficient sorting\n                    algorithms.\n                \u003c/p\u003e\n                \u003ch2 id=\"the-algorithm-insert-into-the-sorted-region\"\u003eThe algorithm: insert into the sorted region\u003c/h2\u003e\n                \u003cp\u003eInsertion Sort is similar to Selection Sort in that it gradually builds up a larger and larger sorted\n                    region\n                    at\n                    the left-most end of the array.\u003c/p\u003e\n                \u003cp\u003eHowever, Insertion Sort differs from Selection Sort because this algorithm does not focus on\n                    searching for\n                    the\n                    right element to place (the next smallest in our Selection Sort) on each pass through the array.\n                    Instead, it\n                    focuses on sorting each element in the order they appear from left to right, regardless of their\n                    value, and\n                    inserting them in the most appropriate position in the sorted region.\u003c/p\u003e\n                \u003cp\u003eSee if you can observe the behavior described above in the following animation:\u003c/p\u003e\n                \u003cfigure\u003e\n                    \u003cimg src=\"images/InsertionSort.gif\" alt=\"insertion sort\" /\u003e\n                    \u003cfigcaption\u003einsertion sort\u003c/figcaption\u003e\n                \u003c/figure\u003e\n                \u003ch2 id=\"the-steps\"\u003eThe Steps\u003c/h2\u003e\n                \u003cp\u003eInsertion Sort grows a sorted array on the left side of the input array by:\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eIf it is the first element, it is already sorted. return 1;\u003c/li\u003e\n                    \u003cli\u003ePick next element\u003c/li\u003e\n                    \u003cli\u003eCompare with all elements in the sorted sub-list\u003c/li\u003e\n                    \u003cli\u003eShift all the elements in the sorted sub-list that is greater than the value to be sorted\u003c/li\u003e\n                    \u003cli\u003eInsert the value\u003c/li\u003e\n                    \u003cli\u003eRepeat until list is sorted\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003cp\u003eThese steps are easy to confuse with selection sort, so you'll want to watch the video lecture and\n                    drawing\n                    that\n                    accompanies this reading as always!\u003c/p\u003e\n                \u003ch2 id=\"the-pseudocode-1\"\u003eThe pseudocode\u003c/h2\u003e\n                \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\"\u003eprocedure insertionSort( A : array of items )\n   int holePosition\n   int valueToInsert\n\n   for i = 1 to length(A) inclusive do:\n\n      /* select value to be inserted */\n      valueToInsert = A[i]\n      holePosition = i\n\n      /*locate hole position for the element to be inserted */\n\n      while holePosition \u0026gt; 0 and A[holePosition-1] \u0026gt; valueToInsert do:\n         A[holePosition] = A[holePosition-1]\n         holePosition = holePosition -1\n      end while\n\n      /* insert the number at hole position */\n      A[holePosition] = valueToInsert\n\n   end for\n\nend procedure\u003c/code\u003e\u003c/pre\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"merge-sort\"\u003eMerge Sort\u003c/h1\u003e\n                \u003cp\u003eYou've explored a few sorting algorithms already, all of them being quite slow with a runtime of\n                    O(n\u003csup\u003e2\u003c/sup\u003e). It's time to level up and learn your first time-efficient sorting algorithm!\n                    You'll\n                    explore\n                    \u003cstrong\u003emerge sort\u003c/strong\u003e in detail soon, but first, you should jot down some key ideas for now.\n                    The\n                    following\n                    points are not steps to an algorithm yet; rather, they are ideas that will motivate how you can\n                    derive this\n                    algorithm.\n                \u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003eit is easy to merge elements of two sorted arrays into a single sorted array\u003c/li\u003e\n                    \u003cli\u003eyou can consider an array containing only a single element as already trivially sorted\u003c/li\u003e\n                    \u003cli\u003eyou can also consider an empty array as trivially sorted\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003ch2 id=\"the-algorithm-divide-and-conquer\"\u003eThe algorithm: divide and conquer\u003c/h2\u003e\n                \u003cp\u003eYou're going to need a helper function that solves the first major point from above. How might you\n                    merge two\n                    sorted arrays? In other words you want a \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003emerge\u003c/code\u003e function that will behave like so:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb21\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\n                        class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode javascript\"\u003e\u003ca class=\"sourceLine\" id=\"cb21-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003elet\u003c/span\u003e arr1 \u003cspan class=\"op\"\u003e=\u003c/span\u003e [\u003cspan class=\"dv\"\u003e1\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e5\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e10\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e15\u003c/span\u003e]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb21-2\" title=\"2\"\u003e\u003cspan class=\"kw\"\u003elet\u003c/span\u003e arr2 \u003cspan class=\"op\"\u003e=\u003c/span\u003e [\u003cspan class=\"dv\"\u003e0\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e2\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e3\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e7\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e10\u003c/span\u003e]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb21-3\" title=\"3\"\u003e\u003cspan class=\"at\"\u003emerge\u003c/span\u003e(arr1\u003cspan class=\"op\"\u003e,\u003c/span\u003e arr2)\u003cspan class=\"op\"\u003e;\u003c/span\u003e \u003cspan class=\"co\"\u003e// =\u0026gt; [0, 1, 2, 3, 5, 7, 10, 10, 15]\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eOnce you have that, you get to the \"divide and conquer\" bit.\u003c/p\u003e\n                \u003cp\u003eThe algorithm for merge sort is actually \u003cem\u003ereally\u003c/em\u003e simple.\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eif there is only one element in the list, it is already sorted. return that array.\u003c/li\u003e\n                    \u003cli\u003eotherwise, divide the list recursively into two halves until it can no more be divided.\u003c/li\u003e\n                    \u003cli\u003emerge the smaller lists into new list in sorted order.\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003cp\u003eThe process is visualized below. When elements are moved to the bottom of the picture, they are going\n                    through\n                    the\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003emerge\u003c/code\u003e step:\n                \u003c/p\u003e\n                \u003cfigure\u003e\n                    \u003cimg src=\"images/MergeSort.gif\" alt=\"merge sort\" /\u003e\n                    \u003cfigcaption\u003emerge sort\u003c/figcaption\u003e\n                \u003c/figure\u003e\n                \u003cp\u003eThe pseudocode for the algorithm is as follows.\u003c/p\u003e\n                \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\"\u003eprocedure mergesort( a as array )\n   if ( n == 1 ) return a\n\n   /* Split the array into two */\n   var l1 as array = a[0] ... a[n/2]\n   var l2 as array = a[n/2+1] ... a[n]\n\n   l1 = mergesort( l1 )\n   l2 = mergesort( l2 )\n\n   return merge( l1, l2 )\nend procedure\n\nprocedure merge( a as array, b as array )\n   var result as array\n   while ( a and b have elements )\n      if ( a[0] \u0026gt; b[0] )\n         add b[0] to the end of result\n         remove b[0] from b\n      else\n         add a[0] to the end of result\n         remove a[0] from a\n      end if\n   end while\n\n   while ( a has elements )\n      add a[0] to the end of result\n      remove a[0] from a\n   end while\n\n   while ( b has elements )\n      add b[0] to the end of result\n      remove b[0] from b\n   end while\n\n   return result\nend procedure\u003c/code\u003e\u003c/pre\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"quick-sort\"\u003eQuick Sort\u003c/h1\u003e\n                \u003cp\u003eQuick Sort has a similar \"divide and conquer\" strategy to Merge Sort. Here are a few key ideas that\n                    will\n                    motivate\n                    the design:\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003eit is easy to sort elements of an array relative to a particular target value\u003c/li\u003e\n                    \u003cli\u003ean array of 0 or 1 elements is already trivially sorted\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003cp\u003eRegarding that first point, for example given \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003e[7, 3, 8, 9, 2]\u003c/code\u003e and a target of\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e5\u003c/code\u003e, we\n                    know \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e[3, 2]\u003c/code\u003e are numbers less than\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e5\u003c/code\u003e and \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003e[7, 8, 9]\u003c/code\u003e are numbers\n                    greater\n                    than \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e5\u003c/code\u003e.\n                \u003c/p\u003e\n                \u003ch2 id=\"how-does-it-work\"\u003eHow does it work?\u003c/h2\u003e\n                \u003cp\u003eIn general, the strategy is to divide the input array into two subarrays: one with the smaller\n                    elements, and\n                    one\n                    with the larger elements. Then, it recursively operates on the two new subarrays. It continues this\n                    process\n                    until of dividing into smaller arrays until it reaches subarrays of length 1 or smaller. As you have\n                    seen\n                    with\n                    Merge Sort, arrays of such length are automatically sorted.\u003c/p\u003e\n                \u003cp\u003eThe steps, when discussed on a high level, are simple:\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003echoose an element called \"the pivot\", how that's done is up to the implementation\u003c/li\u003e\n                    \u003cli\u003etake two variables to point left and right of the list excluding pivot\u003c/li\u003e\n                    \u003cli\u003eleft points to the low index\u003c/li\u003e\n                    \u003cli\u003eright points to the high\u003c/li\u003e\n                    \u003cli\u003ewhile value at left is less than pivot move right\u003c/li\u003e\n                    \u003cli\u003ewhile value at right is greater than pivot move left\u003c/li\u003e\n                    \u003cli\u003eif both step 5 and step 6 does not match swap left and right\u003c/li\u003e\n                    \u003cli\u003eif left ≥ right, the point where they met is new pivot\u003c/li\u003e\n                    \u003cli\u003erepeat, recursively calling this for smaller and smaller arrays\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003cp\u003eBefore we move forward, see if you can observe the behavior described above in the following\n                    animation:\u003c/p\u003e\n                \u003cfigure\u003e\n                    \u003cimg src=\"images/QuickSort.gif\" alt=\"quick sort\" /\u003e\n                    \u003cfigcaption\u003equick sort\u003c/figcaption\u003e\n                \u003c/figure\u003e\n                \u003ch2 id=\"the-algorithm-divide-and-conquer-1\"\u003eThe algorithm: divide and conquer\u003c/h2\u003e\n                \u003cp\u003eFormally, we want to partition elements of an array relative to a pivot value. That is, we want\n                    elements less\n                    than the pivot to be separated from elements that are greater than or equal to the pivot. Our goal\n                    is to\n                    create\n                    a function with this behavior:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb23\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\n                        class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode javascript\"\u003e\u003ca class=\"sourceLine\" id=\"cb23-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003elet\u003c/span\u003e arr \u003cspan class=\"op\"\u003e=\u003c/span\u003e [\u003cspan class=\"dv\"\u003e7\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e3\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e8\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e9\u003c/span\u003e\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=\"cb23-2\" title=\"2\"\u003e\u003cspan class=\"at\"\u003epartition\u003c/span\u003e(arr\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e5\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e  \u003cspan class=\"co\"\u003e// =\u0026gt; [[3, 2], [7,8,9]]\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003ch3 id=\"partition\"\u003ePartition\u003c/h3\u003e\n                \u003cp\u003eSeems simple enough! Let's implement it in JavaScript:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb24\"\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=\"cb24-1\" title=\"1\"\u003e\u003cspan class=\"co\"\u003e// nothing fancy\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-2\" title=\"2\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003epartition\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e pivot) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-3\" title=\"3\"\u003e  \u003cspan class=\"kw\"\u003elet\u003c/span\u003e left \u003cspan class=\"op\"\u003e=\u003c/span\u003e []\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-4\" title=\"4\"\u003e  \u003cspan class=\"kw\"\u003elet\u003c/span\u003e right \u003cspan class=\"op\"\u003e=\u003c/span\u003e []\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-5\" title=\"5\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-6\" title=\"6\"\u003e  \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eforEach\u003c/span\u003e(el \u003cspan class=\"kw\"\u003e=\u0026gt;\u003c/span\u003e \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-7\" title=\"7\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (el \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e pivot) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-8\" title=\"8\"\u003e      \u003cspan class=\"va\"\u003eleft\u003c/span\u003e.\u003cspan class=\"at\"\u003epush\u003c/span\u003e(el)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-9\" title=\"9\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e \u003cspan class=\"cf\"\u003eelse\u003c/span\u003e \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-10\" title=\"10\"\u003e      \u003cspan class=\"va\"\u003eright\u003c/span\u003e.\u003cspan class=\"at\"\u003epush\u003c/span\u003e(el)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-11\" title=\"11\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-12\" title=\"12\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-13\" title=\"13\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-14\" title=\"14\"\u003e  \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e [ left\u003cspan class=\"op\"\u003e,\u003c/span\u003e right ]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-15\" title=\"15\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-16\" title=\"16\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-17\" title=\"17\"\u003e\u003cspan class=\"co\"\u003e// if you fancy\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-18\" title=\"18\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003epartition\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e pivot) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-19\" title=\"19\"\u003e  \u003cspan class=\"kw\"\u003elet\u003c/span\u003e left \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003efilter\u003c/span\u003e(el \u003cspan class=\"kw\"\u003e=\u0026gt;\u003c/span\u003e el \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e pivot)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-20\" title=\"20\"\u003e  \u003cspan class=\"kw\"\u003elet\u003c/span\u003e right \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003efilter\u003c/span\u003e(el \u003cspan class=\"kw\"\u003e=\u0026gt;\u003c/span\u003e el \u003cspan class=\"op\"\u003e\u0026gt;=\u003c/span\u003e pivot)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-21\" title=\"21\"\u003e  \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e [ left\u003cspan class=\"op\"\u003e,\u003c/span\u003e right ]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb24-22\" title=\"22\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eYou don't have to use an explicit \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003epartition\u003c/code\u003e helper function in your Quick Sort\n                    implementation;\n                    however, we will borrow heavily from this pattern. As you design algorithms, it helps to think about\n                    key\n                    patterns in isolation, although your solution may not feature that exact helper. Some would say we\n                    like to\n                    divide and conquer.\u003c/p\u003e\n                \u003ch2 id=\"the-pseudocode-2\"\u003eThe pseudocode\u003c/h2\u003e\n                \u003cp\u003eIt is \u003cem\u003eso\u003c/em\u003e small, this algorithm. It's amazing that it performs so well with so little code!\n                \u003c/p\u003e\n                \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\"\u003eprocedure quickSort(left, right)\n\n  if the length of the array is 0 or 1, return the array\n\n  set the pivot to the first element of the array\n  remove the first element of the array\n\n  put all values less than the pivot value into an array called left\n  put all values greater than the pivot value into an array called right\n\n  call quick sort on left and assign the return value to leftSorted\n  call quick sort on right and assign the return value to rightSorted\n\n  return the concatenation of leftSorted, the pivot value, and rightSorted\n\nend procedure\u003c/code\u003e\u003c/pre\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"binary-search\"\u003eBinary Search\u003c/h1\u003e\n                \u003cp\u003eWe've explored many ways to sort arrays so far, but why did we go through all of that trouble? By\n                    sorting\n                    elements of an array, we are organizing the data in a way that gives us a quick way to look up\n                    elements\n                    later\n                    on. For simplicity, we have been using arrays of numbers up until this point. However, these sorting\n                    concepts\n                    can be generalized to other data types. For example, it would be easy to modify our comparison-based\n                    sorting\n                    algorithms to sort strings: instead of leveraging facts like \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003e0 \u0026lt; 1\u003c/code\u003e, we can say\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e'A'\n                        \u0026lt;\n                        'B'\u003c/code\u003e.\n                \u003c/p\u003e\n                \u003cp\u003eThink of a dictionary. A dictionary contains alphabetically sorted words and their definitions. A\n                    dictionary\n                    is\n                    pretty much only useful if it is ordered in this way. Let's say you wanted to look up the definition\n                    of\n                    \"stupendous.\" What steps might you take?\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003eyou open up the dictionary at the roughly middle page\n                        \u003cul\u003e\n                            \u003cli\u003eyou land in the \"m\" section\u003c/li\u003e\n                        \u003c/ul\u003e\n                    \u003c/li\u003e\n                    \u003cli\u003eyou know \"s\" comes somewhere after \"m\" in the book, so you disregard all pages before the \"m\"\n                        section.\n                        Instead, you flip to the roughly middle page between \"m\" and \"z\"\n                        \u003cul\u003e\n                            \u003cli\u003eyou land in the \"u\" section\u003c/li\u003e\n                        \u003c/ul\u003e\n                    \u003c/li\u003e\n                    \u003cli\u003eyou know \"s\" comes somewhere before \"u\", so you can disregard all pages after the \"u\" section.\n                        Instead,\n                        you\n                        flip to the roughly middle page between the previous \"m\" page and \"u\"\u003c/li\u003e\n                    \u003cli\u003e…\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003cp\u003eYou are essentially using the \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003ebinarySearch\u003c/code\u003e algorithm in the real world.\u003c/p\u003e\n                \u003ch2 id=\"the-algorithm-check-the-middle-and-half-the-search-space\"\u003eThe Algorithm: \"check the middle and\n                    half the\n                    search space\"\u003c/h2\u003e\n                \u003cp\u003eFormally, our \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ebinarySearch\u003c/code\u003e will seek\n                    to solve the following problem:\u003c/p\u003e\n                \u003cpre data-filter-output=\"(out)\"\n                    class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\"\u003eGiven a sorted array of numbers and a target num, return a boolean indicating whether or not that target is contained in the array.\u003c/code\u003e\u003c/pre\u003e\n                \u003cp\u003eProgrammatically, we want to satisfy the following behavior:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb27\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\n                        class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode javascript\"\u003e\u003ca class=\"sourceLine\" id=\"cb27-1\" title=\"1\"\u003e\u003cspan class=\"at\"\u003ebinarySearch\u003c/span\u003e([\u003cspan class=\"dv\"\u003e5\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e10\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e12\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e15\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e20\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e30\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e70\u003c/span\u003e]\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e12\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e  \u003cspan class=\"co\"\u003e// =\u0026gt; true\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb27-2\" title=\"2\"\u003e\u003cspan class=\"at\"\u003ebinarySearch\u003c/span\u003e([\u003cspan class=\"dv\"\u003e5\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e10\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e12\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e15\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e20\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e30\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e70\u003c/span\u003e]\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e24\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e  \u003cspan class=\"co\"\u003e// =\u0026gt; false\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eBefore we move on, really internalize the fact that \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003ebinarySearch\u003c/code\u003e will only work on\n                    \u003cstrong\u003esorted\u003c/strong\u003e arrays! Obviously we can search any array, sorted or unsorted, in\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(n)\u003c/code\u003e\n                    time. But now our goal is be able to search the array with a sub-linear time complexity (less than\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(n)\u003c/code\u003e).\n                \u003c/p\u003e\n                \u003ch2 id=\"the-pseudocode-3\"\u003eThe pseudocode\u003c/h2\u003e\n                \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\"\u003eprocedure binary search (list, target)\n  parameter list: a list of sorted value\n  parameter target: the value to search for\n\n  if the list has zero length, then return false\n\n  determine the slice point:\n    if the list has an even number of elements,\n      the slice point is the number of elements\n      divided by two\n    if the list has an odd number of elements,\n      the slice point is the number of elements\n      minus one divided by two\n\n  create an list of the elements from 0 to the\n    slice point, not including the slice point,\n    which is known as the \u0026quot;left half\u0026quot;\n  create an list of the elements from the\n    slice point to the end of the list which is\n    known as the \u0026quot;right half\u0026quot;\n\n  if the target is less than the value in the\n    original array at the slice point, then\n    return the binary search of the \u0026quot;left half\u0026quot;\n    and the target\n  if the target is greater than the value in the\n    original array at the slice point, then\n    return the binary search of the \u0026quot;right half\u0026quot;\n    and the target\n  if neither of those is true, return true\nend procedure binary search\u003c/code\u003e\u003c/pre\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"bubble-sort-analysis\"\u003eBubble Sort Analysis\u003c/h1\u003e\n                \u003cp\u003eBubble Sort manipulates the array by swapping the position of two elements. To implement Bubble Sort\n                    in JS,\n                    you'll need to perform this operation. It helps to have a function to do that. A key detail in this\n                    function\n                    is\n                    that you need an extra variable to store one of the elements since you will be overwriting them in\n                    the\n                    array:\n                \u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb29\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\" class=\"sourceCode js\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode javascript\"\u003e\u003ca class=\"sourceLine\" id=\"cb29-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003eswap\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e idx1\u003cspan class=\"op\"\u003e,\u003c/span\u003e idx2) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb29-2\" title=\"2\"\u003e  \u003cspan class=\"kw\"\u003elet\u003c/span\u003e temp \u003cspan class=\"op\"\u003e=\u003c/span\u003e array[idx1]\u003cspan class=\"op\"\u003e;\u003c/span\u003e     \u003cspan class=\"co\"\u003e// save a copy of the first value\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb29-3\" title=\"3\"\u003e  array[idx1] \u003cspan class=\"op\"\u003e=\u003c/span\u003e array[idx2]\u003cspan class=\"op\"\u003e;\u003c/span\u003e  \u003cspan class=\"co\"\u003e// overwrite the first value with the second value\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb29-4\" title=\"4\"\u003e  array[idx2] \u003cspan class=\"op\"\u003e=\u003c/span\u003e temp\u003cspan class=\"op\"\u003e;\u003c/span\u003e         \u003cspan class=\"co\"\u003e// overwrite the second value with the first value\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb29-5\" title=\"5\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eNote that the swap function does not create or return a new array. It mutates the original array:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb30\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\n                        class=\"sourceCode js\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode javascript\"\u003e\u003ca class=\"sourceLine\" id=\"cb30-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003elet\u003c/span\u003e arr1 \u003cspan class=\"op\"\u003e=\u003c/span\u003e [\u003cspan class=\"dv\"\u003e2\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e8\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e5\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e2\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e6\u003c/span\u003e]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb30-2\" title=\"2\"\u003e\u003cspan class=\"at\"\u003eswap\u003c/span\u003e(arr1\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e\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=\"cb30-3\" title=\"3\"\u003earr1\u003cspan class=\"op\"\u003e;\u003c/span\u003e \u003cspan class=\"co\"\u003e// =\u0026gt; [ 2, 5, 8, 2, 6 ]\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003ch3 id=\"bubble-sort-js-implementation\"\u003eBubble Sort JS Implementation\u003c/h3\u003e\n                \u003cp\u003eTake a look at the snippet below and try to understand how it corresponds to the conceptual\n                    understanding of\n                    the\n                    algorithm. Scroll down to the commented version when you get stuck.\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb31\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\" class=\"sourceCode js\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode javascript\"\u003e\u003ca class=\"sourceLine\" id=\"cb31-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003ebubbleSort\u003c/span\u003e(array) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb31-2\" title=\"2\"\u003e  \u003cspan class=\"kw\"\u003elet\u003c/span\u003e swapped \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"kw\"\u003etrue\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb31-3\" title=\"3\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb31-4\" title=\"4\"\u003e  \u003cspan class=\"cf\"\u003ewhile\u003c/span\u003e(swapped) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb31-5\" title=\"5\"\u003e    swapped \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"kw\"\u003efalse\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb31-6\" title=\"6\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb31-7\" title=\"7\"\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 \u003cspan class=\"dv\"\u003e1\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=\"cb31-8\" title=\"8\"\u003e      \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (array[i] \u003cspan class=\"op\"\u003e\u0026gt;\u003c/span\u003e array[i\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=\"cb31-9\" title=\"9\"\u003e        \u003cspan class=\"at\"\u003eswap\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e i\u003cspan class=\"op\"\u003e,\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\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb31-10\" title=\"10\"\u003e        swapped \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"kw\"\u003etrue\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb31-11\" title=\"11\"\u003e      \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb31-12\" title=\"12\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb31-13\" title=\"13\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb31-14\" title=\"14\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb31-15\" title=\"15\"\u003e  \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e array\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb31-16\" title=\"16\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb32\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\" class=\"sourceCode js\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode javascript\"\u003e\u003ca class=\"sourceLine\" id=\"cb32-1\" title=\"1\"\u003e\u003cspan class=\"co\"\u003e// commented\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-2\" title=\"2\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003ebubbleSort\u003c/span\u003e(array) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-3\" title=\"3\"\u003e  \u003cspan class=\"co\"\u003e// this variable will be used to track whether or not we\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-4\" title=\"4\"\u003e  \u003cspan class=\"co\"\u003e// made a swap on the previous pass. If we did not make\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-5\" title=\"5\"\u003e  \u003cspan class=\"co\"\u003e// any swap on the previous pass, then the array must\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-6\" title=\"6\"\u003e  \u003cspan class=\"co\"\u003e// already be sorted\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-7\" title=\"7\"\u003e  \u003cspan class=\"kw\"\u003elet\u003c/span\u003e swapped \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"kw\"\u003etrue\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-8\" title=\"8\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-9\" title=\"9\"\u003e  \u003cspan class=\"co\"\u003e// this while will keep doing passes if a swap was made\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-10\" title=\"10\"\u003e  \u003cspan class=\"co\"\u003e// on the previous pass\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-11\" title=\"11\"\u003e  \u003cspan class=\"cf\"\u003ewhile\u003c/span\u003e(swapped) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-12\" title=\"12\"\u003e    swapped \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"kw\"\u003efalse\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e  \u003cspan class=\"co\"\u003e// reset swap to false\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-13\" title=\"13\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-14\" title=\"14\"\u003e    \u003cspan class=\"co\"\u003e// this for will perform a single pass\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-15\" title=\"15\"\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=\"cb32-16\" title=\"16\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-17\" title=\"17\"\u003e      \u003cspan class=\"co\"\u003e// if the two value are not ordered...\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-18\" title=\"18\"\u003e      \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (array[i] \u003cspan class=\"op\"\u003e\u0026gt;\u003c/span\u003e array[i\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=\"cb32-19\" title=\"19\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-20\" title=\"20\"\u003e        \u003cspan class=\"co\"\u003e// swap the two values\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-21\" title=\"21\"\u003e        \u003cspan class=\"at\"\u003eswap\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e i\u003cspan class=\"op\"\u003e,\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\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-22\" title=\"22\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-23\" title=\"23\"\u003e        \u003cspan class=\"co\"\u003e// since you made a swap, remember that you did so\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-24\" title=\"24\"\u003e        \u003cspan class=\"co\"\u003e// b/c we should perform another pass after this one\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-25\" title=\"25\"\u003e        swapped \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"kw\"\u003etrue\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-26\" title=\"26\"\u003e      \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-27\" title=\"27\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-28\" title=\"28\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-29\" title=\"29\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-30\" title=\"30\"\u003e  \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e array\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb32-31\" title=\"31\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003ch2 id=\"time-complexity-on2\"\u003eTime Complexity: O(n\u003csup\u003e2\u003c/sup\u003e)\u003c/h2\u003e\n                \u003cp\u003ePicture the worst case scenario where the input array is completely unsorted. Say it's sorted in\n                    fully\n                    decreasing\n                    order, but the goal is to sort it in increasing order:\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003en is the length of the input array\u003c/li\u003e\n                    \u003cli\u003eThe inner \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003efor\u003c/code\u003e loop along\n                        contributes \u003cem\u003eO(n)\u003c/em\u003e in isolation\u003c/li\u003e\n                    \u003cli\u003eThe outer while loop contributes \u003cem\u003eO(n)\u003c/em\u003e in isolation because a single iteration of the\n                        while loop\n                        will bring one element to its final resting position. In other words, it keeps running the while\n                        loop\n                        until\n                        the array is fully sorted. To fully sort the array we will need to bring all \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003en\u003c/code\u003e\n                        elements\n                        into\n                        their final resting positions.\u003c/li\u003e\n                    \u003cli\u003eThose two loops are nested so the total time complexity is O(n * n) = O(n\u003csup\u003e2\u003c/sup\u003e).\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003cp\u003eIt's worth mentioning that the best case scenario is when the input array is already fully sorted.\n                    This will\n                    cause our for loop to conduct a single pass without performing any swap, so the \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003ewhile\u003c/code\u003e\n                    loop will\n                    not\n                    trigger further iterations. This means best case time complexity is \u003cem\u003eO(n)\u003c/em\u003e for bubble sort.\n                    This best\n                    case linear time is probably the only advantage of bubble sort. Programmers are usually interested\n                    only in\n                    the\n                    worst-case analysis and ignore best-case analysis.\u003c/p\u003e\n                \u003ch2 id=\"space-complexity-o1\"\u003eSpace Complexity: O(1)\u003c/h2\u003e\n                \u003cp\u003eBubble Sort is a constant space, O(1), algorithm. The amount of memory consumed by the algorithm does\n                    not\n                    increase relative to the size of the input array. It uses the same amount of memory and create the\n                    same\n                    amount\n                    of variables regardless of the size of the input, making this algorithm quite space efficient. The\n                    space\n                    efficiency mostly comes from the fact that it mutates the input array in-place. This is known as a\n                    \u003cstrong\u003edestructive sort\u003c/strong\u003e because it \"destroys\" the positions of the values in the array.\n                \u003c/p\u003e\n                \u003ch2 id=\"when-should-you-use-bubble-sort\"\u003eWhen should you use Bubble Sort?\u003c/h2\u003e\n                \u003cp\u003eNearly never, but it may be a good choice in the following list of special cases:\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003eWhen sorting really small arrays where run time will be negligible no matter what algorithm you\n                        choose.\n                    \u003c/li\u003e\n                    \u003cli\u003eWhen sorting arrays that you expect to already be nearly sorted.\u003c/li\u003e\n                    \u003cli\u003eAt parties\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"selection-sort-analysis\"\u003eSelection Sort Analysis\u003c/h1\u003e\n                \u003cp\u003eSince a component of Selection Sort requires us to locate the smallest value in the array, let's\n                    focus on\n                    that\n                    pattern in isolation:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb33\"\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=\"cb33-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003eminumumValueIndex\u003c/span\u003e(arr) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb33-2\" title=\"2\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e minIndex \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb33-3\" title=\"3\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb33-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\"\u003e0\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e j \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e \u003cspan class=\"va\"\u003earr\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e\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=\"cb33-5\" title=\"5\"\u003e        \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (arr[minIndex] \u003cspan class=\"op\"\u003e\u0026gt;\u003c/span\u003e arr[j]) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb33-6\" title=\"6\"\u003e            minIndex \u003cspan class=\"op\"\u003e=\u003c/span\u003e j\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb33-7\" title=\"7\"\u003e        \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb33-8\" title=\"8\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb33-9\" title=\"9\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb33-10\" title=\"10\"\u003e    \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e minIndex\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb33-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\u003ePretty basic code right? We won't use this explicit helper function to solve selection sort, however\n                    we will\n                    borrow from this pattern soon.\u003c/p\u003e\n                \u003ch2 id=\"selection-sort-js-implementation\"\u003eSelection Sort JS Implementation\u003c/h2\u003e\n                \u003cp\u003eWe'll also utilize the classic swap pattern that we introduced in the bubble sort. To refresh:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb34\"\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=\"cb34-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003eswap\u003c/span\u003e(arr\u003cspan class=\"op\"\u003e,\u003c/span\u003e index1\u003cspan class=\"op\"\u003e,\u003c/span\u003e index2) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb34-2\" title=\"2\"\u003e  \u003cspan class=\"kw\"\u003elet\u003c/span\u003e temp \u003cspan class=\"op\"\u003e=\u003c/span\u003e arr[index1]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb34-3\" title=\"3\"\u003e  arr[index1] \u003cspan class=\"op\"\u003e=\u003c/span\u003e arr[index2]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb34-4\" title=\"4\"\u003e  arr[index2] \u003cspan class=\"op\"\u003e=\u003c/span\u003e temp\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb34-5\" title=\"5\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eNow for the punchline! Take a look at the snippet below and try to understand how it corresponds to\n                    our\n                    conceptual understanding of the selection sort algorithm. Scroll down to the commented version when\n                    you get\n                    stuck.\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb35\"\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=\"cb35-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003eselectionSort\u003c/span\u003e(arr) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb35-2\" title=\"2\"\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\"\u003earr\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=\"cb35-3\" title=\"3\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e minIndex \u003cspan class=\"op\"\u003e=\u003c/span\u003e i\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb35-4\" title=\"4\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb35-5\" title=\"5\"\u003e    \u003cspan class=\"cf\"\u003efor\u003c/span\u003e (\u003cspan class=\"kw\"\u003elet\u003c/span\u003e j \u003cspan class=\"op\"\u003e=\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 j \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e \u003cspan class=\"va\"\u003earr\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e\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=\"cb35-6\" title=\"6\"\u003e      \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (arr[minIndex] \u003cspan class=\"op\"\u003e\u0026gt;\u003c/span\u003e arr[j]) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb35-7\" title=\"7\"\u003e        minIndex \u003cspan class=\"op\"\u003e=\u003c/span\u003e j\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb35-8\" title=\"8\"\u003e      \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb35-9\" title=\"9\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb35-10\" title=\"10\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb35-11\" title=\"11\"\u003e    \u003cspan class=\"at\"\u003eswap\u003c/span\u003e(arr\u003cspan class=\"op\"\u003e,\u003c/span\u003e i\u003cspan class=\"op\"\u003e,\u003c/span\u003e minIndex)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb35-12\" title=\"12\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb35-13\" title=\"13\"\u003e  \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e arr\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb35-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                \u003cdiv class=\"sourceCode\" id=\"cb36\"\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=\"cb36-1\" title=\"1\"\u003e\u003cspan class=\"co\"\u003e// commented\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb36-2\" title=\"2\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003eselectionSort\u003c/span\u003e(arr) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb36-3\" title=\"3\"\u003e    \u003cspan class=\"co\"\u003e// the `i` loop will track the index that points to the first element of the unsorted region:\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb36-4\" title=\"4\"\u003e    \u003cspan class=\"co\"\u003e//    this means that the sorted region is everything left of index i\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb36-5\" title=\"5\"\u003e    \u003cspan class=\"co\"\u003e//    and the unsorted region is everything to the right of index i\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb36-6\" title=\"6\"\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\"\u003earr\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=\"cb36-7\" title=\"7\"\u003e        \u003cspan class=\"kw\"\u003elet\u003c/span\u003e minIndex \u003cspan class=\"op\"\u003e=\u003c/span\u003e i\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb36-8\" title=\"8\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb36-9\" title=\"9\"\u003e        \u003cspan class=\"co\"\u003e// the `j` loop will iterate through the unsorted region and find the index of the smallest element\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb36-10\" title=\"10\"\u003e        \u003cspan class=\"cf\"\u003efor\u003c/span\u003e (\u003cspan class=\"kw\"\u003elet\u003c/span\u003e j \u003cspan class=\"op\"\u003e=\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 j \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e \u003cspan class=\"va\"\u003earr\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e\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=\"cb36-11\" title=\"11\"\u003e            \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (arr[minIndex] \u003cspan class=\"op\"\u003e\u0026gt;\u003c/span\u003e arr[j]) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb36-12\" title=\"12\"\u003e                minIndex \u003cspan class=\"op\"\u003e=\u003c/span\u003e j\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb36-13\" title=\"13\"\u003e            \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb36-14\" title=\"14\"\u003e        \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb36-15\" title=\"15\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb36-16\" title=\"16\"\u003e        \u003cspan class=\"co\"\u003e// after we find the minIndex in the unsorted region,\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb36-17\" title=\"17\"\u003e        \u003cspan class=\"co\"\u003e// swap that minIndex with the first index of the unsorted region\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb36-18\" title=\"18\"\u003e        \u003cspan class=\"at\"\u003eswap\u003c/span\u003e(arr\u003cspan class=\"op\"\u003e,\u003c/span\u003e i\u003cspan class=\"op\"\u003e,\u003c/span\u003e minIndex)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb36-19\" title=\"19\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb36-20\" title=\"20\"\u003e    \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e arr\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb36-21\" title=\"21\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003ch2 id=\"time-complexity-analysis\"\u003eTime Complexity Analysis\u003c/h2\u003e\n                \u003cp\u003eSelection Sort runtime is O(n\u003csup\u003e2\u003c/sup\u003e) because:\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003en\u003c/code\u003e is the length of the input\n                        array\u003c/li\u003e\n                    \u003cli\u003eThe outer loop i contributes O(n) in isolation, this is plain to see\u003c/li\u003e\n                    \u003cli\u003eThe inner loop j is more complicated, it will make one less iteration for every iteration of i.\n                        \u003cul\u003e\n                            \u003cli\u003efor example, let's say we have an array of 10 elements, \u003ccode\n                                    class=\"language-javascript  highlight\" id=\"button\"\u003en = 10\u003c/code\u003e.\u003c/li\u003e\n                            \u003cli\u003ethe first full cycle of \u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003ej\u003c/code\u003e will have 9 iterations\u003c/li\u003e\n                            \u003cli\u003ethe second full cycle of \u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003ej\u003c/code\u003e will have 8 iterations\u003c/li\u003e\n                            \u003cli\u003ethe third full cycle of \u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003ej\u003c/code\u003e will have 7 iterations\u003c/li\u003e\n                            \u003cli\u003e…\u003c/li\u003e\n                            \u003cli\u003ethe last full cycle of \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ej\u003c/code\u003e\n                                will have 1 iteration\u003c/li\u003e\n                            \u003cli\u003eThis means that the inner loop j will contribute roughly O(n / 2) on average\u003c/li\u003e\n                        \u003c/ul\u003e\n                    \u003c/li\u003e\n                    \u003cli\u003eThe two loops are nested so our total time complexity is O(n * n / 2) = O(n\u003csup\u003e2\u003c/sup\u003e)\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003cp\u003eYou'll notice that during this analysis we said something silly like O(n / 2). In some analyses such\n                    as this\n                    one,\n                    we'll prefer to drop the constants only at the end of the sketch so you understand the logical steps\n                    we took\n                    to\n                    derive a complicated time complexity.\u003c/p\u003e\n                \u003ch2 id=\"space-complexity-analysis-o1\"\u003eSpace Complexity Analysis: O(1)\u003c/h2\u003e\n                \u003cp\u003eThe amount of memory consumed by the algorithm does not increase relative to the size of the input\n                    array. We\n                    use\n                    the same amount of memory and create the same amount of variables regardless of the size of our\n                    input. A\n                    quick\n                    indicator of this is the fact that we don't create any arrays.\u003c/p\u003e\n                \u003ch2 id=\"when-should-we-use-selection-sort\"\u003eWhen should we use Selection Sort?\u003c/h2\u003e\n                \u003cp\u003eThere is really only one use case where Selection Sort becomes superior to Bubble Sort. Both\n                    algorithms are\n                    quadratic in time and constant in space, but the point at which they differ is in the \u003cem\u003enumber of\n                        swaps\u003c/em\u003e\n                    they make.\u003c/p\u003e\n                \u003cp\u003eBubble Sort, in the worst case, invokes a swap on every single comparison. Selection Sort only swaps\n                    once our\n                    inner loop has completely finished traversing the array. Therefore, Selection Sort is optimized to\n                    make the\n                    least possible number of swaps.\u003c/p\u003e\n                \u003cp\u003eSelection Sort becomes advantageous when making a swap is the most expensive operation in your\n                    system. You\n                    will\n                    likely rarely encounter this scenario, but in a situation where you've built (or have inherited) a\n                    system\n                    with\n                    suboptimal write speed ability, for instance, maybe you're sorting data in a specialized database\n                    tuned\n                    strictly\n                    for fast read speeds at the expense of slow write speeds, using Selection Sort would save you a ton\n                    of\n                    expensive\n                    operations that could potential crash your system under peak load.\u003c/p\u003e\n                \u003cp\u003eThough in industry this situation is very rare, the insights above make for a fantastic\n                    conversational piece\n                    when\n                    weighing technical tradeoffs while strategizing solutions in an interview setting. This commentary\n                    may help\n                    deliver the impression that you are well-versed in system design and technical analysis, a key\n                    indicator\n                    that\n                    someone is prepared for a senior level position.\u003c/p\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"insertion-sort-analysis\"\u003eInsertion Sort Analysis\u003c/h1\u003e\n                \u003cp\u003eTake a look at the snippet below and try to understand how it corresponds to our conceptual\n                    understanding of\n                    the\n                    Insertion Sort algorithm. Scroll down to the commented version when you get stuck:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb37\"\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=\"cb37-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003einsertionSort\u003c/span\u003e(arr) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb37-2\" title=\"2\"\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=\"va\"\u003earr\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=\"cb37-3\" title=\"3\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e currElement \u003cspan class=\"op\"\u003e=\u003c/span\u003e arr[i]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb37-4\" title=\"4\"\u003e    \u003cspan class=\"cf\"\u003efor\u003c/span\u003e (\u003cspan class=\"kw\"\u003evar\u003c/span\u003e j \u003cspan class=\"op\"\u003e=\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 j \u003cspan class=\"op\"\u003e\u0026gt;=\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e \u003cspan class=\"op\"\u003e\u0026amp;\u0026amp;\u003c/span\u003e currElement \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e arr[j]\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=\"cb37-5\" title=\"5\"\u003e      arr[j \u003cspan class=\"op\"\u003e+\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e] \u003cspan class=\"op\"\u003e=\u003c/span\u003e arr[j]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb37-6\" title=\"6\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb37-7\" title=\"7\"\u003e    arr[j \u003cspan class=\"op\"\u003e+\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e] \u003cspan class=\"op\"\u003e=\u003c/span\u003e currElement\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb37-8\" title=\"8\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb37-9\" title=\"9\"\u003e  \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e arr\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb37-10\" title=\"10\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb38\"\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=\"cb38-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003einsertionSort\u003c/span\u003e(arr) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb38-2\" title=\"2\"\u003e    \u003cspan class=\"co\"\u003e// the `i` loop will iterate through every element of the array\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb38-3\" title=\"3\"\u003e    \u003cspan class=\"co\"\u003e// we begin at i = 1, because we can consider the first element of the array as a\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb38-4\" title=\"4\"\u003e    \u003cspan class=\"co\"\u003e// trivially sorted region of only one element\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb38-5\" title=\"5\"\u003e    \u003cspan class=\"co\"\u003e// insertion sort allows us to insert new elements anywhere within the sorted region\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb38-6\" title=\"6\"\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=\"va\"\u003earr\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=\"cb38-7\" title=\"7\"\u003e        \u003cspan class=\"co\"\u003e// grab the first element of the unsorted region\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb38-8\" title=\"8\"\u003e        \u003cspan class=\"kw\"\u003elet\u003c/span\u003e currElement \u003cspan class=\"op\"\u003e=\u003c/span\u003e arr[i]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb38-9\" title=\"9\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb38-10\" title=\"10\"\u003e        \u003cspan class=\"co\"\u003e// the `j` loop will iterate left through the sorted region,\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb38-11\" title=\"11\"\u003e        \u003cspan class=\"co\"\u003e// looking for a legal spot to insert currElement\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb38-12\" title=\"12\"\u003e        \u003cspan class=\"cf\"\u003efor\u003c/span\u003e (\u003cspan class=\"kw\"\u003evar\u003c/span\u003e j \u003cspan class=\"op\"\u003e=\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 j \u003cspan class=\"op\"\u003e\u0026gt;=\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e \u003cspan class=\"op\"\u003e\u0026amp;\u0026amp;\u003c/span\u003e currElement \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e arr[j]\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=\"cb38-13\" title=\"13\"\u003e            \u003cspan class=\"co\"\u003e// keep moving left while currElement is less than the j-th element\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb38-14\" title=\"14\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb38-15\" title=\"15\"\u003e            arr[j \u003cspan class=\"op\"\u003e+\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e] \u003cspan class=\"op\"\u003e=\u003c/span\u003e arr[j]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb38-16\" title=\"16\"\u003e            \u003cspan class=\"co\"\u003e// the line above will move the j-th element to the right,\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb38-17\" title=\"17\"\u003e            \u003cspan class=\"co\"\u003e// leaving a gap to potentially insert currElement\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb38-18\" title=\"18\"\u003e        \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb38-19\" title=\"19\"\u003e        \u003cspan class=\"co\"\u003e// insert currElement into that gap\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb38-20\" title=\"20\"\u003e        arr[j \u003cspan class=\"op\"\u003e+\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e] \u003cspan class=\"op\"\u003e=\u003c/span\u003e currElement\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb38-21\" title=\"21\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb38-22\" title=\"22\"\u003e    \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e arr\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb38-23\" title=\"23\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eThere are a few key pieces to point out in the above solution before moving forward:\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003e\n                        \u003cp\u003eThe outer \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003efor\u003c/code\u003e loop starts at\n                            the 1st index, not the 0th index, and moves to the\n                            right.\n                        \u003c/p\u003e\n                    \u003c/li\u003e\n                    \u003cli\u003e\n                        \u003cp\u003eThe inner \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003efor\u003c/code\u003e loop starts\n                            immediately to the left of the current element, and\n                            moves to\n                            the\n                            left.\u003c/p\u003e\n                    \u003c/li\u003e\n                    \u003cli\u003eThe condition for the inner \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003efor\u003c/code\u003e\n                        loop is complicated, and behaves similarly to a\n                        while loop!\n                        \u003cul\u003e\n                            \u003cli\u003eIt continues iterating to the left toward \u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003ej = 0\u003c/code\u003e, \u003cem\u003eonly while\u003c/em\u003e the\n                                \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ecurrElement\u003c/code\u003e is less than\n                                \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003earr[j]\u003c/code\u003e.\n                            \u003c/li\u003e\n                            \u003cli\u003eIt does this over and over until it finds the proper place to insert\n                                \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ecurrElement\u003c/code\u003e,\n                                and\n                                then we exit the inner loop!\n                            \u003c/li\u003e\n                        \u003c/ul\u003e\n                    \u003c/li\u003e\n                    \u003cli\u003e\n                        \u003cp\u003eWhen shifting elements in the sorted region to the right, it \u003cem\u003edoes not\u003c/em\u003e replace the\n                            value at\n                            their\n                            old index! If the input array is \u003ccode class=\"language-javascript  highlight\"\n                                id=\"button\"\u003e[1, 2, 4, 3]\u003c/code\u003e, and \u003ccode class=\"language-javascript  highlight\"\n                                id=\"button\"\u003ecurrElement\u003c/code\u003e is\n                            \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e3\u003c/code\u003e, after comparing \u003ccode\n                                class=\"language-javascript  highlight\" id=\"button\"\u003e4\u003c/code\u003e and \u003ccode\n                                class=\"language-javascript  highlight\" id=\"button\"\u003e3\u003c/code\u003e, but before inserting\n                            \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e3\u003c/code\u003e\n                            between \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e2\u003c/code\u003e and \u003ccode\n                                class=\"language-javascript  highlight\" id=\"button\"\u003e4\u003c/code\u003e, the array will look like\n                            this: \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e[1, 2, 4,\n                                4]\u003c/code\u003e.\n                        \u003c/p\u003e\n                    \u003c/li\u003e\n                \u003c/ol\u003e\n                \u003cp\u003eIf you are currently scratching your head, that is perfectly okay because when this one clicks, it\n                    clicks for\n                    good.\u003c/p\u003e\n                \u003cp\u003eIf you're struggling, you should try taking out a pen and paper and step through the solution\n                    provided above\n                    one\n                    step at a time. Keep track of \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ei\u003c/code\u003e,\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ej\u003c/code\u003e, \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003ecurrElement\u003c/code\u003e,\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003earr[j]\u003c/code\u003e,\n                    and\n                    the input \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003earr\u003c/code\u003e itself \u003cem\u003eat every\n                        step\u003c/em\u003e. After going through this a few times,\n                    you'll have\n                    your\n                    \"ah HA!\" moment.\n                \u003c/p\u003e\n                \u003ch2 id=\"time-and-space-complexity-analysis\"\u003eTime and Space Complexity Analysis\u003c/h2\u003e\n                \u003cp\u003eInsertion Sort runtime is O(n\u003csup\u003e2\u003c/sup\u003e) because:\u003c/p\u003e\n                \u003cp\u003eIn the \u003cstrong\u003eworst case scenario\u003c/strong\u003e where our input array is entirely unsorted, since this\n                    algorithm\n                    contains a nested loop, its run time behaves similarly to \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003ebubbleSort\u003c/code\u003e and\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eselectionSort\u003c/code\u003e. In this case, we are\n                    forced to make a comparison at each iteration of\n                    the inner\n                    loop. Not convinced? Let's derive the complexity. We'll use much of the same argument as we did in\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eselectionSort\u003c/code\u003e. Say we had the worst\n                    case scenario where are input array is sorted in\n                    full\n                    decreasing order, but we wanted to sort it in increasing order:\n                \u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003en\u003c/code\u003e is the length of the input\n                        array\u003c/li\u003e\n                    \u003cli\u003eThe outer loop i contributes O(n) in isolation, this is plain to see\u003c/li\u003e\n                    \u003cli\u003eThe inner loop j is more complicated. We know j will iterate until it finds an appropriate place\n                        to\n                        insert\n                        the \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ecurrElement\u003c/code\u003e into the sorted\n                        region. However, since we are discussing the case\n                        where the\n                        data is already in decreasing order, the element must travel the maximum distance to find it's\n                        insertion\n                        point! We know this insertion point to be index 0, since every \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003ecurrElement\u003c/code\u003e will be\n                        the next\n                        smallest of the array. So:\n                        \u003cul\u003e\n                            \u003cli\u003ethe 1st element travels 1 distance to be inserted\u003c/li\u003e\n                            \u003cli\u003ethe 2nd element travels 2 distance to be inserted\u003c/li\u003e\n                            \u003cli\u003ethe 3rd element travels 3 distance to be inserted\u003c/li\u003e\n                            \u003cli\u003e…\u003c/li\u003e\n                            \u003cli\u003ethe n-1th element travels n-1 distance to be inserted\u003c/li\u003e\n                            \u003cli\u003eThis means that our inner loop j will contribute roughly O(n / 2) on average\u003c/li\u003e\n                        \u003c/ul\u003e\n                    \u003c/li\u003e\n                    \u003cli\u003eThe two loops are nested so our total time complexity is O(n * n / 2) = O(n\u003csup\u003e2\u003c/sup\u003e)\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003ch3 id=\"space-complexity-o1-1\"\u003eSpace Complexity: O(1)\u003c/h3\u003e\n                \u003cp\u003eThe amount of memory consumed by the algorithm does not increase relative to the size of the input\n                    array. We\n                    use\n                    the same amount of memory and create the same amount of variables regardless of the size of our\n                    input. A\n                    quick\n                    indicator of this is the fact that we don't create any arrays.\u003c/p\u003e\n                \u003ch2 id=\"when-should-you-use-insertion-sort\"\u003eWhen should you use Insertion Sort?\u003c/h2\u003e\n                \u003cp\u003eInsertion Sort has one advantage that makes it absolutely supreme in one special case. Insertion Sort\n                    is\n                    what's\n                    known as an \"online\" algorithm. Online algorithms are great when you're dealing with \u003cem\u003estreaming\n                        data\u003c/em\u003e,\n                    because they can sort the data live \u003cem\u003eas it is received\u003c/em\u003e.\u003c/p\u003e\n                \u003cp\u003eIf you must sort a set of data that is ever-incoming, for example, maybe you are sorting the most\n                    relevant\n                    posts\n                    in a social media feed so that those posts that are most likely to impact the site's audience always\n                    appear\n                    at\n                    the top of the feed, an online algorithm like Insertion Sort is a great option.\u003c/p\u003e\n                \u003cp\u003eInsertion Sort works well in this situation because the left side of the array is always sorted, and\n                    in the\n                    case\n                    of nearly sorted arrays, it can run in linear time. The absolute best case scenario for Insertion\n                    Sort is\n                    when\n                    there is only one unsorted element, and it is located all the way to the right of the array.\u003c/p\u003e\n                \u003cp\u003eWell, if you have data constantly being pushed to the array, it will always be added to the right\n                    side. If\n                    you\n                    keep your algorithm constantly running, the left side will always be sorted. Now you have linear\n                    time sort.\n                \u003c/p\u003e\n                \u003cp\u003eOtherwise, Insertion Sort is, in general, useful in all the same situations as Bubble Sort. It's a\n                    good\n                    option\n                    when:\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003eYou are sorting really small arrays where run time will be negligible no matter what algorithm\n                        we\n                        choose.\n                    \u003c/li\u003e\n                    \u003cli\u003eYou are sorting an array that you expect to already be nearly sorted.\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"merge-sort-analysis\"\u003eMerge Sort Analysis\u003c/h1\u003e\n                \u003cp\u003eYou needed to come up with two pieces of code to make merge sort work.\u003c/p\u003e\n                \u003ch2 id=\"full-code\"\u003eFull code\u003c/h2\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb39\"\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=\"cb39-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003emerge\u003c/span\u003e(array1\u003cspan class=\"op\"\u003e,\u003c/span\u003e array2) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-2\" title=\"2\"\u003e  \u003cspan class=\"kw\"\u003elet\u003c/span\u003e merged \u003cspan class=\"op\"\u003e=\u003c/span\u003e []\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-3\" title=\"3\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-4\" title=\"4\"\u003e  \u003cspan class=\"cf\"\u003ewhile\u003c/span\u003e (\u003cspan class=\"va\"\u003earray1\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e||\u003c/span\u003e \u003cspan class=\"va\"\u003earray2\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-5\" title=\"5\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e ele1 \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray1\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e?\u003c/span\u003e array1[\u003cspan class=\"dv\"\u003e0\u003c/span\u003e] : \u003cspan class=\"kw\"\u003eInfinity\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-6\" title=\"6\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e ele2 \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray2\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e?\u003c/span\u003e array2[\u003cspan class=\"dv\"\u003e0\u003c/span\u003e] : \u003cspan class=\"kw\"\u003eInfinity\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-7\" title=\"7\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-8\" title=\"8\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e next\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-9\" title=\"9\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (ele1 \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e ele2) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-10\" title=\"10\"\u003e      next \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray1\u003c/span\u003e.\u003cspan class=\"at\"\u003eshift\u003c/span\u003e()\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-11\" title=\"11\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e \u003cspan class=\"cf\"\u003eelse\u003c/span\u003e \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-12\" title=\"12\"\u003e      next \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray2\u003c/span\u003e.\u003cspan class=\"at\"\u003eshift\u003c/span\u003e()\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-13\" title=\"13\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-14\" title=\"14\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-15\" title=\"15\"\u003e    \u003cspan class=\"va\"\u003emerged\u003c/span\u003e.\u003cspan class=\"at\"\u003epush\u003c/span\u003e(next)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-16\" title=\"16\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-17\" title=\"17\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-18\" title=\"18\"\u003e  \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e merged\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-19\" title=\"19\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-20\" title=\"20\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-21\" title=\"21\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003emergeSort\u003c/span\u003e(array) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-22\" title=\"22\"\u003e  \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e\u0026lt;=\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=\"cb39-23\" title=\"23\"\u003e    \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e array\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-24\" title=\"24\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-25\" title=\"25\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-26\" title=\"26\"\u003e  \u003cspan class=\"kw\"\u003elet\u003c/span\u003e midIdx \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003eMath\u003c/span\u003e.\u003cspan class=\"at\"\u003efloor\u003c/span\u003e(\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\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=\"cb39-27\" title=\"27\"\u003e  \u003cspan class=\"kw\"\u003elet\u003c/span\u003e leftHalf \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eslice\u003c/span\u003e(\u003cspan class=\"dv\"\u003e0\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e midIdx)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-28\" title=\"28\"\u003e  \u003cspan class=\"kw\"\u003elet\u003c/span\u003e rightHalf \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eslice\u003c/span\u003e(midIdx)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-29\" title=\"29\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-30\" title=\"30\"\u003e  \u003cspan class=\"kw\"\u003elet\u003c/span\u003e sortedLeft \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"at\"\u003emergeSort\u003c/span\u003e(leftHalf)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-31\" title=\"31\"\u003e  \u003cspan class=\"kw\"\u003elet\u003c/span\u003e sortedRight \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"at\"\u003emergeSort\u003c/span\u003e(rightHalf)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-32\" title=\"32\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-33\" title=\"33\"\u003e  \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"at\"\u003emerge\u003c/span\u003e(sortedLeft\u003cspan class=\"op\"\u003e,\u003c/span\u003e sortedRight)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb39-34\" title=\"34\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003ch2 id=\"merging-two-sorted-arrays\"\u003eMerging two sorted arrays\u003c/h2\u003e\n                \u003cp\u003eMerging two sorted arrays is simple. Since both arrays are sorted, we know the smallest numbers to\n                    always be\n                    at\n                    the front of the arrays. We can construct the new array by comparing the first elements of both\n                    input\n                    arrays. We\n                    remove the smaller element from it's respective array and add it to our new array. Do this until\n                    both input\n                    arrays are empty:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb40\"\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=\"cb40-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003emerge\u003c/span\u003e(array1\u003cspan class=\"op\"\u003e,\u003c/span\u003e array2) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb40-2\" title=\"2\"\u003e  \u003cspan class=\"kw\"\u003elet\u003c/span\u003e merged \u003cspan class=\"op\"\u003e=\u003c/span\u003e []\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb40-3\" title=\"3\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb40-4\" title=\"4\"\u003e  \u003cspan class=\"cf\"\u003ewhile\u003c/span\u003e (\u003cspan class=\"va\"\u003earray1\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e||\u003c/span\u003e \u003cspan class=\"va\"\u003earray2\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb40-5\" title=\"5\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e ele1 \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray1\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e?\u003c/span\u003e array1[\u003cspan class=\"dv\"\u003e0\u003c/span\u003e] : \u003cspan class=\"kw\"\u003eInfinity\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb40-6\" title=\"6\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e ele2 \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray2\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e?\u003c/span\u003e array2[\u003cspan class=\"dv\"\u003e0\u003c/span\u003e] : \u003cspan class=\"kw\"\u003eInfinity\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb40-7\" title=\"7\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb40-8\" title=\"8\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e next\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb40-9\" title=\"9\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (ele1 \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e ele2) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb40-10\" title=\"10\"\u003e      next \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray1\u003c/span\u003e.\u003cspan class=\"at\"\u003eshift\u003c/span\u003e()\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb40-11\" title=\"11\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e \u003cspan class=\"cf\"\u003eelse\u003c/span\u003e \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb40-12\" title=\"12\"\u003e      next \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray2\u003c/span\u003e.\u003cspan class=\"at\"\u003eshift\u003c/span\u003e()\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb40-13\" title=\"13\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb40-14\" title=\"14\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb40-15\" title=\"15\"\u003e    \u003cspan class=\"va\"\u003emerged\u003c/span\u003e.\u003cspan class=\"at\"\u003epush\u003c/span\u003e(next)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb40-16\" title=\"16\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb40-17\" title=\"17\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb40-18\" title=\"18\"\u003e  \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e merged\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb40-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\u003eRemember the following about JavaScript to understand the above code.\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e0\u003c/code\u003e is considered a falsey value,\n                        meaning it acts like \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003efalse\u003c/code\u003e when\n                        used\n                        in\n                        Boolean\n                        expressions. All other numbers are truthy.\u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eInfinity\u003c/code\u003e is a value that is\n                        guaranteed to be greater than any other quantity\u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eshift\u003c/code\u003e is an array method that\n                        removes and returns the first element\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003cp\u003eHere's the annotated version.\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb41\"\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=\"cb41-1\" title=\"1\"\u003e\u003cspan class=\"co\"\u003e// commented\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-2\" title=\"2\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003emerge\u003c/span\u003e(array1\u003cspan class=\"op\"\u003e,\u003c/span\u003e array2) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-3\" title=\"3\"\u003e  \u003cspan class=\"kw\"\u003elet\u003c/span\u003e merged \u003cspan class=\"op\"\u003e=\u003c/span\u003e []\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-4\" title=\"4\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-5\" title=\"5\"\u003e  \u003cspan class=\"co\"\u003e// keep running while either array still contains elements\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-6\" title=\"6\"\u003e  \u003cspan class=\"cf\"\u003ewhile\u003c/span\u003e (\u003cspan class=\"va\"\u003earray1\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e||\u003c/span\u003e \u003cspan class=\"va\"\u003earray2\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-7\" title=\"7\"\u003e    \u003cspan class=\"co\"\u003e// if array1 is nonempty, take its the first element as ele1\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-8\" title=\"8\"\u003e    \u003cspan class=\"co\"\u003e// otherwise array1 is empty, so take Infinity as ele1\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-9\" title=\"9\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e ele1 \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray1\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e?\u003c/span\u003e array1[\u003cspan class=\"dv\"\u003e0\u003c/span\u003e] : \u003cspan class=\"kw\"\u003eInfinity\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-10\" title=\"10\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-11\" title=\"11\"\u003e    \u003cspan class=\"co\"\u003e// do the same for array2, ele2\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-12\" title=\"12\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e ele2 \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray2\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e?\u003c/span\u003e array2[\u003cspan class=\"dv\"\u003e0\u003c/span\u003e] : \u003cspan class=\"kw\"\u003eInfinity\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-13\" title=\"13\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-14\" title=\"14\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e next\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-15\" title=\"15\"\u003e    \u003cspan class=\"co\"\u003e// remove the smaller of the eles from it\u0026#39;s array\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-16\" title=\"16\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (ele1 \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e ele2) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-17\" title=\"17\"\u003e      next \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray1\u003c/span\u003e.\u003cspan class=\"at\"\u003eshift\u003c/span\u003e()\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-18\" title=\"18\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e \u003cspan class=\"cf\"\u003eelse\u003c/span\u003e \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-19\" title=\"19\"\u003e      next \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray2\u003c/span\u003e.\u003cspan class=\"at\"\u003eshift\u003c/span\u003e()\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-20\" title=\"20\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-21\" title=\"21\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-22\" title=\"22\"\u003e    \u003cspan class=\"co\"\u003e// and add that ele to the new array\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-23\" title=\"23\"\u003e    \u003cspan class=\"va\"\u003emerged\u003c/span\u003e.\u003cspan class=\"at\"\u003epush\u003c/span\u003e(next)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-24\" title=\"24\"\u003e  \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-25\" title=\"25\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-26\" title=\"26\"\u003e  \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e merged\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb41-27\" title=\"27\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eBy using \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eInfinity\u003c/code\u003e as the default\n                    element when an array is empty, we are able to\n                    elegantly handle\n                    the\n                    scenario where one array empties before the other. We know that any actual element will be less than\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eInfinity\u003c/code\u003e so we will continually take\n                    the other element into our merged array.\n                \u003c/p\u003e\n                \u003cp\u003eIn other words, we can safely handle this edge case:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb42\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\n                        class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode javascript\"\u003e\u003ca class=\"sourceLine\" id=\"cb42-1\" title=\"1\"\u003e\u003cspan class=\"at\"\u003emerge\u003c/span\u003e([\u003cspan class=\"dv\"\u003e10\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e13\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e15\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e25\u003c/span\u003e]\u003cspan class=\"op\"\u003e,\u003c/span\u003e [])\u003cspan class=\"op\"\u003e;\u003c/span\u003e  \u003cspan class=\"co\"\u003e// =\u0026gt; [10, 13, 15, 25]\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eNice! We now have a way to merge two sorted arrays into a single sorted array. It's worth mentioning\n                    that\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003emerge\u003c/code\u003e will have a \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003eO(n)\u003c/code\u003e runtime where \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003en\u003c/code\u003e is the combined length\n                    of the\n                    two\n                    input arrays. This is what we meant when we said it was \"easy\" to merge two sorted arrays; linear\n                    time is\n                    fast!\n                    We'll find fact this useful later.\n                \u003c/p\u003e\n                \u003ch2 id=\"divide-and-conquer-step-by-step\"\u003eDivide and conquer, step-by-step\u003c/h2\u003e\n                \u003cp\u003eNow that we satisfied the merge idea, let's handle the second point. That is, we say an array of 1 or\n                    0\n                    elements\n                    is already sorted. This will be the base case of our recursion. Let's begin adding this code:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb43\"\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=\"cb43-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003emergeSort\u003c/span\u003e(array) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb43-2\" title=\"2\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e\u0026lt;=\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=\"cb43-3\" title=\"3\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e array\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb43-4\" title=\"4\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb43-5\" title=\"5\"\u003e    \u003cspan class=\"co\"\u003e// ....\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb43-6\" title=\"6\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eIf our base case pertains to an array of a very small size, then the design of our recursive case\n                    should make\n                    progress toward hitting this base scenario. In other words, we should recursively call\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003emergeSort\u003c/code\u003e on\n                    smaller and smaller arrays. A logical way to do this is to take the input array and split it into\n                    left and\n                    right\n                    halves.\n                \u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb44\"\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=\"cb44-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003emergeSort\u003c/span\u003e(array) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb44-2\" title=\"2\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e\u0026lt;=\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=\"cb44-3\" title=\"3\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e array\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb44-4\" title=\"4\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb44-5\" title=\"5\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb44-6\" title=\"6\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e midIdx \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003eMath\u003c/span\u003e.\u003cspan class=\"at\"\u003efloor\u003c/span\u003e(\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\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=\"cb44-7\" title=\"7\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e leftHalf \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eslice\u003c/span\u003e(\u003cspan class=\"dv\"\u003e0\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e midIdx)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb44-8\" title=\"8\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e rightHalf \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eslice\u003c/span\u003e(midIdx)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb44-9\" title=\"9\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb44-10\" title=\"10\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e sortedLeft \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"at\"\u003emergeSort\u003c/span\u003e(leftHalf)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb44-11\" title=\"11\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e sortedRight \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"at\"\u003emergeSort\u003c/span\u003e(rightHalf)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb44-12\" title=\"12\"\u003e    \u003cspan class=\"co\"\u003e// ...\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb44-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\u003eHere is the part of the recursion where we do a lot of hand waving and we take things on faith. We\n                    know that\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003emergeSort\u003c/code\u003e will take in an array and\n                    return the sorted version; we assume that it works.\n                    That\n                    means\n                    the two recursive calls will return the \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003esortedLeft\u003c/code\u003e and \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003esortedRight\u003c/code\u003e halves.\n                \u003c/p\u003e\n                \u003cp\u003eOkay, so we have two sorted arrays. We want to return one sorted array. So \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003emerge\u003c/code\u003e them!\n                    Using the\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003emerge\u003c/code\u003e function we designed earlier:\n                \u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb45\"\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=\"cb45-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003emergeSort\u003c/span\u003e(array) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb45-2\" title=\"2\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e\u0026lt;=\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=\"cb45-3\" title=\"3\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e array\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb45-4\" title=\"4\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb45-5\" title=\"5\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb45-6\" title=\"6\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e midIdx \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003eMath\u003c/span\u003e.\u003cspan class=\"at\"\u003efloor\u003c/span\u003e(\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\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=\"cb45-7\" title=\"7\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e leftHalf \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eslice\u003c/span\u003e(\u003cspan class=\"dv\"\u003e0\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e midIdx)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb45-8\" title=\"8\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e rightHalf \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eslice\u003c/span\u003e(midIdx)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb45-9\" title=\"9\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb45-10\" title=\"10\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e sortedLeft \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"at\"\u003emergeSort\u003c/span\u003e(leftHalf)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb45-11\" title=\"11\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e sortedRight \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"at\"\u003emergeSort\u003c/span\u003e(rightHalf)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb45-12\" title=\"12\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb45-13\" title=\"13\"\u003e    \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"at\"\u003emerge\u003c/span\u003e(sortedLeft\u003cspan class=\"op\"\u003e,\u003c/span\u003e sortedRight)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb45-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\u003eWow. that's it. Notice how light the implementation of \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003emergeSort\u003c/code\u003e is. Much of the heavy\n                    lifting\n                    (the\n                    actually comparisons) is done by the \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003emerge\u003c/code\u003e helper.\u003c/p\u003e\n                \u003cp\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003emergeSort\u003c/code\u003e is a classic example of a\n                    \"Divide and Conquer\" algorithm. In other words, we\n                    keep\n                    breaking\n                    the array into smaller and smaller sub arrays. This is the same as saying we take the problem and\n                    break it\n                    down\n                    into smaller and smaller subproblems. We do this until the subproblems are so small that we\n                    trivially know\n                    the\n                    answer to them (an array length 0 or 1 is already sorted). Once we have those subanswers we can\n                    combine to\n                    reconstruct the larger problems that we previously divided (merge the left and right subarrays).\u003c/p\u003e\n                \u003ch2 id=\"time-and-space-complexity-analysis-1\"\u003eTime and Space Complexity Analysis\u003c/h2\u003e\n                \u003ch3 id=\"time-complexity-on-logn\"\u003eTime Complexity: O(n log(n))\u003c/h3\u003e\n                \u003cul\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003en\u003c/code\u003e is the length of the input\n                        array\u003c/li\u003e\n                    \u003cli\u003eWe must calculate how many recursive calls we make. The number of recursive calls is the number\n                        of times\n                        we\n                        must split the array to reach the base case. Since we split in half each time, the number of\n                        recursive\n                        calls\n                        is \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(log(n))\u003c/code\u003e.\n                        \u003cul\u003e\n                            \u003cli\u003efor example, say we had an array of length \u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003e32\u003c/code\u003e\u003c/li\u003e\n                            \u003cli\u003ethen the length would change as \u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003e32 -\u0026gt; 16 -\u0026gt; 8 -\u0026gt; 4 -\u0026gt; 2 -\u0026gt;\n                                    1\u003c/code\u003e, we\n                                have to\n                                split 5 times before reaching the base case, \u003ccode\n                                    class=\"language-javascript  highlight\" id=\"button\"\u003elog(32) = 5\u003c/code\u003e\u003c/li\u003e\n                            \u003cli\u003ein our algorithm, \u003cstrong\u003elog(n)\u003c/strong\u003e describes how many times we must halve\n                                \u003cstrong\u003en\u003c/strong\u003e\n                                until the quantity reaches 1.\n                            \u003c/li\u003e\n                        \u003c/ul\u003e\n                    \u003c/li\u003e\n                    \u003cli\u003eBesides the recursive calls, we must consider the while loop within the \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003emerge\u003c/code\u003e\n                        function,\n                        which\n                        contributes \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(n)\u003c/code\u003e in isolation\n                    \u003c/li\u003e\n                    \u003cli\u003eWe call \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003emerge\u003c/code\u003e in every recursive\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003emergeSort\u003c/code\u003e call, so the total\n                        complexity is\n                        \u003cstrong\u003eO(n * log(n))\u003c/strong\u003e\n                    \u003c/li\u003e\n                \u003c/ul\u003e\n                \u003ch3 id=\"space-complexity-on\"\u003eSpace Complexity: O(n)\u003c/h3\u003e\n                \u003cp\u003eMerge Sort is the first non-O(1) space sorting algorithm we've seen thus far.\u003c/p\u003e\n                \u003cp\u003eThe larger the size of our input array, the greater the number of subarrays we must create in memory.\n                    These\n                    are\n                    not free! They each take up finite space, and we will need a new subarray for each element in the\n                    original\n                    input. Therefore, Merge Sort has a linear space complexity, O(n).\u003c/p\u003e\n                \u003ch3 id=\"when-should-you-use-merge-sort\"\u003eWhen should you use Merge Sort?\u003c/h3\u003e\n                \u003cp\u003eUnless we, the engineers, have access in advance to some unique, exploitable insight about our\n                    dataset, it\n                    turns\n                    out that O(n log n) time is \u003cem\u003ethe best\u003c/em\u003e we can do when sorting unknown datasets.\u003c/p\u003e\n                \u003cp\u003eThat means that Merge Sort is fast! It's way faster than Bubble Sort, Selection Sort, and Insertion\n                    Sort.\n                    However, due to its linear space complexity, we must always weigh the trade off between speed and\n                    memory\n                    consumption when making the choice to use Merge Sort. Consider the following:\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003eIf you have unlimited memory available, use it, it's fast!\u003c/li\u003e\n                    \u003cli\u003eIf you have a decent amount of memory available and a medium sized dataset, run some tests\n                        first, but\n                        use\n                        it!\u003c/li\u003e\n                    \u003cli\u003eIn other cases, maybe you should consider other options.\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"quick-sort-analysis\"\u003eQuick Sort Analysis\u003c/h1\u003e\n                \u003cp\u003eLet's begin structuring the recursion. The base case of any recursive problem is where the input is\n                    so\n                    trivial,\n                    we immediately know the answer without calculation. If our problem is to sort an array, what is the\n                    trivial\n                    array? An array of 1 or 0 elements! Let's establish the code:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb46\"\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=\"cb46-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003equickSort\u003c/span\u003e(array) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb46-2\" title=\"2\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e\u0026lt;=\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=\"cb46-3\" title=\"3\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e array\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb46-4\" title=\"4\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb46-5\" title=\"5\"\u003e    \u003cspan class=\"co\"\u003e// ...\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb46-6\" title=\"6\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eIf our base case pertains to an array of a very small size, then the design of our recursive case\n                    should make\n                    progress toward hitting this base scenario. In other words, we should recursively call\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003equickSort\u003c/code\u003e on\n                    smaller and smaller arrays. This is very similar to our previous \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003emergeSort\u003c/code\u003e, except we\n                    don't\n                    just\n                    split the array down the middle. Instead we should arbitrarily choose an element of the array as a\n                    pivot and\n                    partition the remaining elements relative to this pivot:\n                \u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb47\"\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=\"cb47-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003equickSort\u003c/span\u003e(array) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb47-2\" title=\"2\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e\u0026lt;=\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=\"cb47-3\" title=\"3\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e array\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb47-4\" title=\"4\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb47-5\" title=\"5\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb47-6\" title=\"6\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e pivot \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eshift\u003c/span\u003e()\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb47-7\" title=\"7\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e left \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003efilter\u003c/span\u003e(el \u003cspan class=\"kw\"\u003e=\u0026gt;\u003c/span\u003e el \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e pivot)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb47-8\" title=\"8\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e right \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003efilter\u003c/span\u003e(el \u003cspan class=\"kw\"\u003e=\u0026gt;\u003c/span\u003e el \u003cspan class=\"op\"\u003e\u0026gt;=\u003c/span\u003e pivot)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb47-9\" title=\"9\"\u003e    \u003cspan class=\"co\"\u003e// ...\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eHere is what to notice about the partition step above: 1. the pivot is an element of the array; we\n                    arbitrarily\n                    chose the first element 2. we removed the pivot from the master array before we filter into the left\n                    and\n                    right\n                    partitions\u003c/p\u003e\n                \u003cp\u003eNow that we have the two subarrays of \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003eleft\u003c/code\u003e and \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003eright\u003c/code\u003e we have our\n                    subproblems! To\n                    solve\n                    these subproblems we must sort the subarrays. I wish we had a function that sorts an array…oh wait\n                    we do,\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003equickSort\u003c/code\u003e! Recursively:\n                \u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb48\"\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=\"cb48-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003equickSort\u003c/span\u003e(array) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb48-2\" title=\"2\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e\u0026lt;=\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=\"cb48-3\" title=\"3\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e array\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb48-4\" title=\"4\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb48-5\" title=\"5\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb48-6\" title=\"6\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e pivot \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eshift\u003c/span\u003e()\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb48-7\" title=\"7\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e left \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003efilter\u003c/span\u003e(el \u003cspan class=\"kw\"\u003e=\u0026gt;\u003c/span\u003e el \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e pivot)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb48-8\" title=\"8\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e right \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003efilter\u003c/span\u003e(el \u003cspan class=\"kw\"\u003e=\u0026gt;\u003c/span\u003e el \u003cspan class=\"op\"\u003e\u0026gt;=\u003c/span\u003e pivot)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb48-9\" title=\"9\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb48-10\" title=\"10\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e leftSorted \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"at\"\u003equickSort\u003c/span\u003e(left)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb48-11\" title=\"11\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e rightSorted \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"at\"\u003equickSort\u003c/span\u003e(right)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb48-12\" title=\"12\"\u003e    \u003cspan class=\"co\"\u003e// ...\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eOkay, so we have the two sorted partitions. This means we have the two subsolutions. But how do we\n                    put them\n                    together? Think about how we partitioned them in the first place. Everything in\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eleftSorted\u003c/code\u003e is\n                    \u003cstrong\u003eguaranteed\u003c/strong\u003e to be less than everything in \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003erightSorted\u003c/code\u003e. On top of that,\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003epivot\u003c/code\u003e should be placed after the\n                    last element in \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eleftSorted\u003c/code\u003e, but\n                    before\n                    the first\n                    element in \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003erightSorted\u003c/code\u003e. So all we\n                    need to do is to combine the elements in the order\n                    \"left,\n                    pivot,\n                    right\"!\n                \u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb49\"\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=\"cb49-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003equickSort\u003c/span\u003e(array) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb49-2\" title=\"2\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e\u0026lt;=\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=\"cb49-3\" title=\"3\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e array\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb49-4\" title=\"4\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb49-5\" title=\"5\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb49-6\" title=\"6\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e pivot \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eshift\u003c/span\u003e()\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb49-7\" title=\"7\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e left \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003efilter\u003c/span\u003e(el \u003cspan class=\"kw\"\u003e=\u0026gt;\u003c/span\u003e el \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e pivot)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb49-8\" title=\"8\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e right \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003efilter\u003c/span\u003e(el \u003cspan class=\"kw\"\u003e=\u0026gt;\u003c/span\u003e el \u003cspan class=\"op\"\u003e\u0026gt;=\u003c/span\u003e pivot)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb49-9\" title=\"9\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb49-10\" title=\"10\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e leftSorted \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"at\"\u003equickSort\u003c/span\u003e(left)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb49-11\" title=\"11\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e rightSorted \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"at\"\u003equickSort\u003c/span\u003e(right)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb49-12\" title=\"12\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb49-13\" title=\"13\"\u003e    \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"va\"\u003eleftSorted\u003c/span\u003e.\u003cspan class=\"at\"\u003econcat\u003c/span\u003e([pivot]).\u003cspan class=\"at\"\u003econcat\u003c/span\u003e(rightSorted)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb49-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\u003eThat last \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003econcat\u003c/code\u003e line is a bit\n                    clunky. Bonus JS Lesson: we can use the spread\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e...\u003c/code\u003e\n                    operator to elegantly concatenate arrays. In general:\n                \u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb50\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\n                        class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode javascript\"\u003e\u003ca class=\"sourceLine\" id=\"cb50-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003elet\u003c/span\u003e one \u003cspan class=\"op\"\u003e=\u003c/span\u003e [\u003cspan class=\"st\"\u003e\u0026#39;a\u0026#39;\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"st\"\u003e\u0026#39;b\u0026#39;\u003c/span\u003e]\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb50-2\" title=\"2\"\u003e\u003cspan class=\"kw\"\u003elet\u003c/span\u003e two \u003cspan class=\"op\"\u003e=\u003c/span\u003e [\u003cspan class=\"st\"\u003e\u0026#39;d\u0026#39;\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"st\"\u003e\u0026#39;e\u0026#39;\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"st\"\u003e\u0026#39;f\u0026#39;\u003c/span\u003e]\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb50-3\" title=\"3\"\u003e\u003cspan class=\"kw\"\u003elet\u003c/span\u003e newArr \u003cspan class=\"op\"\u003e=\u003c/span\u003e [ ...\u003cspan class=\"at\"\u003eone\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"st\"\u003e\u0026#39;c\u0026#39;\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e ...\u003cspan class=\"at\"\u003etwo\u003c/span\u003e  ]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb50-4\" title=\"4\"\u003enewArr\u003cspan class=\"op\"\u003e;\u003c/span\u003e \u003cspan class=\"co\"\u003e// =\u0026gt;  [ \u0026#39;a\u0026#39;, \u0026#39;b\u0026#39;, \u0026#39;c\u0026#39;, \u0026#39;d\u0026#39;, \u0026#39;e\u0026#39;, \u0026#39;f\u0026#39; ]\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eUtilizing that spread pattern gives us this final implementation:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb51\"\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=\"cb51-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003equickSort\u003c/span\u003e(array) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb51-2\" title=\"2\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e\u0026lt;=\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=\"cb51-3\" title=\"3\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e array\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb51-4\" title=\"4\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb51-5\" title=\"5\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb51-6\" title=\"6\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e pivot \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eshift\u003c/span\u003e()\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb51-7\" title=\"7\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e left \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003efilter\u003c/span\u003e(el \u003cspan class=\"kw\"\u003e=\u0026gt;\u003c/span\u003e el \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e pivot)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb51-8\" title=\"8\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e right \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003efilter\u003c/span\u003e(el \u003cspan class=\"kw\"\u003e=\u0026gt;\u003c/span\u003e el \u003cspan class=\"op\"\u003e\u0026gt;=\u003c/span\u003e pivot)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb51-9\" title=\"9\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb51-10\" title=\"10\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e leftSorted \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"at\"\u003equickSort\u003c/span\u003e(left)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb51-11\" title=\"11\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e rightSorted \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"at\"\u003equickSort\u003c/span\u003e(right)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb51-12\" title=\"12\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb51-13\" title=\"13\"\u003e    \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e [ ...\u003cspan class=\"at\"\u003eleftSorted\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e pivot\u003cspan class=\"op\"\u003e,\u003c/span\u003e ...\u003cspan class=\"at\"\u003erightSorted\u003c/span\u003e ]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb51-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                \u003ch3 id=\"quicksort-sort-js-implementation\"\u003eQuicksort Sort JS Implementation\u003c/h3\u003e\n                \u003cp\u003eThat code was so clean we should show it again. Here's the complete code for your reference, for when\n                    you\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ectrl+F \"quicksort\"\u003c/code\u003e the night before\n                    an interview:\n                \u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb52\"\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=\"cb52-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003equickSort\u003c/span\u003e(array) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb52-2\" title=\"2\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e\u0026lt;=\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=\"cb52-3\" title=\"3\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e array\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb52-4\" title=\"4\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb52-5\" title=\"5\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb52-6\" title=\"6\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e pivot \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eshift\u003c/span\u003e()\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb52-7\" title=\"7\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e left \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003efilter\u003c/span\u003e(el \u003cspan class=\"kw\"\u003e=\u0026gt;\u003c/span\u003e el \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e pivot)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb52-8\" title=\"8\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e right \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003efilter\u003c/span\u003e(el \u003cspan class=\"kw\"\u003e=\u0026gt;\u003c/span\u003e el \u003cspan class=\"op\"\u003e\u0026gt;=\u003c/span\u003e pivot)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb52-9\" title=\"9\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb52-10\" title=\"10\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e leftSorted \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"at\"\u003equickSort\u003c/span\u003e(left)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb52-11\" title=\"11\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e rightSorted \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"at\"\u003equickSort\u003c/span\u003e(right)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb52-12\" title=\"12\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb52-13\" title=\"13\"\u003e    \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e [ ...\u003cspan class=\"at\"\u003eleftSorted\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e pivot\u003cspan class=\"op\"\u003e,\u003c/span\u003e ...\u003cspan class=\"at\"\u003erightSorted\u003c/span\u003e ]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb52-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                \u003ch2 id=\"time-and-space-complexity-analysis-2\"\u003eTime and Space Complexity Analysis\u003c/h2\u003e\n                \u003cp\u003eHere is a summary of the complexity.\u003c/p\u003e\n                \u003ch3 id=\"time-complexity\"\u003eTime Complexity\u003c/h3\u003e\n                \u003cul\u003e\n                    \u003cli\u003eAvg Case: O(n log(n))\u003c/li\u003e\n                    \u003cli\u003eWorst Case: O(n\u003csup\u003e2\u003c/sup\u003e)\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003cp\u003eThe runtime analysis of \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003equickSort\u003c/code\u003e is\n                    more complex than \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003emergeSort\u003c/code\u003e\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003en\u003c/code\u003e is the length of the input\n                        array\u003c/li\u003e\n                    \u003cli\u003eThe partition step alone is \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(n)\u003c/code\u003e\n                    \u003c/li\u003e\n                    \u003cli\u003eWe must calculate how many recursive calls we make. The number of recursive calls is the number\n                        of times\n                        we\n                        must split the array to reach the base case. This is dependent on how we choose the pivot. Let's\n                        analyze\n                        the\n                        best and worst case:\n                        \u003cul\u003e\n                            \u003cli\u003e\u003cstrong\u003eBest Case:\u003c/strong\u003e We are lucky and always choose the median as the pivot. This\n                                means\n                                the\n                                left and right partitions will have equal length. This will halve the array length at\n                                every step\n                                of\n                                the recursion. We benefit from this halving with \u003ccode\n                                    class=\"language-javascript  highlight\" id=\"button\"\u003eO(log(n))\u003c/code\u003e recursive calls\n                                to reach\n                                the\n                                base case.\u003c/li\u003e\n                            \u003cli\u003e\u003cstrong\u003eWorst Case:\u003c/strong\u003e We are unlucky and always choose the min or max as the\n                                pivot. This\n                                means one partition will contain everything, and the other partition is empty. This will\n                                decrease\n                                the array length by 1 at every step of the recursion. We suffer from \u003ccode\n                                    class=\"language-javascript  highlight\" id=\"button\"\u003eO(n)\u003c/code\u003e\n                                recursive\n                                calls to reach the base case.\u003c/li\u003e\n                        \u003c/ul\u003e\n                    \u003c/li\u003e\n                    \u003cli\u003eThe partition step occurs in every recursive call, so our total complexities are:\n                        \u003cul\u003e\n                            \u003cli\u003e\u003cstrong\u003eBest Case:\u003c/strong\u003e O(n * log(n))\u003c/li\u003e\n                            \u003cli\u003e\u003cstrong\u003eWorst Case:\u003c/strong\u003e O(n\u003csup\u003e2\u003c/sup\u003e)\u003c/li\u003e\n                        \u003c/ul\u003e\n                    \u003c/li\u003e\n                \u003c/ul\u003e\n                \u003cp\u003eAlthough we typically take the worst case when describing Big-O for an algorithm, much research on\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003equickSort\u003c/code\u003e has shown the worst case\n                    to be an exceedingly rare occurrence even if we\n                    choose the\n                    pivot\n                    at random. Because of this we still consider \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003equickSort\u003c/code\u003e an efficient algorithm. This is\n                    a common\n                    interview talking point, so you should be familiar with the relationship between the choice of pivot\n                    and\n                    efficiency of the algorithm.\n                \u003c/p\u003e\n                \u003cp\u003eJust in case: A somewhat common question a student may ask when studying \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003equickSort\u003c/code\u003e is,\n                    \"If the\n                    median is the best pivot, why don't we always just choose the median when we partition?\" Don't\n                    overthink\n                    this.\n                    To know the median of an array, it must be sorted in the first place.\u003c/p\u003e\n                \u003ch3 id=\"space-complexity\"\u003eSpace Complexity\u003c/h3\u003e\n                \u003cp\u003eOur implementation of \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003equickSort\u003c/code\u003e uses\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(n)\u003c/code\u003e space because of the partition\n                    arrays we\n                    create. There is an in-place version of \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003equickSort\u003c/code\u003e that uses \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003eO(log(n))\u003c/code\u003e\n                    space.\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(log(n))\u003c/code\u003e space is not huge benefit\n                    over \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(n)\u003c/code\u003e. You'll also find our\n                    version of\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003equickSort\u003c/code\u003e as easier to remember,\n                    easier to implement. Just know that a\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(logn)\u003c/code\u003e\n                    space\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003equickSort\u003c/code\u003e exists.\n                \u003c/p\u003e\n                \u003ch3 id=\"when-should-you-use-quick-sort\"\u003eWhen should you use Quick Sort?\u003c/h3\u003e\n                \u003cul\u003e\n                    \u003cli\u003eWhen you are in a pinch and need to throw down an efficient sort (on average). The recursive\n                        code is\n                        light\n                        and simple to implement; much smaller than \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003emergeSort\u003c/code\u003e.\u003c/li\u003e\n                    \u003cli\u003eWhen constant space is important to you, use the in-place version. This will of course trade off\n                        some\n                        simplicity of implementation.\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003cp\u003eIf you know some constraints about dataset you can make some modifications to optimize pivot choice.\n                    Here's\n                    some\n                    food for thought. Our implementation of \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003equickSort\u003c/code\u003e will always take the first element as\n                    the\n                    pivot.\n                    This means we will suffer from the worst case time complexity in the event that we are given an\n                    already\n                    sorted\n                    array (ironic isn't it?). If you know your input data to be mostly already sorted, randomize the\n                    choice of\n                    pivot\n                    - this is a very easy change. Bam. Solved like a true engineer.\u003c/p\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"binary-search-analysis\"\u003eBinary Search Analysis\u003c/h1\u003e\n                \u003cp\u003eWe'll implement binary search recursively. As always, we start with a base case that captures the\n                    scenario of\n                    the\n                    input array being so trivial, that we know the answer without further calculation. If we are given\n                    an empty\n                    array and a target, we can be certain that the target is not inside of the array:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb53\"\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=\"cb53-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003ebinarySearch\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e target) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb53-2\" title=\"2\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e===\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb53-3\" title=\"3\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"kw\"\u003efalse\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb53-4\" title=\"4\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb53-5\" title=\"5\"\u003e    \u003cspan class=\"co\"\u003e// ...\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb53-6\" title=\"6\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eNow for our recursive case. If we want to get a time complexity less than \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003eO(n)\u003c/code\u003e, we must\n                    avoid\n                    touching all \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003en\u003c/code\u003e elements. Adopting\n                    our dictionary strategy, let's find the middle\n                    element and\n                    grab\n                    references to the left and right halves of the sorted array:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb54\"\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=\"cb54-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003ebinarySearch\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e target) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb54-2\" title=\"2\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e===\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb54-3\" title=\"3\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"kw\"\u003efalse\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb54-4\" title=\"4\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb54-5\" title=\"5\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb54-6\" title=\"6\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e midIdx \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003eMath\u003c/span\u003e.\u003cspan class=\"at\"\u003efloor\u003c/span\u003e(\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\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=\"cb54-7\" title=\"7\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e leftHalf \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eslice\u003c/span\u003e(\u003cspan class=\"dv\"\u003e0\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e midIdx)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb54-8\" title=\"8\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e rightHalf \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eslice\u003c/span\u003e(midIdx \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=\"cb54-9\" title=\"9\"\u003e    \u003cspan class=\"co\"\u003e// ...\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb54-10\" title=\"10\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eIt's worth pointing out that the left and right halves do not contain the middle element we chose.\n                \u003c/p\u003e\n                \u003cp\u003eHere is where we leverage the sorted property of the array. If the target is less than the middle,\n                    then the\n                    target must be in the left half of the array. If the target is greater than the middle, then the\n                    target must\n                    be\n                    in the right half of the array. So we can narrow our search to one of these halves, and ignore the\n                    other.\n                    Luckily we have a function that can search the half, its \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003ebinarySearch\u003c/code\u003e:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb55\"\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=\"cb55-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003ebinarySearch\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e target) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb55-2\" title=\"2\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e===\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb55-3\" title=\"3\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"kw\"\u003efalse\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb55-4\" title=\"4\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb55-5\" title=\"5\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb55-6\" title=\"6\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e midIdx \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003eMath\u003c/span\u003e.\u003cspan class=\"at\"\u003efloor\u003c/span\u003e(\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\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=\"cb55-7\" title=\"7\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e leftHalf \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eslice\u003c/span\u003e(\u003cspan class=\"dv\"\u003e0\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e midIdx)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb55-8\" title=\"8\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e rightHalf \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eslice\u003c/span\u003e(midIdx \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=\"cb55-9\" title=\"9\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb55-10\" title=\"10\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (target \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e array[midIdx]) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb55-11\" title=\"11\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"at\"\u003ebinarySearch\u003c/span\u003e(leftHalf\u003cspan class=\"op\"\u003e,\u003c/span\u003e target)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb55-12\" title=\"12\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e \u003cspan class=\"cf\"\u003eelse\u003c/span\u003e \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (target \u003cspan class=\"op\"\u003e\u0026gt;\u003c/span\u003e array[midIdx]) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb55-13\" title=\"13\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"at\"\u003ebinarySearch\u003c/span\u003e(rightHalf\u003cspan class=\"op\"\u003e,\u003c/span\u003e target)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb55-14\" title=\"14\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb55-15\" title=\"15\"\u003e    \u003cspan class=\"co\"\u003e// ...\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb55-16\" title=\"16\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eWe know \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ebinarySeach\u003c/code\u003e will return the\n                    correct Boolean, so we just pass that result up by\n                    returning\n                    it\n                    ourselves. However, something is lacking in our code. It is only possible to get a false from the\n                    literal\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ereturn false\u003c/code\u003e line, but there is no\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ereturn true\u003c/code\u003e. Looking at our\n                    conditionals, we\n                    handle\n                    the cases where the target is less than middle or the target is greater than the middle, but what if\n                    the\n                    product\n                    is \u003cstrong\u003eequal\u003c/strong\u003e to the middle? If the target is equal to the middle, then we found the\n                    target and\n                    should \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ereturn true\u003c/code\u003e! This is easy to\n                    add with an \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eelse\u003c/code\u003e:\n                \u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb56\"\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=\"cb56-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003ebinarySearch\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e target) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb56-2\" title=\"2\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e===\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb56-3\" title=\"3\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"kw\"\u003efalse\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb56-4\" title=\"4\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb56-5\" title=\"5\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb56-6\" title=\"6\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e midIdx \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003eMath\u003c/span\u003e.\u003cspan class=\"at\"\u003efloor\u003c/span\u003e(\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\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=\"cb56-7\" title=\"7\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e leftHalf \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eslice\u003c/span\u003e(\u003cspan class=\"dv\"\u003e0\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e midIdx)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb56-8\" title=\"8\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e rightHalf \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eslice\u003c/span\u003e(midIdx \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=\"cb56-9\" title=\"9\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb56-10\" title=\"10\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (target \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e array[midIdx]) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb56-11\" title=\"11\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"at\"\u003ebinarySearch\u003c/span\u003e(leftHalf\u003cspan class=\"op\"\u003e,\u003c/span\u003e target)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb56-12\" title=\"12\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e \u003cspan class=\"cf\"\u003eelse\u003c/span\u003e \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (target \u003cspan class=\"op\"\u003e\u0026gt;\u003c/span\u003e array[midIdx]) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb56-13\" title=\"13\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"at\"\u003ebinarySearch\u003c/span\u003e(rightHalf\u003cspan class=\"op\"\u003e,\u003c/span\u003e target)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb56-14\" title=\"14\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e \u003cspan class=\"cf\"\u003eelse\u003c/span\u003e \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb56-15\" title=\"15\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"kw\"\u003etrue\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb56-16\" title=\"16\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb56-17\" title=\"17\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eTo wrap up, we have confidence of our base case will eventually be hit because we are continually\n                    halving the\n                    array. We halve the array until it's length is 0 or we actually find the target.\u003c/p\u003e\n                \u003ch3 id=\"binary-search-js-implementation\"\u003eBinary Search JS Implementation\u003c/h3\u003e\n                \u003cp\u003eHere is the code again for your quick reference:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb57\"\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=\"cb57-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003ebinarySearch\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e target) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb57-2\" title=\"2\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e===\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb57-3\" title=\"3\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"kw\"\u003efalse\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb57-4\" title=\"4\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb57-5\" title=\"5\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb57-6\" title=\"6\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e midIdx \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003eMath\u003c/span\u003e.\u003cspan class=\"at\"\u003efloor\u003c/span\u003e(\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\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=\"cb57-7\" title=\"7\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e leftHalf \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eslice\u003c/span\u003e(\u003cspan class=\"dv\"\u003e0\u003c/span\u003e\u003cspan class=\"op\"\u003e,\u003c/span\u003e midIdx)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb57-8\" title=\"8\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e rightHalf \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eslice\u003c/span\u003e(midIdx \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=\"cb57-9\" title=\"9\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb57-10\" title=\"10\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (target \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e array[midIdx]) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb57-11\" title=\"11\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"at\"\u003ebinarySearch\u003c/span\u003e(leftHalf\u003cspan class=\"op\"\u003e,\u003c/span\u003e target)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb57-12\" title=\"12\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e \u003cspan class=\"cf\"\u003eelse\u003c/span\u003e \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (target \u003cspan class=\"op\"\u003e\u0026gt;\u003c/span\u003e array[midIdx]) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb57-13\" title=\"13\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"at\"\u003ebinarySearch\u003c/span\u003e(rightHalf\u003cspan class=\"op\"\u003e,\u003c/span\u003e target)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb57-14\" title=\"14\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e \u003cspan class=\"cf\"\u003eelse\u003c/span\u003e \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb57-15\" title=\"15\"\u003e        \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e \u003cspan class=\"kw\"\u003etrue\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb57-16\" title=\"16\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb57-17\" title=\"17\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003ch2 id=\"time-and-space-complexity-analysis-3\"\u003eTime and Space Complexity Analysis\u003c/h2\u003e\n                \u003cp\u003eThe complexity analysis of this algorithm is easier to explain through visuals, so we \u003cstrong\u003ehighly\n                        encourage\u003c/strong\u003e you to watch the lecture that accompanies this reading. In any case, here is\n                    a\n                    summary of\n                    the complexity:\u003c/p\u003e\n                \u003ch3 id=\"time-complexity-ologn\"\u003eTime Complexity: O(log(n))\u003c/h3\u003e\n                \u003cul\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003en\u003c/code\u003e is the length of the input\n                        array\u003c/li\u003e\n                    \u003cli\u003eWe have no loops, so we must only consider the number of recursive calls it takes to hit the\n                        base case\n                    \u003c/li\u003e\n                    \u003cli\u003eThe number of recursive calls is the number of times we must halve the array until it's length\n                        becomes\n                        0.\n                        This number can be described by \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003elog(n)\u003c/code\u003e\n                        \u003cul\u003e\n                            \u003cli\u003efor example, say we had an array of 8 elements, \u003ccode\n                                    class=\"language-javascript  highlight\" id=\"button\"\u003en = 8\u003c/code\u003e\u003c/li\u003e\n                            \u003cli\u003ethe length would halve as \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e8\n                                    -\u0026gt; 4 -\u0026gt; 2 -\u0026gt; 1\u003c/code\u003e\u003c/li\u003e\n                            \u003cli\u003eit takes 3 calls, \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003elog(8) =\n                                    3\u003c/code\u003e\u003c/li\u003e\n                        \u003c/ul\u003e\n                    \u003c/li\u003e\n                \u003c/ul\u003e\n                \u003ch3 id=\"space-complexity-on-1\"\u003eSpace Complexity: O(n)\u003c/h3\u003e\n                \u003cp\u003eOur implementation uses \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003en\u003c/code\u003e space due\n                    to half arrays we create using slice. Note that\n                    JavaScript\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eslice\u003c/code\u003e creates a new array, so it\n                    requires additional memory to be allocated.\n                \u003c/p\u003e\n                \u003ch3 id=\"when-should-we-use-binary-search\"\u003eWhen should we use Binary Search?\u003c/h3\u003e\n                \u003cp\u003eUse this algorithm when the input data is sorted!!! This is a heavy requirement, but if you have it,\n                    you'll\n                    have\n                    an insanely fast algorithm. Of course, you can use one of your high-functioning sorting algorithms\n                    to sort\n                    the\n                    input and \u003cem\u003ethen\u003c/em\u003e perform the binary search!\u003c/p\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"practice-bubble-sort\"\u003ePractice: Bubble Sort\u003c/h1\u003e\n                \u003cp\u003eThis project contains a skeleton for you to implement Bubble Sort. In the file\n                    \u003cstrong\u003elib/bubble_sort.js\u003c/strong\u003e, you should implement the Bubble Sort. This is a description of\n                    how the\n                    Bubble Sort works (and is also in the code file).\n                \u003c/p\u003e\n                \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\"\u003eBubble Sort: (array)\n  n := length(array)\n  repeat\n    swapped = false\n    for i := 1 to n - 1 inclusive do\n\n      /* if this pair is out of order */\n      if array[i - 1] \u0026gt; array[i] then\n\n        /* swap them and remember something changed */\n        swap(array, i - 1, i)\n        swapped := true\n\n      end if\n    end for\n  until not swapped\u003c/code\u003e\u003c/pre\u003e\n                \u003ch2 id=\"instructions\"\u003eInstructions\u003c/h2\u003e\n                \u003cul\u003e\n                    \u003cli\u003eClone the project from https://github.com/appacademy-starters/algorithms-bubble-sort-starter.\n                    \u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ecd\u003c/code\u003e into the project folder\u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003enpm install\u003c/code\u003e to install\n                        dependencies in the project root directory\u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003enpm test\u003c/code\u003e to run the specs\u003c/li\u003e\n                    \u003cli\u003eYou can view the test cases in \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003e/test/test.js\u003c/code\u003e. Your job is to write code in the\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e/lib/bubble_sort.js\u003c/code\u003e that\n                        implements the Bubble Sort.\n                    \u003c/li\u003e\n                \u003c/ul\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"practice-selection-sort\"\u003ePractice: Selection Sort\u003c/h1\u003e\n                \u003cp\u003eThis project contains a skeleton for you to implement Selection Sort. In the file\n                    \u003cstrong\u003elib/selection_sort.js\u003c/strong\u003e, you should implement the Selection Sort. You can use the\n                    same\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eswap\u003c/code\u003e function from Bubble Sort;\n                    however, try to implement it on your own, first.\n                \u003c/p\u003e\n                \u003cp\u003eThe algorithm can be summarized as the following:\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eSet MIN to location 0\u003c/li\u003e\n                    \u003cli\u003eSearch the minimum element in the list\u003c/li\u003e\n                    \u003cli\u003eSwap with value at location MIN\u003c/li\u003e\n                    \u003cli\u003eIncrement MIN to point to next element\u003c/li\u003e\n                    \u003cli\u003eRepeat until list is sorted\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003cp\u003eThis is a description of how the Selection Sort works (and is also in the code file).\u003c/p\u003e\n                \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\"\u003eprocedure selection sort(list)\n   list  : array of items\n   n     : size of list\n\n   for i = 1 to n - 1\n   /* set current element as minimum*/\n      min = i\n\n      /* check the element to be minimum */\n\n      for j = i+1 to n\n         if list[j] \u0026lt; list[min] then\n            min = j;\n         end if\n      end for\n\n      /* swap the minimum element with the current element*/\n      if indexMin != i  then\n         swap list[min] and list[i]\n      end if\n   end for\nend procedure\u003c/code\u003e\u003c/pre\u003e\n                \u003ch2 id=\"instructions-1\"\u003eInstructions\u003c/h2\u003e\n                \u003cul\u003e\n                    \u003cli\u003eClone the project from https://github.com/appacademy-starters/algorithms-selection-sort-starter.\n                    \u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ecd\u003c/code\u003e into the project folder\u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003enpm install\u003c/code\u003e to install\n                        dependencies in the project root directory\u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003enpm test\u003c/code\u003e to run the specs\u003c/li\u003e\n                    \u003cli\u003eYou can view the test cases in \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003e/test/test.js\u003c/code\u003e. Your job is to write code in the\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e/lib/selection_sort.js\u003c/code\u003e that\n                        implements the Selection Sort.\n                    \u003c/li\u003e\n                \u003c/ul\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"practice-insertion-sort\"\u003ePractice: Insertion Sort\u003c/h1\u003e\n                \u003cp\u003eThis project contains a skeleton for you to implement Insertion Sort. In the file\n                    \u003cstrong\u003elib/insertion_sort.js\u003c/strong\u003e, you should implement the Insertion Sort.\n                \u003c/p\u003e\n                \u003cp\u003eThe algorithm can be summarized as the following:\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eIf it is the first element, it is already sorted. return 1;\u003c/li\u003e\n                    \u003cli\u003ePick next element\u003c/li\u003e\n                    \u003cli\u003eCompare with all elements in the sorted sub-list\u003c/li\u003e\n                    \u003cli\u003eShift all the elements in the sorted sub-list that is greater than the value to be sorted\u003c/li\u003e\n                    \u003cli\u003eInsert the value\u003c/li\u003e\n                    \u003cli\u003eRepeat until list is sorted\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003cp\u003eThis is a description of how the Insertion Sort works (and is also in the code file).\u003c/p\u003e\n                \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\"\u003eprocedure insertionSort( A : array of items )\n   int holePosition\n   int valueToInsert\n\n   for i = 1 to length(A) inclusive do:\n\n      /* select value to be inserted */\n      valueToInsert = A[i]\n      holePosition = i\n\n      /*locate hole position for the element to be inserted */\n\n      while holePosition \u0026gt; 0 and A[holePosition-1] \u0026gt; valueToInsert do:\n         A[holePosition] = A[holePosition-1]\n         holePosition = holePosition -1\n      end while\n\n      /* insert the number at hole position */\n      A[holePosition] = valueToInsert\n\n   end for\n\nend procedure\u003c/code\u003e\u003c/pre\u003e\n                \u003ch2 id=\"instructions-2\"\u003eInstructions\u003c/h2\u003e\n                \u003cul\u003e\n                    \u003cli\u003eClone the project from https://github.com/appacademy-starters/algorithms-insertion-sort-starter.\n                    \u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ecd\u003c/code\u003e into the project folder\u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003enpm install\u003c/code\u003e to install\n                        dependencies in the project root directory\u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003enpm test\u003c/code\u003e to run the specs\u003c/li\u003e\n                    \u003cli\u003eYou can view the test cases in \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003e/test/test.js\u003c/code\u003e. Your job is to write code in the\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e/lib/insertion_sort.js\u003c/code\u003e that\n                        implements the Insertion Sort.\n                    \u003c/li\u003e\n                \u003c/ul\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"practice-merge-sort\"\u003ePractice: Merge Sort\u003c/h1\u003e\n                \u003cp\u003eThis project contains a skeleton for you to implement Merge Sort. In the file\n                    \u003cstrong\u003elib/merge_sort.js\u003c/strong\u003e,\n                    you should implement the Merge Sort.\n                \u003c/p\u003e\n                \u003cp\u003eThe algorithm can be summarized as the following:\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eif there is only one element in the list, it is already sorted. return that array.\u003c/li\u003e\n                    \u003cli\u003eotherwise, divide the list recursively into two halves until it can no more be divided.\u003c/li\u003e\n                    \u003cli\u003emerge the smaller lists into new list in sorted order.\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003cp\u003eThis is a description of how the Merge Sort works (and is also in the code file).\u003c/p\u003e\n                \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\"\u003eprocedure mergesort( a as array )\n   if ( n == 1 ) return a\n\n   /* Split the array into two */\n   var l1 as array = a[0] ... a[n/2]\n   var l2 as array = a[n/2+1] ... a[n]\n\n   l1 = mergesort( l1 )\n   l2 = mergesort( l2 )\n\n   return merge( l1, l2 )\nend procedure\n\nprocedure merge( a as array, b as array )\n   var result as array\n   while ( a and b have elements )\n      if ( a[0] \u0026gt; b[0] )\n         add b[0] to the end of result\n         remove b[0] from b\n      else\n         add a[0] to the end of result\n         remove a[0] from a\n      end if\n   end while\n\n   while ( a has elements )\n      add a[0] to the end of result\n      remove a[0] from a\n   end while\n\n   while ( b has elements )\n      add b[0] to the end of result\n      remove b[0] from b\n   end while\n\n   return result\nend procedure\u003c/code\u003e\u003c/pre\u003e\n                \u003ch2 id=\"instructions-3\"\u003eInstructions\u003c/h2\u003e\n                \u003cul\u003e\n                    \u003cli\u003eClone the project from https://github.com/appacademy-starters/algorithms-merge-sort-starter.\n                    \u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ecd\u003c/code\u003e into the project folder\u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003enpm install\u003c/code\u003e to install\n                        dependencies in the project root directory\u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003enpm test\u003c/code\u003e to run the specs\u003c/li\u003e\n                    \u003cli\u003eYou can view the test cases in \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003e/test/test.js\u003c/code\u003e. Your job is to write code in the\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e/lib/merge_sort.js\u003c/code\u003e that\n                        implements the Merge Sort.\n                    \u003c/li\u003e\n                \u003c/ul\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"practice-quick-sort\"\u003ePractice: Quick Sort\u003c/h1\u003e\n                \u003cp\u003eThis project contains a skeleton for you to implement Quick Sort. In the file\n                    \u003cstrong\u003elib/quick_sort.js\u003c/strong\u003e,\n                    you should implement the Quick Sort. This is a description of how the Quick Sort works (and is also\n                    in the\n                    code\n                    file).\n                \u003c/p\u003e\n                \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\"\u003eprocedure quick sort (array)\n  if the length of the array is 0 or 1, return the array\n\n  set the pivot to the first element of the array\n  remove the first element of the array\n\n  put all values less than the pivot value into an array called left\n  put all values greater than the pivot value into an array called right\n\n  call quick sort on left and assign the return value to leftSorted\n  call quick sort on right and assign the return value to rightSorted\n\n  return the concatenation of leftSorted, the pivot value, and rightSorted\nend procedure quick sort\u003c/code\u003e\u003c/pre\u003e\n                \u003ch2 id=\"instructions-4\"\u003eInstructions\u003c/h2\u003e\n                \u003cul\u003e\n                    \u003cli\u003eClone the project from https://github.com/appacademy-starters/algorithms-quick-sort-starter.\n                    \u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ecd\u003c/code\u003e into the project folder\u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003enpm install\u003c/code\u003e to install\n                        dependencies in the project root directory\u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003enpm test\u003c/code\u003e to run the specs\u003c/li\u003e\n                    \u003cli\u003eYou can view the test cases in \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003e/test/test.js\u003c/code\u003e. Your job is to write code in the\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e/lib/quick_sort.js\u003c/code\u003e that\n                        implements the Quick Sort.\n                    \u003c/li\u003e\n                \u003c/ul\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"practice-binary-search\"\u003ePractice: Binary Search\u003c/h1\u003e\n                \u003cp\u003eThis project contains a skeleton for you to implement Binary Search. In the file\n                    \u003cstrong\u003elib/binary_search.js\u003c/strong\u003e, you should implement the Binary Search and its cousin Binary\n                    Search\n                    Index.\n                \u003c/p\u003e\n                \u003cp\u003eThe Binary Search algorithm can be summarized as the following:\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eIf the array is empty, then return false\u003c/li\u003e\n                    \u003cli\u003eCheck the value in the middle of the array against the target value\u003c/li\u003e\n                    \u003cli\u003eIf the value is equal to the target value, then return true\u003c/li\u003e\n                    \u003cli\u003eIf the value is less than the target value, then return the binary search on the left half of\n                        the array\n                        for\n                        the target\u003c/li\u003e\n                    \u003cli\u003eIf the value is greater than the target value, then return the binary search on the right half\n                        of the\n                        array\n                        for the target\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003cp\u003eThis is a description of how the Binary Search works (and is also in the code file).\u003c/p\u003e\n                \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\"\u003eprocedure binary search (list, target)\n  parameter list: a list of sorted value\n  parameter target: the value to search for\n\n  if the list has zero length, then return false\n\n  determine the slice point:\n    if the list has an even number of elements,\n      the slice point is the number of elements\n      divided by two\n    if the list has an odd number of elements,\n      the slice point is the number of elements\n      minus one divided by two\n\n  create an list of the elements from 0 to the\n    slice point, not including the slice point,\n    which is known as the \u0026quot;left half\u0026quot;\n  create an list of the elements from the\n    slice point to the end of the list which is\n    known as the \u0026quot;right half\u0026quot;\n\n  if the target is less than the value in the\n    original array at the slice point, then\n    return the binary search of the \u0026quot;left half\u0026quot;\n    and the target\n  if the target is greater than the value in the\n    original array at the slice point, then\n    return the binary search of the \u0026quot;right half\u0026quot;\n    and the target\n  if neither of those is true, return true\nend procedure binary search\u003c/code\u003e\u003c/pre\u003e\n                \u003cp\u003eThen you need to adapt that to return \u003cem\u003ethe index\u003c/em\u003e of the found item rather than a Boolean\n                    value. The\n                    pseudocode is also in the code file.\u003c/p\u003e\n                \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\"\u003eprocedure binary search index(list, target, low, high)\n  parameter list: a list of sorted value\n  parameter target: the value to search for\n  parameter low: the lower index for the search\n  parameter high: the upper index for the search\n\n  if low is equal to high, then return -1 to indicate\n    that the value was not found\n\n  determine the slice point:\n    if the list between the high index and the low index\n    has an even number of elements,\n      the slice point is the number of elements\n      between high and low divided by two\n    if the list between the high index and the low index\n    has an odd number of elements,\n      the slice point is the number of elements\n      between high and low minus one, divided by two\n\n  if the target is less than the value in the\n    original array at the slice point, then\n    return the binary search of the array,\n    the target, low, and the slice point\n  if the target is greater than the value in the\n    original array at the slice point, then return\n    the binary search of the array, the target,\n    the slice point plus one, and high\n  if neither of those is true, return the slice point\nend procedure binary search index\u003c/code\u003e\u003c/pre\u003e\n                \u003ch2 id=\"instructions-5\"\u003eInstructions\u003c/h2\u003e\n                \u003cul\u003e\n                    \u003cli\u003eClone the project from https://github.com/appacademy-starters/algorithms-binary-search-starter.\n                    \u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ecd\u003c/code\u003e into the project folder\u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003enpm install\u003c/code\u003e to install\n                        dependencies in the project root directory\u003c/li\u003e\n                    \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003enpm test\u003c/code\u003e to run the specs\u003c/li\u003e\n                    \u003cli\u003eYou can view the test cases in \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003e/test/test.js\u003c/code\u003e. Your job is to write code in the\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e/lib/binary_search.js\u003c/code\u003e that\n                        implements the Binary Search and Binary Search Index.\n                    \u003c/li\u003e\n                \u003c/ul\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"week-07-day-4-lists-stacks-queues\" data-ignore=\"true\"\u003eWEEK-07 DAY-4\u003cbr\u003e\u003cem\u003eLists, Stacks,\n                        Queues\u003c/em\u003e\n                \u003c/h1\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"lists-stacks-and-queues-\"\u003eLists, Stacks, and Queues \u003c/h1\u003e\n                \u003cp\u003e\u003cstrong\u003eThe objective of this lesson\u003c/strong\u003e is for you to become comfortable with implementing\n                    common data\n                    structures. This is important because questions about data structures are incredibly likely to be\n                    interview\n                    questions for software engineers from junior to senior levels. Moreover, understanding how different\n                    data\n                    structures work will influence the libraries and frameworks that you choose when writing software.\n                \u003c/p\u003e\n                \u003cp\u003eWhen you are done, you will be able to:\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eExplain and implement a List.\u003c/li\u003e\n                    \u003cli\u003eExplain and implement a Stack.\u003c/li\u003e\n                    \u003cli\u003eExplain and implement a Queue.me comfortable with implementing common data structures. This is\n                        important\n                        because questions about data structures are incredibly likely to be interview questions for\n                        software\n                        engineers from junior to senior levels. Moreover, understanding how different data structures\n                        work will\n                        influence the libraries and frameworks that you choose when writing software.\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003cp\u003eWhen you are done, you will be able to:\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eExplain and implement a List.\u003c/li\u003e\n                    \u003cli\u003eExplain and implement a Stack.\u003c/li\u003e\n                    \u003cli\u003eExplain and implement a Queue.\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"linked-lists\"\u003eLinked Lists\u003c/h1\u003e\n                \u003cp\u003eIn the university setting, it's common for Linked Lists to appear early on in an undergraduate's\n                    Computer\n                    Science\n                    coursework. While they don't always have the most practical real-world applications in industry,\n                    Linked\n                    Lists\n                    make for an important and effective educational tool in helping develop a student's mental model on\n                    what\n                    data\n                    structures actually are to begin with.\u003c/p\u003e\n                \u003cp\u003eLinked lists are simple. They have many compelling, reoccurring edge cases to consider that emphasize\n                    to the\n                    student the need for care and intent while implementing data structures. They can be applied as the\n                    underlying\n                    data structure while implementing a variety of other prevalent abstract data types, such as Lists,\n                    Stacks,\n                    and\n                    Queues, and they have a level of versatility high enough to clearly illustrate the value of the\n                    Object\n                    Oriented\n                    Programming paradigm.\u003c/p\u003e\n                \u003cp\u003eThey also come up in software engineering interviews quite often.\u003c/p\u003e\n                \u003ch2 id=\"what-is-a-linked-list\"\u003eWhat is a Linked List?\u003c/h2\u003e\n                \u003cp\u003eA Linked List data structure represents a linear sequence of \"vertices\" (or \"nodes\"), and tracks\n                    three\n                    important\n                    properties.\u003c/p\u003e\n                \u003cp align=\"center\"\u003e\n                    \u003cb\u003eLinked List Properties:\u003c/b\u003e\n                \u003c/p\u003e\n                \u003ctable\u003e\n                    \u003cthead\u003e\n                        \u003ctr class=\"header\"\u003e\n                            \u003cth style=\"text-align: center;\"\u003eProperty\u003c/th\u003e\n                            \u003cth style=\"text-align: center;\"\u003eDescription\u003c/th\u003e\n                        \u003c/tr\u003e\n                    \u003c/thead\u003e\n                    \u003ctbody\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003ehead\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eThe first node in the list.\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003etail\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eThe last node in the list.\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003elength\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eThe number of nodes in the list; the list's length.\u003c/td\u003e\n                        \u003c/tr\u003e\n                    \u003c/tbody\u003e\n                \u003c/table\u003e\n                \u003cp\u003eThe data being tracked by a particular Linked List does not live inside the Linked List instance\n                    itself.\n                    Instead,\n                    each vertex is actually an instance of an even simpler, smaller data structure, often referred to as\n                    a\n                    \"Node\".\n                \u003c/p\u003e\n                \u003cp\u003eDepending on the type of Linked List (there are many), Node instances track some very important\n                    properties as\n                    well.\u003c/p\u003e\n                \u003cp align=\"center\"\u003e\n                    \u003cb\u003eLinked List Node Properties:\u003c/b\u003e\n                \u003c/p\u003e\n                \u003ctable\u003e\n                    \u003cthead\u003e\n                        \u003ctr class=\"header\"\u003e\n                            \u003cth style=\"text-align: center;\"\u003eProperty\u003c/th\u003e\n                            \u003cth style=\"text-align: center;\"\u003eDescription\u003c/th\u003e\n                        \u003c/tr\u003e\n                    \u003c/thead\u003e\n                    \u003ctbody\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003evalue\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eThe actual value this node represents.\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003enext\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eThe next node in the list (relative to this node).\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003eprevious\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eThe previous node in the list (relative to this node).\u003c/td\u003e\n                        \u003c/tr\u003e\n                    \u003c/tbody\u003e\n                \u003c/table\u003e\n                \u003cp align=\"center\"\u003e\n                    \u003cb\u003eNOTE:\u003c/b\u003e The \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eprevious\u003c/code\u003e property\n                    is for Doubly Linked Lists only!\n                \u003c/p\u003e\n                \u003cp\u003eLinked Lists contain \u003cem\u003eordered\u003c/em\u003e data, just like arrays. The first node in the list is, indeed,\n                    first.\n                    From\n                    the perspective of the very first node in the list, the \u003cem\u003enext\u003c/em\u003e node is the second node. From\n                    the\n                    perspective of the second node in the list, the \u003cem\u003eprevious\u003c/em\u003e node is the first node, and the\n                    \u003cem\u003enext\u003c/em\u003e\n                    node is the third node. And so it goes.\n                \u003c/p\u003e\n                \u003ch4 id=\"sothis-sounds-a-lot-like-an-array\"\u003e\u003cem\u003e\"So…this sounds a lot like an Array…\"\u003c/em\u003e\u003c/h4\u003e\n                \u003cp\u003eAdmittedly, this does \u003cem\u003esound\u003c/em\u003e a lot like an Array so far, and that's because Arrays and Linked\n                    Lists\n                    are\n                    both implementations of the List ADT. However, there is an incredibly important distinction to be\n                    made\n                    between\n                    Arrays and Linked Lists, and that is how they \u003cem\u003ephysically store\u003c/em\u003e their data. (As opposed to\n                    how they\n                    \u003cem\u003erepresent\u003c/em\u003e the order of their data.)\n                \u003c/p\u003e\n                \u003cp\u003eRecall that Arrays contain \u003cem\u003econtiguous\u003c/em\u003e data. Each element of an array is actually stored\n                    \u003cem\u003enext\n                        to\u003c/em\u003e\n                    it's neighboring element \u003cem\u003ein the actual hardware of your machine\u003c/em\u003e, in a single continuous\n                    block in\n                    memory.\n                \u003c/p\u003e\n                \u003cimg src=\"images/array-in-memory.png\" alt=\"Array in Memory\" /\u003e\n                \u003cp align=\"center\"\u003e\n                    \u003ci\u003eAn Array's contiguous data being stored in a continuous block of addresses in memory.\u003c/i\u003e\n                \u003c/p\u003e\n                \u003cp\u003e\u003cbr\u003e\u003c/p\u003e\n                \u003cp\u003eUnlike Arrays, Linked Lists contain \u003cem\u003enon-contiguous\u003c/em\u003e data. Though Linked Lists\n                    \u003cem\u003erepresent\u003c/em\u003e data\n                    that is ordered linearly, that mental model is just that - an interpretation of the\n                    \u003cem\u003erepresentation\u003c/em\u003e\n                    of\n                    information, not reality.\n                \u003c/p\u003e\n                \u003cp\u003eIn reality, in the actual hardware of your machine, whether it be in disk or in memory, a Linked\n                    List's Nodes\n                    are\n                    not stored in a single continuous block of addresses. Rather, Linked List Nodes live at randomly\n                    distributed\n                    addresses throughout your machine! The only reason we know which node comes next in the list is\n                    because\n                    we've\n                    assigned its reference to the current node's \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\"\u003enext\u003c/code\u003e pointer.\u003c/p\u003e\n                \u003cimg src=\"images/SLL-diagram.png\" alt=\"Array in Memory\" /\u003e\n                \u003cp align=\"center\"\u003e\n                    \u003ci\u003eA Singly Linked List's non-contiguous data (Nodes) being stored at randomly distributed addresses\n                        in\n                        memory.\u003c/i\u003e\n                \u003c/p\u003e\n                \u003cp\u003e\u003cbr\u003e\u003c/p\u003e\n                \u003cp\u003eFor this reason, Linked List Nodes have \u003cem\u003eno indices\u003c/em\u003e, and no \u003cem\u003erandom access\u003c/em\u003e. Without\n                    random\n                    access, we do not have the ability to look up an individual Linked List Node in constant time.\n                    Instead, to\n                    find\n                    a particular Node, we have to start at the very first Node and iterate through the Linked List one\n                    node at a\n                    time, checking each Node's \u003cem\u003enext\u003c/em\u003e Node until we find the one we're interested in.\u003c/p\u003e\n                \u003cp\u003eSo when implementing a Linked List, we actually must implement both the Linked List class\n                    \u003cem\u003eand\u003c/em\u003e the\n                    Node\n                    class. Since the actual data lives in the Nodes, it's simpler to implement the Node class first.\n                \u003c/p\u003e\n                \u003ch2 id=\"types-of-linked-lists\"\u003eTypes of Linked Lists\u003c/h2\u003e\n                \u003cp\u003eThere are four flavors of Linked List you should be familiar with when walking into your job\n                    interviews.\u003c/p\u003e\n                \u003cp align=\"center\"\u003e\n                    \u003cb\u003eLinked List Types:\u003c/b\u003e\n                \u003c/p\u003e\n                \u003ctable\u003e\n                    \u003ccolgroup\u003e\n                        \u003ccol style=\"width: 16%\" /\u003e\n                        \u003ccol style=\"width: 62%\" /\u003e\n                        \u003ccol style=\"width: 21%\" /\u003e\n                    \u003c/colgroup\u003e\n                    \u003cthead\u003e\n                        \u003ctr class=\"header\"\u003e\n                            \u003cth style=\"text-align: center;\"\u003eList Type\u003c/th\u003e\n                            \u003cth style=\"text-align: center;\"\u003eDescription\u003c/th\u003e\n                            \u003cth style=\"text-align: center;\"\u003eDirectionality\u003c/th\u003e\n                        \u003c/tr\u003e\n                    \u003c/thead\u003e\n                    \u003ctbody\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eSingly Linked\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eNodes have a single pointer connecting them in a single\n                                direction.\n                            \u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eHead→Tail\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eDoubly Linked\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eNodes have two pointers connecting them bi-directionally.\n                            \u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eHead⇄Tail\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eMultiply Linked\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eNodes have two or more pointers, providing a variety of\n                                potential\n                                node\n                                orderings.\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eHead⇄Tail, A→Z, Jan→Dec, etc.\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eCircularly Linked\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eFinal node's \u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003enext\u003c/code\u003e pointer points to the first\n                                node,\n                                creating a non-linear, circular version of a Linked List.\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eHead→Tail→Head→Tail\u003c/td\u003e\n                        \u003c/tr\u003e\n                    \u003c/tbody\u003e\n                \u003c/table\u003e\n                \u003cp align=\"center\"\u003e\n                    \u003cb\u003eNOTE:\u003c/b\u003e These Linked List types are not always mutually exclusive.\n                \u003c/p\u003e\n                \u003cp\u003eFor instance:\u003c/p\u003e\n                \u003cul\u003e\n                    \u003cli\u003eAny type of Linked List can be implemented Circularly (e.g. A Circular Doubly Linked List).\u003c/li\u003e\n                    \u003cli\u003eA Doubly Linked List is actually just a special case of a Multiply Linked List.\u003c/li\u003e\n                \u003c/ul\u003e\n                \u003cp\u003eYou are most likely to encounter Singly and Doubly Linked Lists in your upcoming job search, so we\n                    are going\n                    to\n                    focus exclusively on those two moving forward. However, in more senior level interviews, it is very\n                    valuable\n                    to\n                    have some familiarity with the other types of Linked Lists. Though you may not actually code them\n                    out,\n                    \u003cem\u003eyou\n                        will win extra points by illustrating your ability to weigh the tradeoffs of your technical\n                        decisions\u003c/em\u003e\n                    by discussing how your choice of Linked List type may affect the efficiency of the solutions you\n                    propose.\n                \u003c/p\u003e\n                \u003ch2 id=\"linked-list-methods\"\u003eLinked List Methods\u003c/h2\u003e\n                \u003cp\u003eLinked Lists are great foundation builders when learning about data structures because they share a\n                    number of\n                    similar methods (and edge cases) with many other common data structures. You will find that many of\n                    the\n                    concepts\n                    discussed here will repeat themselves as we dive into some of the more complex non-linear data\n                    structures\n                    later\n                    on, like Trees and Graphs.\u003c/p\u003e\n                \u003cp\u003eIn the project that follows, we will implement the following Linked List methods:\u003c/p\u003e\n                \u003ctable style=\"width:100%;\"\u003e\n                    \u003ccolgroup\u003e\n                        \u003ccol style=\"width: 7%\" /\u003e\n                        \u003ccol style=\"width: 10%\" /\u003e\n                        \u003ccol style=\"width: 66%\" /\u003e\n                        \u003ccol style=\"width: 15%\" /\u003e\n                    \u003c/colgroup\u003e\n                    \u003cthead\u003e\n                        \u003ctr class=\"header\"\u003e\n                            \u003cth style=\"text-align: center;\"\u003eType\u003c/th\u003e\n                            \u003cth style=\"text-align: center;\"\u003eName\u003c/th\u003e\n                            \u003cth style=\"text-align: center;\"\u003eDescription\u003c/th\u003e\n                            \u003cth style=\"text-align: center;\"\u003eReturns\u003c/th\u003e\n                        \u003c/tr\u003e\n                    \u003c/thead\u003e\n                    \u003ctbody\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eInsertion\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003eaddToTail\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eAdds a new node to the tail of the Linked List.\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eUpdated Linked List\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eInsertion\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003eaddToHead\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eAdds a new node to the head of the Linked List.\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eUpdated Linked List\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eInsertion\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003einsertAt\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eInserts a new node at the \"index\", or position, specified.\n                            \u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eBoolean\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eDeletion\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003eremoveTail\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eRemoves the node at the tail of the Linked List.\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eRemoved node\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eDeletion\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003eremoveHead\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eRemoves the node at the head of the Linked List.\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eRemoved node\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eDeletion\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003eremoveFrom\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eRemoves the node at the \"index\", or position, specified.\n                            \u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eRemoved node\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eSearch\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003econtains\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eSearches the Linked List for a node with the value\n                                specified.\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eBoolean\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eAccess\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003eget\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eGets the node at the \"index\", or position, specified.\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eNode at index\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eAccess\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003eset\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eUpdates the value of a node at the \"index\", or position,\n                                specified.\n                            \u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eBoolean\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eMeta\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003esize\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eReturns the current size of the Linked List.\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eInteger\u003c/td\u003e\n                        \u003c/tr\u003e\n                    \u003c/tbody\u003e\n                \u003c/table\u003e\n                \u003ch2 id=\"time-and-space-complexity-analysis-4\"\u003eTime and Space Complexity Analysis\u003c/h2\u003e\n                \u003cp\u003eBefore we begin our analysis, here is a quick summary of the Time and Space constraints of each\n                    Linked List\n                    Operation. The complexities below apply to both Singly and Doubly Linked Lists:\u003c/p\u003e\n                \u003ctable\u003e\n                    \u003ccolgroup\u003e\n                        \u003ccol style=\"width: 26%\" /\u003e\n                        \u003ccol style=\"width: 22%\" /\u003e\n                        \u003ccol style=\"width: 25%\" /\u003e\n                        \u003ccol style=\"width: 26%\" /\u003e\n                    \u003c/colgroup\u003e\n                    \u003cthead\u003e\n                        \u003ctr class=\"header\"\u003e\n                            \u003cth style=\"text-align: center;\"\u003eData Structure Operation\u003c/th\u003e\n                            \u003cth style=\"text-align: center;\"\u003eTime Complexity (Avg)\u003c/th\u003e\n                            \u003cth style=\"text-align: center;\"\u003eTime Complexity (Worst)\u003c/th\u003e\n                            \u003cth style=\"text-align: center;\"\u003eSpace Complexity (Worst)\u003c/th\u003e\n                        \u003c/tr\u003e\n                    \u003c/thead\u003e\n                    \u003ctbody\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eAccess\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003eΘ(n)\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003eO(n)\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003eO(n)\u003c/code\u003e\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eSearch\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003eΘ(n)\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003eO(n)\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003eO(n)\u003c/code\u003e\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"odd\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eInsertion\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003eΘ(1)\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003eO(1)\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003eO(n)\u003c/code\u003e\u003c/td\u003e\n                        \u003c/tr\u003e\n                        \u003ctr class=\"even\"\u003e\n                            \u003ctd style=\"text-align: center;\"\u003eDeletion\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003eΘ(1)\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003eO(1)\u003c/code\u003e\u003c/td\u003e\n                            \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                    id=\"button\"\u003eO(n)\u003c/code\u003e\u003c/td\u003e\n                        \u003c/tr\u003e\n                    \u003c/tbody\u003e\n                \u003c/table\u003e\n                \u003cp\u003eBefore moving forward, see if you can reason to yourself why each operation has the time and space\n                    complexity\n                    listed above!\u003c/p\u003e\n                \u003ch2 id=\"time-complexity---access-and-search\"\u003eTime Complexity - Access and Search:\u003c/h2\u003e\n                \u003ch3 id=\"scenarios\"\u003eScenarios:\u003c/h3\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eWe have a Linked List, and we'd like to find the 8th item in the list.\u003c/li\u003e\n                    \u003cli\u003eWe have a Linked List of sorted alphabet letters, and we'd like to see if the letter \"Q\" is\n                        inside that\n                        list.\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003ch3 id=\"discussion\"\u003eDiscussion:\u003c/h3\u003e\n                \u003cp\u003eUnlike Arrays, Linked Lists Nodes are not stored contiguously in memory, and thereby do not have an\n                    indexed\n                    set\n                    of memory addresses at which we can quickly lookup individual nodes in constant time. Instead, we\n                    must begin\n                    at\n                    the head of the list (or possibly at the tail, if we have a Doubly Linked List), and iterate through\n                    the\n                    list\n                    until we arrive at the node of interest.\u003c/p\u003e\n                \u003cp\u003eIn Scenario 1, we'll know we're there because we've iterated 8 times. In Scenario 2, we'll know we're\n                    there\n                    because, while iterating, we've checked each node's value and found one that matches our target\n                    value, \"Q\".\n                \u003c/p\u003e\n                \u003cp\u003eIn the worst case scenario, we may have to traverse the entire Linked List until we arrive at the\n                    final node.\n                    This makes both Access \u0026amp; Search \u003cstrong\u003eLinear Time\u003c/strong\u003e operations.\u003c/p\u003e\n                \u003ch2 id=\"time-complexity---insertion-and-deletion\"\u003eTime Complexity - Insertion and Deletion:\u003c/h2\u003e\n                \u003ch3 id=\"scenarios-1\"\u003eScenarios:\u003c/h3\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eWe have an empty Linked List, and we'd like to insert our first node.\u003c/li\u003e\n                    \u003cli\u003eWe have a Linked List, and we'd like to insert or delete a node at the Head or Tail.\u003c/li\u003e\n                    \u003cli\u003eWe have a Linked List, and we'd like to insert or delete a node from somewhere in the middle of\n                        the\n                        list.\n                    \u003c/li\u003e\n                \u003c/ol\u003e\n                \u003ch3 id=\"discussion-1\"\u003eDiscussion:\u003c/h3\u003e\n                \u003cp\u003eSince we have our Linked List Nodes stored in a non-contiguous manner that relies on pointers to keep\n                    track\n                    of\n                    where the next and previous nodes live, Linked Lists liberate us from the linear time nature of\n                    Array\n                    insertions\n                    and deletions. We no longer have to adjust the position at which each node/element is stored after\n                    making an\n                    insertion at a particular position in the list. Instead, if we want to insert a new node at position\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ei\u003c/code\u003e, we can simply:\n                \u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eCreate a new node.\u003c/li\u003e\n                    \u003cli\u003eSet the new node's \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003enext\u003c/code\u003e and\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eprevious\u003c/code\u003e pointers to the nodes\n                        that live\n                        at\n                        positions\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ei\u003c/code\u003e and \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003ei - 1\u003c/code\u003e, respectively.\n                    \u003c/li\u003e\n                    \u003cli\u003eAdjust the \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003enext\u003c/code\u003e pointer of the\n                        node that lives at position \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ei -\n                            1\u003c/code\u003e to\n                        point to\n                        the\n                        new node.\u003c/li\u003e\n                    \u003cli\u003eAdjust the \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eprevious\u003c/code\u003e pointer of\n                        the node that lives at position \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003ei\u003c/code\u003e to\n                        point to\n                        the\n                        new node.\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003cp\u003eAnd we're done, in Constant Time. No iterating across the entire list necessary.\u003c/p\u003e\n                \u003cp\u003e\"But hold on one second,\" you may be thinking. \"In order to insert a new node in the middle of the\n                    list,\n                    don't we\n                    have to lookup its position? Doesn't that take linear time?!\"\u003c/p\u003e\n                \u003cp\u003eYes, it is tempting to call insertion or deletion in the middle of a Linked List a linear time\n                    operation\n                    since\n                    there is lookup involved. However, it's usually the case that you'll already have a reference to the\n                    node\n                    where\n                    your desired insertion or deletion will occur.\u003c/p\u003e\n                \u003cp\u003eFor this reason, we separate the Access time complexity from the Insertion/Deletion time complexity,\n                    and\n                    formally\n                    state that Insertion and Deletion in a Linked List are \u003cstrong\u003eConstant Time\u003c/strong\u003e across the\n                    board.\u003c/p\u003e\n                \u003ch3 id=\"note\"\u003eNOTE:\u003c/h3\u003e\n                \u003cp\u003eWithout a reference to the node at which an insertion or deletion will occur, due to linear time\n                    lookup, an\n                    insertion or deletion \u003cem\u003ein the middle\u003c/em\u003e of a Linked List will still take Linear Time, sum\n                    total.\u003c/p\u003e\n                \u003ch2 id=\"space-complexity-1\"\u003eSpace Complexity:\u003c/h2\u003e\n                \u003ch3 id=\"scenarios-2\"\u003eScenarios:\u003c/h3\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eWe're given a Linked List, and need to operate on it.\u003c/li\u003e\n                    \u003cli\u003eWe've decided to create a new Linked List as part of strategy to solve some problem.\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003ch3 id=\"discussion-2\"\u003eDiscussion:\u003c/h3\u003e\n                \u003cp\u003eIt's obvious that Linked Lists have one node for every one item in the list, and for that reason we\n                    know that\n                    Linked Lists take up Linear Space in memory. However, when asked in an interview setting what the\n                    Space\n                    Complexity \u003cem\u003eof your solution\u003c/em\u003e to a problem is, it's important to recognize the difference\n                    between the\n                    two\n                    scenarios above.\u003c/p\u003e\n                \u003cp\u003eIn Scenario 1, we \u003cem\u003eare not\u003c/em\u003e creating a new Linked List. We simply need to operate on the one\n                    given.\n                    Since\n                    we are not storing a \u003cem\u003enew\u003c/em\u003e node for every node represented in the Linked List we are\n                    provided, our\n                    solution is \u003cem\u003enot necessarily\u003c/em\u003e linear in space.\u003c/p\u003e\n                \u003cp\u003eIn Scenario 2, we \u003cem\u003eare\u003c/em\u003e creating a new Linked List. If the number of nodes we create is\n                    linearly\n                    correlated to the size of our input data, we are now operating in Linear Space.\u003c/p\u003e\n                \u003ch3 id=\"note-1\"\u003eNOTE:\u003c/h3\u003e\n                \u003cp\u003eLinked Lists can be traversed both iteratively and recursively. \u003cem\u003eIf you choose to traverse a\n                        Linked List\n                        recursively\u003c/em\u003e, there will be a recursive function call added to the call stack for every node\n                    in the\n                    Linked List. Even if you're provided the Linked List, as in Scenario 1, you will still use Linear\n                    Space in\n                    the\n                    call stack, and that counts.\u003c/p\u003e\n                \u003chr /\u003e\n                \u003ch1 id=\"stacks-and-queues\"\u003eStacks and Queues\u003c/h1\u003e\n                \u003cp\u003eStacks and Queues aren't really \"data structures\" by the strict definition of the term. The more\n                    appropriate\n                    terminology would be to call them abstract data types (ADTs), meaning that their definitions are\n                    more\n                    conceptual\n                    and related to the rules governing their user-facing behaviors rather than their core\n                    implementations.\u003c/p\u003e\n                \u003cp\u003eFor the sake of simplicity, we'll refer to them as data structures and ADTs interchangeably\n                    throughout the\n                    course, but the distinction is an important one to be familiar with as you level up as an engineer.\n                \u003c/p\u003e\n                \u003cp\u003eNow that that's out of the way, Stacks and Queues represent a linear collection of nodes or values.\n                    In this\n                    way,\n                    they are quite similar to the Linked List data structure we discussed in the previous section. In\n                    fact, you\n                    can\n                    even use a modified version of a Linked List to implement each of them. (Hint, hint.)\u003c/p\u003e\n                \u003cp\u003eThese two ADTs are similar to each other as well, but each obey their own special rule regarding the\n                    order\n                    with\n                    which Nodes can be added and removed from the structure.\u003c/p\u003e\n                \u003cp\u003eSince we've covered Linked Lists in great length, these two data structures will be quick and easy.\n                    Let's\n                    break\n                    them down individually in the next couple of sections.\u003c/p\u003e\n                \u003ch2 id=\"what-is-a-stack\"\u003eWhat is a Stack?\u003c/h2\u003e\n                \u003cp\u003eStacks are a Last In First Out (LIFO) data structure. The last Node added to a stack is always the\n                    first Node\n                    to\n                    be removed, and as a result, the first Node added is always the last Node removed.\u003c/p\u003e\n                \u003cp\u003eThe name Stack actually comes from this characteristic, as it is helpful to visualize the data\n                    structure as a\n                    vertical stack of items. Personally, I like to think of a Stack as a stack of plates, or a stack of\n                    sheets\n                    of\n                    paper. This seems to make them more approachable, because the analogy relates to something in our\n                    everyday\n                    lives.\u003c/p\u003e\n                \u003cp\u003eIf you can imagine adding items to, or removing items from, a Stack of…literally anything…you'll\n                    realize that\n                    every (sane) person naturally obeys the LIFO rule.\u003c/p\u003e\n                \u003cp\u003eWe add things to the \u003cem\u003etop\u003c/em\u003e of a stack. We remove things from the \u003cem\u003etop\u003c/em\u003e of a stack. We\n                    never add\n                    things to, or remove things from, the \u003cem\u003ebottom\u003c/em\u003e of the stack. That's just crazy.\u003c/p\u003e\n                \u003cp\u003eNote: We can use JavaScript Arrays to implement a basic stack. \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003eArray#push\u003c/code\u003e adds to the\n                    top of the\n                    stack and \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eArray#pop\u003c/code\u003e will remove from\n                    the top of the stack. In the exercise that\n                    follows, we'll\n                    build our own Stack class from scratch (without using any arrays). In an interview setting, your\n                    evaluator\n                    may\n                    be okay with you using an array as a stack.\u003c/p\u003e\n                \u003ch2 id=\"what-is-a-queue\"\u003eWhat is a Queue?\u003c/h2\u003e\n                \u003cp\u003eQueues are a First In First Out (FIFO) data structure. The first Node added to the queue is always\n                    the first\n                    Node\n                    to be removed.\u003c/p\u003e\n                \u003cp\u003eThe name Queue comes from this characteristic, as it is helpful to visualize this data structure as a\n                    horizontal\n                    line of items with a beginning and an end. Personally, I like to think of a Queue as the line one\n                    waits on\n                    for\n                    an amusement park, at a grocery store checkout, or to see the teller at a bank.\u003c/p\u003e\n                \u003cp\u003eIf you can imagine a queue of humans waiting…again, for literally anything…you'll realize that\n                    \u003cem\u003emost\u003c/em\u003e\n                    people (the civil ones) naturally obey the FIFO rule.\n                \u003c/p\u003e\n                \u003cp\u003ePeople add themselves to the \u003cem\u003eback\u003c/em\u003e of a queue, wait their turn in line, and make their way\n                    toward the\n                    \u003cem\u003efront\u003c/em\u003e. People exit from the \u003cem\u003efront\u003c/em\u003e of a queue, but only when they have made their\n                    way to\n                    being\n                    first in line.\n                \u003c/p\u003e\n                \u003cp\u003eWe never add ourselves to the front of a queue (unless there is no one else in line), otherwise we\n                    would be\n                    \"cutting\" the line, and other humans don't seem to appreciate that.\u003c/p\u003e\n                \u003cp\u003eNote: We can use JavaScript Arrays to implement a basic queue. \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\"\u003eArray#push\u003c/code\u003e adds to the\n                    back\n                    (enqueue)\n                    and \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eArray#shift\u003c/code\u003e will remove from the\n                    front (dequeue). In the exercise that follows,\n                    we'll build\n                    our\n                    own Queue class from scratch (without using any arrays). In an interview setting, your evaluator may\n                    be okay\n                    with you using an array as a queue.\u003c/p\u003e\n\n\n\n                \u003ch1 id=\"reverse-part-of-a-linked-list-via-recusion\"\u003eReverse Part of a Linked List via Recusion\u003c/h1\u003e\n                \u003cp\u003e\u003cstrong\u003eTranslator: \u003ca href=\"https://github.com/CarrieOn\"\u003eCarrieOn\u003c/a\u003e\u003c/strong\u003e\u003c/p\u003e\n                \u003cp\u003e\u003cstrong\u003eAuthor: \u003ca href=\"https://github.com/labuladong\"\u003elabuladong\u003c/a\u003e\u003c/strong\u003e\u003c/p\u003e\n                \u003cp\u003eIt's easy to reverse a single linked list using iteration, however it's kind of difficult to come up\n                    with a\n                    recursive solution. Furthermore, if only part of a linked list needs reversed, can you nail it with\n                    \u003cstrong\u003erecursion\u003c/strong\u003e?\n                \u003c/p\u003e\n                \u003cp\u003eIf you haven't known how to \u003cstrong\u003erecursively reverse a single linked list\u003c/strong\u003e, no worry, we\n                    will\n                    start\n                    right here and guide you step by step to a deeper level.\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb1\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\" data-filter-output=\"(out)\"\n                        class=\"sourceCode java\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode java\"\u003e\u003ca class=\"sourceLine\" id=\"cb1-1\" title=\"1\"\u003e\u003cspan class=\"lang-js co\"\u003e// node structure for a single linked list \u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-2\" title=\"2\"\u003e\u003cspan class=\"lang-js kw\"\u003epublic\u003c/span\u003e \u003cspan class=\"lang-js kw\"\u003eclass\u003c/span\u003e ListNode {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-3\" title=\"3\"\u003e    \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e val;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-4\" title=\"4\"\u003e    ListNode next;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-5\" title=\"5\"\u003e    \u003cspan class=\"lang-js fu\"\u003eListNode\u003c/span\u003e(\u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e x) { val = x; }\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb1-6\" title=\"6\"\u003e}\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003e\u003cbr\u003e\u003c/p\u003e\n                \u003cp\u003eTo reverse part of a linked list means we only reverse elements in a specific interval and leave\n                    others\n                    untouched.\u003c/p\u003e\n                \u003cp\u003e\u003cimg src=\"../pictures/reverse_linked_list/title.png\" /\u003e\u003c/p\u003e\n                \u003cp\u003eNote: \u003cstrong\u003eIndex starts from 1\u003c/strong\u003e. Two loops needed if solve via iteration: use one for-loop\n                    to find\n                    the\n                    mth element, and then use another for-loop to reverse elements between m and n. While in recursive\n                    solution,\n                    no\n                    loop at all.\u003c/p\u003e\n                \u003cp\u003eThough iterative solution looks simple, you have to be careful with the details. On the contrary,\n                    recursive\n                    solution is quite elegant. Let's start reversing a whole single linked list in the recursive way.\n                \u003c/p\u003e\n                \u003ch3 id=\"recursively-reverse-a-whole-single-linked-list\"\u003e1. Recursively reverse a whole single Linked\n                    List\u003c/h3\u003e\n                \u003cp\u003eYou may have already known the solution below.\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb2\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\" data-filter-output=\"(out)\"\n                        class=\"sourceCode java\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode java\"\u003e\u003ca class=\"sourceLine\" id=\"cb2-1\" title=\"1\"\u003eListNode \u003cspan class=\"lang-js fu\"\u003ereverse\u003c/span\u003e(ListNode head) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-2\" title=\"2\"\u003e    \u003cspan class=\"lang-js kw\"\u003eif\u003c/span\u003e (head.\u003cspan class=\"lang-js fu\"\u003enext\u003c/span\u003e == \u003cspan class=\"lang-js kw\"\u003enull\u003c/span\u003e) \u003cspan class=\"lang-js kw\"\u003ereturn\u003c/span\u003e head;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-3\" title=\"3\"\u003e    ListNode last = \u003cspan class=\"lang-js fu\"\u003ereverse\u003c/span\u003e(head.\u003cspan class=\"lang-js fu\"\u003enext\u003c/span\u003e);\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-4\" title=\"4\"\u003e    head.\u003cspan class=\"lang-js fu\"\u003enext\u003c/span\u003e.\u003cspan class=\"lang-js fu\"\u003enext\u003c/span\u003e = head;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-5\" title=\"5\"\u003e    head.\u003cspan class=\"lang-js fu\"\u003enext\u003c/span\u003e = \u003cspan class=\"lang-js kw\"\u003enull\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-6\" title=\"6\"\u003e    \u003cspan class=\"lang-js kw\"\u003ereturn\u003c/span\u003e last;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb2-7\" title=\"7\"\u003e}\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eDo you feel lost in trying to understand code above? Well, you are not the only one. This algorithm\n                    is often\n                    used\n                    to show how clever and elegant recursion can be. Let's dig into the code together.\u003c/p\u003e\n                \u003cp\u003eFor recursion, \u003cstrong\u003ethe most important thing is to clarify the definition of the recursive\n                        function\u003c/strong\u003e.\n                    Specifically, we define \u003ccode class=\"language-javascript  highlight\" id=\"button\"\n                        class=\"language-javascript\"\u003ereverse\u003c/code\u003e as follows:\u003c/p\u003e\n                \u003cp\u003e\u003cstrong\u003eInput a node \u003ccode class=\"language-javascript  highlight\" id=\"button\"\n                            class=\"language-javascript\"\u003ehead\u003c/code\u003e , we will reverse the list\n                        starting from\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\" class=\"language-javascript\"\u003ehead\u003c/code\u003e\n                        , and return\n                        the new head node.\u003c/strong\u003e\u003c/p\u003e\n                \u003cp\u003eAfter clarifying the definition, we look back at the problem. For example, we want to reverse the\n                    list below:\n                \u003c/p\u003e\n                \u003cp\u003e\u003cimg src=\"../pictures/reverse_linked_list/1.jpg\" /\u003e\u003c/p\u003e\n                \u003cp\u003eSo after calling \u003ccode class=\"language-javascript  highlight\" id=\"button\"\n                        class=\"language-javascript\"\u003ereverse(head)\u003c/code\u003e , recursion happens:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb3\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\" data-filter-output=\"(out)\"\n                        class=\"sourceCode java\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode java\"\u003e\u003ca class=\"sourceLine\" id=\"cb3-1\" title=\"1\"\u003eListNode last = \u003cspan class=\"lang-js fu\"\u003ereverse\u003c/span\u003e(head.\u003cspan class=\"lang-js fu\"\u003enext\u003c/span\u003e);\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eDid you just step into the messy details in recursion? Oops, it's a wrong way, step back now! Focus\n                    on the\n                    recursion definition (which tells you what it does) to understand how recursive code works the\n                    wonder.\u003c/p\u003e\n                \u003cp\u003e\u003cimg src=\"../pictures/reverse_linked_list/2.jpg\" /\u003e\u003c/p\u003e\n                \u003cp\u003eAfter executing \u003ccode class=\"language-javascript  highlight\" id=\"button\"\n                        class=\"language-javascript\"\u003ereverse(head.next)\u003c/code\u003e , the whole linked list\n                    becomes\n                    this:\n                \u003c/p\u003e\n                \u003cp\u003e\u003cimg src=\"../pictures/reverse_linked_list/3.jpg\" /\u003e\u003c/p\u003e\n                \u003cp\u003eAccording to the definition of the recursive function, \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\" class=\"language-javascript\"\u003ereverse\u003c/code\u003e needs\n                    to\n                    return the new head node, so\n                    we use variable \u003ccode class=\"language-javascript  highlight\" id=\"button\"\n                        class=\"language-javascript\"\u003elast\u003c/code\u003e to mark it.\u003c/p\u003e\n                \u003cp\u003eLet's continue cracking the next piece of code:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb4\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\" data-filter-output=\"(out)\"\n                        class=\"sourceCode java\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode java\"\u003e\u003ca class=\"sourceLine\" id=\"cb4-1\" title=\"1\"\u003ehead.\u003cspan class=\"lang-js fu\"\u003enext\u003c/span\u003e.\u003cspan class=\"lang-js fu\"\u003enext\u003c/span\u003e = head;\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003e\u003cimg src=\"../pictures/reverse_linked_list/4.jpg\" /\u003e\u003c/p\u003e\n                \u003cp\u003eLast work to do：\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb5\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\" data-filter-output=\"(out)\"\n                        class=\"sourceCode java\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode java\"\u003e\u003ca class=\"sourceLine\" id=\"cb5-1\" title=\"1\"\u003ehead.\u003cspan class=\"lang-js fu\"\u003enext\u003c/span\u003e = \u003cspan class=\"lang-js kw\"\u003enull\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb5-2\" title=\"2\"\u003e\u003cspan class=\"lang-js kw\"\u003ereturn\u003c/span\u003e last;\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003e\u003cimg src=\"../pictures/reverse_linked_list/5.jpg\" /\u003e\u003c/p\u003e\n                \u003cp\u003eThe whole linked list is successfully reversed now. Amazing, isn't it?\u003c/p\u003e\n                \u003cp\u003eLast but not the least, there are two things in recursion you need to pay attention to:\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eRecursion needs a base case.\u003c/li\u003e\n                \u003c/ol\u003e\n                \u003cp\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\" class=\"language-javascript\"\u003ejava\n                        if(head.next == null) return head;\u003c/code\u003e\u003c/p\u003e\n                \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\" data-filter-output=\"(out)\"\n                    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  highlight\" id=\"button\" class=\"language-javascript\"\u003ewhich means when\n                    there is only one node, after reversion, the head is\n                    still\n                    itself.\u003c/code\u003e\u003c/pre\u003e\n                \u003col start=\"2\" type=\"1\"\u003e\n                    \u003cli\u003eAfter reversion, the new head is \u003ccode class=\"language-javascript  highlight\" id=\"button\"\n                            class=\"language-javascript\"\u003elast\u003c/code\u003e, and the former\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\" class=\"language-javascript\"\u003ehead\u003c/code\u003e\n                        becomes the last node,\n                        don't forget to point its tail to null.\n                    \u003c/li\u003e\n                \u003c/ol\u003e\n                \u003cp\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\" class=\"language-javascript\"\u003ejava head.next =\n                        null;\u003c/code\u003e\u003c/p\u003e\n                \u003cp\u003eAfter understanding above, now we can proceed further, the problem below is actually an extend to the\n                    above\n                    solution.\u003c/p\u003e\n                \u003ch3 id=\"reverse-first-n-nodes\"\u003e2. Reverse first N nodes\u003c/h3\u003e\n                \u003cp\u003eThis time we will implement a funtion below:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb7\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\" data-filter-output=\"(out)\"\n                        class=\"sourceCode java\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode java\"\u003e\u003ca class=\"sourceLine\" id=\"cb7-1\" title=\"1\"\u003e\u003cspan class=\"lang-js co\"\u003e// reverse first n nodes in a linked list (n \u0026lt;= length of the list)\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb7-2\" title=\"2\"\u003eListNode \u003cspan class=\"lang-js fu\"\u003ereverseN\u003c/span\u003e(ListNode head, \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e n)\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eTake below as an example, call \u003ccode class=\"language-javascript  highlight\" id=\"button\"\n                        class=\"language-javascript\"\u003ereverseN(head, 3)\u003c/code\u003e :\u003c/p\u003e\n                \u003cp\u003e\u003cimg src=\"../pictures/reverse_linked_list/6.jpg\" /\u003e\u003c/p\u003e\n                \u003cp\u003eThe idea is similar to reversing the whole linked list, only a few modifications needed:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb8\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\" data-filter-output=\"(out)\"\n                        class=\"sourceCode java\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode java\"\u003e\u003ca class=\"sourceLine\" id=\"cb8-1\" title=\"1\"\u003eListNode successor = \u003cspan class=\"lang-js kw\"\u003enull\u003c/span\u003e; \u003cspan class=\"lang-js co\"\u003e// successor node\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-2\" title=\"2\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-3\" title=\"3\"\u003e\u003cspan class=\"lang-js co\"\u003e// reverse n nodes starting from head, and return new head\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-4\" title=\"4\"\u003eListNode \u003cspan class=\"lang-js fu\"\u003ereverseN\u003c/span\u003e(ListNode head, \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e n) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-5\" title=\"5\"\u003e    \u003cspan class=\"lang-js kw\"\u003eif\u003c/span\u003e (n == \u003cspan class=\"lang-js dv\"\u003e1\u003c/span\u003e) { \u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-6\" title=\"6\"\u003e        \u003cspan class=\"lang-js co\"\u003e// mark the (n + 1)th node\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-7\" title=\"7\"\u003e        successor = head.\u003cspan class=\"lang-js fu\"\u003enext\u003c/span\u003e;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-8\" title=\"8\"\u003e        \u003cspan class=\"lang-js kw\"\u003ereturn\u003c/span\u003e head;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-9\" title=\"9\"\u003e    }\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-10\" title=\"10\"\u003e    \u003cspan class=\"lang-js co\"\u003e// starts from head.next, revers the first n - 1 nodes\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-11\" title=\"11\"\u003e    ListNode last = \u003cspan class=\"lang-js fu\"\u003ereverseN\u003c/span\u003e(head.\u003cspan class=\"lang-js fu\"\u003enext\u003c/span\u003e, n - \u003cspan class=\"lang-js dv\"\u003e1\u003c/span\u003e);\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-12\" title=\"12\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-13\" title=\"13\"\u003e    head.\u003cspan class=\"lang-js fu\"\u003enext\u003c/span\u003e.\u003cspan class=\"lang-js fu\"\u003enext\u003c/span\u003e = head;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-14\" title=\"14\"\u003e    \u003cspan class=\"lang-js co\"\u003e// link the new head to successor\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-15\" title=\"15\"\u003e    head.\u003cspan class=\"lang-js fu\"\u003enext\u003c/span\u003e = successor;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-16\" title=\"16\"\u003e    \u003cspan class=\"lang-js kw\"\u003ereturn\u003c/span\u003e last;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb8-17\" title=\"17\"\u003e}    \u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eMain differences:\u003c/p\u003e\n                \u003col type=\"1\"\u003e\n                    \u003cli\u003eBase case \u003ccode class=\"language-javascript  highlight\" id=\"button\" class=\"language-javascript\"\u003en\n                            == 1\u003c/code\u003e, if reverse only one element, then new\n                        head is\n                        itself, meanwhile\n                        \u003cstrong\u003eremember to mark the successor node\u003c/strong\u003e.\n                    \u003c/li\u003e\n                    \u003cli\u003eIn previouse solution, we set \u003ccode class=\"language-javascript  highlight\" id=\"button\"\n                            class=\"language-javascript\"\u003ehead.next\u003c/code\u003e directly to\n                        null,\n                        because\n                        after reversing the whole\n                        list, head becoms the last node. But now \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\" class=\"language-javascript\"\u003ehead\u003c/code\u003e may not\n                        be the\n                        last\n                        node after reversion, so we\n                        need mark \u003ccode class=\"language-javascript  highlight\" id=\"button\"\n                            class=\"language-javascript\"\u003esuccessor\u003c/code\u003e (the (n+1)th node), and link it to\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\" class=\"language-javascript\"\u003ehead\u003c/code\u003e\n                        after reversion.\n                    \u003c/li\u003e\n                \u003c/ol\u003e\n                \u003cp\u003e\u003cimg src=\"../pictures/reverse_linked_list/7.jpg\" /\u003e\u003c/p\u003e\n                \u003cp\u003eOK, now we are pretty close to reversing part of the linked list.\u003c/p\u003e\n                \u003ch3 id=\"reverse-part-of-a-linked-list\"\u003e3. Reverse part of a linked list\u003c/h3\u003e\n                \u003cp\u003eGiven an interval \u003ccode class=\"language-javascript  highlight\" id=\"button\"\n                        class=\"language-javascript\"\u003e[m,n]\u003c/code\u003e (index starts from 1), only reverse\n                    elements\n                    in\n                    this section.\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb9\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\" data-filter-output=\"(out)\"\n                        class=\"sourceCode java\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode java\"\u003e\u003ca class=\"sourceLine\" id=\"cb9-1\" title=\"1\"\u003eListNode \u003cspan class=\"lang-js fu\"\u003ereverseBetween\u003c/span\u003e(ListNode head, \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e m, \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e n)\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eFirst, if \u003ccode class=\"language-javascript  highlight\" id=\"button\" class=\"language-javascript\"\u003em ==\n                        1\u003c/code\u003e , it is equal to reversing the first \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\" class=\"language-javascript\"\u003en\u003c/code\u003e elements as we discussed just\n                    now.\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb10\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\" data-filter-output=\"(out)\"\n                        class=\"sourceCode java\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode java\"\u003e\u003ca class=\"sourceLine\" id=\"cb10-1\" title=\"1\"\u003eListNode \u003cspan class=\"lang-js fu\"\u003ereverseBetween\u003c/span\u003e(ListNode head, \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e m, \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e n) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-2\" title=\"2\"\u003e    \u003cspan class=\"lang-js co\"\u003e// base case\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-3\" title=\"3\"\u003e    \u003cspan class=\"lang-js kw\"\u003eif\u003c/span\u003e (m == \u003cspan class=\"lang-js dv\"\u003e1\u003c/span\u003e) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-4\" title=\"4\"\u003e        \u003cspan class=\"lang-js co\"\u003e// equals to reversing the first n nodes\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-5\" title=\"5\"\u003e        \u003cspan class=\"lang-js kw\"\u003ereturn\u003c/span\u003e \u003cspan class=\"lang-js fu\"\u003ereverseN\u003c/span\u003e(head, n);\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-6\" title=\"6\"\u003e    }\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-7\" title=\"7\"\u003e    \u003cspan class=\"lang-js co\"\u003e// ...\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb10-8\" title=\"8\"\u003e}\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                \u003c/div\u003e\n                \u003cp\u003eWhat if \u003ccode class=\"language-javascript  highlight\" id=\"button\" class=\"language-javascript\"\u003em !=\n                        1\u003c/code\u003e ? If we take the index of the \u003ccode class=\"language-javascript  highlight\" id=\"button\"\n                        class=\"language-javascript\"\u003ehead\u003c/code\u003e as 1, then we need to reverse from\n                    the \u003ccode class=\"language-javascript  highlight\" id=\"button\" class=\"language-javascript\"\u003emth\u003c/code\u003e\n                    element. And what if we take the index of the \u003ccode class=\"language-javascript  highlight\"\n                        id=\"button\" class=\"language-javascript\"\u003ehead.next\u003c/code\u003e as 1? Then compared to\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\n                        class=\"language-javascript\"\u003ehead.next\u003c/code\u003e , the reverse section should start from \u003ccode\n                        class=\"language-javascript  highlight\" id=\"button\" class=\"language-javascript\"\u003e(m-1)th\u003c/code\u003e\n                    element. And what about\n                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\n                        class=\"language-javascript\"\u003ehead.next.next\u003c/code\u003e …\n                \u003c/p\u003e\n                \u003cp\u003eDifferent from iteration, this is how we think in the recursive way, so our code should be:\u003c/p\u003e\n                \u003cdiv class=\"sourceCode\" id=\"cb11\"\u003e\n                    \u003cpre data-filter-output=\"(out)\" class=\"sourceCode javascript\" data-filter-output=\"(out)\"\n                        class=\"sourceCode java\"\u003e\u003ccode  class=\"language-javascript  highlight\" id=\"button\" class=\"sourceCode java\"\u003e\u003ca class=\"sourceLine\" id=\"cb11-1\" title=\"1\"\u003eListNode \u003cspan class=\"lang-js fu\"\u003ereverseBetween\u003c/span\u003e(ListNode head, \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e m, \u003cspan class=\"lang-js dt\"\u003eint\u003c/span\u003e n) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb11-2\" title=\"2\"\u003e    \u003cspan class=\"lang-js co\"\u003e// base case\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb11-3\" title=\"3\"\u003e    \u003cspan class=\"lang-js kw\"\u003eif\u003c/span\u003e (m == \u003cspan class=\"lang-js dv\"\u003e1\u003c/span\u003e) {\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb11-4\" title=\"4\"\u003e        \u003cspan class=\"lang-js kw\"\u003ereturn\u003c/span\u003e \u003cspan class=\"lang-js fu\"\u003ereverseN\u003c/span\u003e(head, n);\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb11-5\" title=\"5\"\u003e    }\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb11-6\" title=\"6\"\u003e    head.\u003cspan class=\"lang-js fu\"\u003enext\u003c/span\u003e = \u003cspan class=\"lang-js fu\"\u003ereverseBetween\u003c/span\u003e(head.\u003cspan class=\"lang-js fu\"\u003enext\u003c/span\u003e, m - \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=\"cb11-7\" title=\"7\"\u003e    \u003cspan class=\"lang-js kw\"\u003ereturn\u003c/span\u003e head;\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb11-8\" title=\"8\"\u003e}\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n\n\n                    \u003ch2 id=\"stack-and-queue-properties\"\u003eStack and Queue Properties\u003c/h2\u003e\n                    Stacks and Queues are so similar in composition that we can discuss their properties together. They\n                    track\n                    the\n                    following three properties:\n                    \u003cp align=\"center\"\u003e\n                        \u003cb\u003eStack Properties | Queue Properties:\u003c/b\u003e\n                    \u003c/p\u003e\n                    \u003ctable\u003e\n                        \u003ccolgroup\u003e\n                            \u003ccol style=\"width: 10%\" /\u003e\n                            \u003ccol style=\"width: 39%\" /\u003e\n                            \u003ccol style=\"width: 10%\" /\u003e\n                            \u003ccol style=\"width: 39%\" /\u003e\n                        \u003c/colgroup\u003e\n                        \u003cthead\u003e\n                            \u003ctr class=\"header\"\u003e\n                                \u003cth style=\"text-align: center;\"\u003eStack Property\u003c/th\u003e\n                                \u003cth style=\"text-align: center;\"\u003eDescription\u003c/th\u003e\n                                \u003cth style=\"text-align: center;\"\u003eQueue Property\u003c/th\u003e\n                                \u003cth style=\"text-align: center;\"\u003eDescription\u003c/th\u003e\n                            \u003c/tr\u003e\n                        \u003c/thead\u003e\n                        \u003ctbody\u003e\n                            \u003ctr class=\"odd\"\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003etop\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eThe first node in the Stack\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003efront\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eThe first node in the Queue.\u003c/td\u003e\n                            \u003c/tr\u003e\n                            \u003ctr class=\"even\"\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e—-\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eStacks do not have an equivalent\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003eback\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eThe last node in the Queue.\u003c/td\u003e\n                            \u003c/tr\u003e\n                            \u003ctr class=\"odd\"\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003elength\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eThe number of nodes in the Stack; the Stack's length.\n                                \u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003elength\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eThe number of nodes in the Queue; the Queue's length.\n                                \u003c/td\u003e\n                            \u003c/tr\u003e\n                        \u003c/tbody\u003e\n                    \u003c/table\u003e\n                    \u003cp\u003eNotice that rather than having a \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003ehead\u003c/code\u003e and a \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003etail\u003c/code\u003e like Linked Lists,\n                        Stacks have\n                        a\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003etop\u003c/code\u003e, and Queues have a \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003efront\u003c/code\u003e and a \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003eback\u003c/code\u003e instead. Stacks\n                        don't\n                        have\n                        the\n                        equivalent of a \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003etail\u003c/code\u003e because you\n                        only ever push or pop things off the top of\n                        Stacks. These\n                        properties are essentially the same; pointers to the end points of the respective List ADT where\n                        important\n                        actions way take place. The differences in naming conventions are strictly for human\n                        comprehension.\n                    \u003c/p\u003e\n                    \u003chr /\u003e\n                    \u003cp\u003eSimilarly to Linked Lists, the values stored inside a Stack or a Queue are actually contained\n                        within\n                        Stack\n                        Node\n                        and Queue Node instances. Stack, Queue, and Singly Linked List Nodes are all identical, but just\n                        as a\n                        reminder\n                        and for the sake of completion, these List Nodes track the following two properties:\u003c/p\u003e\n                    \u003cp align=\"center\"\u003e\n                        \u003cb\u003eStack \u0026amp; Queue Node Properties:\u003c/b\u003e\n                    \u003c/p\u003e\n                    \u003ctable\u003e\n                        \u003cthead\u003e\n                            \u003ctr class=\"header\"\u003e\n                                \u003cth style=\"text-align: center;\"\u003eProperty\u003c/th\u003e\n                                \u003cth style=\"text-align: center;\"\u003eDescription\u003c/th\u003e\n                            \u003c/tr\u003e\n                        \u003c/thead\u003e\n                        \u003ctbody\u003e\n                            \u003ctr class=\"odd\"\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003evalue\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eThe actual value this node represents.\u003c/td\u003e\n                            \u003c/tr\u003e\n                            \u003ctr class=\"even\"\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003enext\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eThe next node in the Stack (relative to this node).\u003c/td\u003e\n                            \u003c/tr\u003e\n                        \u003c/tbody\u003e\n                    \u003c/table\u003e\n                    \u003ch2 id=\"stack-methods\"\u003eStack Methods\u003c/h2\u003e\n                    \u003cp\u003eIn the exercise that follows, we will implement a Stack data structure along with the following\n                        Stack\n                        methods:\n                    \u003c/p\u003e\n                    \u003ctable\u003e\n                        \u003ccolgroup\u003e\n                            \u003ccol style=\"width: 9%\" /\u003e\n                            \u003ccol style=\"width: 12%\" /\u003e\n                            \u003ccol style=\"width: 45%\" /\u003e\n                            \u003ccol style=\"width: 32%\" /\u003e\n                        \u003c/colgroup\u003e\n                        \u003cthead\u003e\n                            \u003ctr class=\"header\"\u003e\n                                \u003cth style=\"text-align: center;\"\u003eType\u003c/th\u003e\n                                \u003cth style=\"text-align: center;\"\u003eName\u003c/th\u003e\n                                \u003cth style=\"text-align: center;\"\u003eDescription\u003c/th\u003e\n                                \u003cth style=\"text-align: center;\"\u003eReturns\u003c/th\u003e\n                            \u003c/tr\u003e\n                        \u003c/thead\u003e\n                        \u003ctbody\u003e\n                            \u003ctr class=\"odd\"\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eInsertion\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003epush\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eAdds a Node to the top of the Stack.\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eInteger - New size of stack\u003c/td\u003e\n                            \u003c/tr\u003e\n                            \u003ctr class=\"even\"\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eDeletion\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003epop\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eRemoves a Node from the top of the Stack.\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eNode removed from top of Stack\u003c/td\u003e\n                            \u003c/tr\u003e\n                            \u003ctr class=\"odd\"\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eMeta\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003esize\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eReturns the current size of the Stack.\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eInteger\u003c/td\u003e\n                            \u003c/tr\u003e\n                        \u003c/tbody\u003e\n                    \u003c/table\u003e\n                    \u003ch2 id=\"queue-methods\"\u003eQueue Methods\u003c/h2\u003e\n                    \u003cp\u003eIn the exercise that follows, we will implement a Queue data structure along with the following\n                        Queue\n                        methods:\n                    \u003c/p\u003e\n                    \u003ctable\u003e\n                        \u003ccolgroup\u003e\n                            \u003ccol style=\"width: 9%\" /\u003e\n                            \u003ccol style=\"width: 12%\" /\u003e\n                            \u003ccol style=\"width: 45%\" /\u003e\n                            \u003ccol style=\"width: 32%\" /\u003e\n                        \u003c/colgroup\u003e\n                        \u003cthead\u003e\n                            \u003ctr class=\"header\"\u003e\n                                \u003cth style=\"text-align: center;\"\u003eType\u003c/th\u003e\n                                \u003cth style=\"text-align: center;\"\u003eName\u003c/th\u003e\n                                \u003cth style=\"text-align: center;\"\u003eDescription\u003c/th\u003e\n                                \u003cth style=\"text-align: center;\"\u003eReturns\u003c/th\u003e\n                            \u003c/tr\u003e\n                        \u003c/thead\u003e\n                        \u003ctbody\u003e\n                            \u003ctr class=\"odd\"\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eInsertion\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003eenqueue\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eAdds a Node to the front of the Queue.\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eInteger - New size of Queue\u003c/td\u003e\n                            \u003c/tr\u003e\n                            \u003ctr class=\"even\"\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eDeletion\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003edequeue\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eRemoves a Node from the front of the Queue.\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eNode removed from front of Queue\u003c/td\u003e\n                            \u003c/tr\u003e\n                            \u003ctr class=\"odd\"\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eMeta\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003esize\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eReturns the current size of the Queue.\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eInteger\u003c/td\u003e\n                            \u003c/tr\u003e\n                        \u003c/tbody\u003e\n                    \u003c/table\u003e\n                    \u003ch2 id=\"time-and-space-complexity-analysis-5\"\u003eTime and Space Complexity Analysis\u003c/h2\u003e\n                    \u003cp\u003eBefore we begin our analysis, here is a quick summary of the Time and Space constraints of each\n                        Stack\n                        Operation.\n                    \u003c/p\u003e\n                    \u003ctable\u003e\n                        \u003ccolgroup\u003e\n                            \u003ccol style=\"width: 26%\" /\u003e\n                            \u003ccol style=\"width: 22%\" /\u003e\n                            \u003ccol style=\"width: 25%\" /\u003e\n                            \u003ccol style=\"width: 26%\" /\u003e\n                        \u003c/colgroup\u003e\n                        \u003cthead\u003e\n                            \u003ctr class=\"header\"\u003e\n                                \u003cth style=\"text-align: center;\"\u003eData Structure Operation\u003c/th\u003e\n                                \u003cth style=\"text-align: center;\"\u003eTime Complexity (Avg)\u003c/th\u003e\n                                \u003cth style=\"text-align: center;\"\u003eTime Complexity (Worst)\u003c/th\u003e\n                                \u003cth style=\"text-align: center;\"\u003eSpace Complexity (Worst)\u003c/th\u003e\n                            \u003c/tr\u003e\n                        \u003c/thead\u003e\n                        \u003ctbody\u003e\n                            \u003ctr class=\"odd\"\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eAccess\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003eΘ(n)\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003eO(n)\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003eO(n)\u003c/code\u003e\u003c/td\u003e\n                            \u003c/tr\u003e\n                            \u003ctr class=\"even\"\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eSearch\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003eΘ(n)\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003eO(n)\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003eO(n)\u003c/code\u003e\u003c/td\u003e\n                            \u003c/tr\u003e\n                            \u003ctr class=\"odd\"\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eInsertion\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003eΘ(1)\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003eO(1)\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003eO(n)\u003c/code\u003e\u003c/td\u003e\n                            \u003c/tr\u003e\n                            \u003ctr class=\"even\"\u003e\n                                \u003ctd style=\"text-align: center;\"\u003eDeletion\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003eΘ(1)\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003eO(1)\u003c/code\u003e\u003c/td\u003e\n                                \u003ctd style=\"text-align: center;\"\u003e\u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003eO(n)\u003c/code\u003e\u003c/td\u003e\n                            \u003c/tr\u003e\n                        \u003c/tbody\u003e\n                    \u003c/table\u003e\n                    \u003cp\u003eBefore moving forward, see if you can reason to yourself why each operation has the time and\n                        space\n                        complexity\n                        listed above!\u003c/p\u003e\n                    \u003ch4 id=\"time-complexity---access-and-search-1\"\u003eTime Complexity - Access and Search:\u003c/h4\u003e\n                    \u003cp\u003eWhen the Stack ADT was first conceived, its inventor definitely did not prioritize searching and\n                        accessing\n                        individual Nodes or values in the list. The same idea applies for the Queue ADT. There are\n                        certainly\n                        better\n                        data\n                        structures for speedy search and lookup, and if these operations are a priority for your use\n                        case, it\n                        would\n                        be\n                        best to choose something else!\u003c/p\u003e\n                    \u003cp\u003eSearch and Access are both linear time operations for Stacks and Queues, and that shouldn't be\n                        too\n                        unclear.\n                        Both\n                        ADTs are nearly identical to Linked Lists in this way. The only way to find a Node somewhere in\n                        the\n                        middle\n                        of a\n                        Stack or a Queue, is to start at the \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003etop\u003c/code\u003e (or the \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003eback\u003c/code\u003e) and traverse\n                        downward\n                        (or\n                        forward) toward the \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ebottom\u003c/code\u003e (or\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003efront\u003c/code\u003e) one node at a time via\n                        each\n                        Node's\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003enext\u003c/code\u003e property.\n                    \u003c/p\u003e\n                    \u003cp\u003eThis is a linear time operation, O(n).\u003c/p\u003e\n                    \u003ch4 id=\"time-complexity---insertion-and-deletion-1\"\u003eTime Complexity - Insertion and Deletion:\u003c/h4\u003e\n                    \u003cp\u003eFor Stacks and Queues, insertion and deletion is what it's all about. If there is one feature a\n                        Stack\n                        absolutely\n                        must have, it's constant time insertion and removal to and from the \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003etop\u003c/code\u003e of the\n                        Stack\n                        (FIFO).\n                        The\n                        same applies for Queues, but with insertion occurring at the \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003eback\u003c/code\u003e and removal\n                        occurring at\n                        the\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003efront\u003c/code\u003e (LIFO).\n                    \u003c/p\u003e\n                    \u003cp\u003eThink about it. When you add a plate to the top of a stack of plates, do you have to iterate\n                        through all\n                        of\n                        the\n                        other plates first to do so? Of course not. You simply add your plate to the top of the stack,\n                        and\n                        that's\n                        that.\n                        The concept is the same for removal.\u003c/p\u003e\n                    \u003cp\u003eTherefore, Stacks and Queues have constant time Insertion and Deletion via their\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003epush\u003c/code\u003e and\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003epop\u003c/code\u003e or \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003eenqueue\u003c/code\u003e and \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003edequeue\u003c/code\u003e methods, O(1).\n                    \u003c/p\u003e\n                    \u003ch4 id=\"space-complexity-2\"\u003eSpace Complexity:\u003c/h4\u003e\n                    \u003cp\u003eThe space complexity of Stacks and Queues is very simple. Whether we are instantiating a new\n                        instance of\n                        a\n                        Stack\n                        or Queue to store a set of data, or we are using a Stack or Queue as part of a strategy to solve\n                        some\n                        problem,\n                        Stacks and Queues always store one Node for each value they receive as input.\u003c/p\u003e\n                    \u003cp\u003eFor this reason, we always consider Stacks and Queues to have a linear space complexity, O(n).\n                    \u003c/p\u003e\n                    \u003ch2 id=\"when-should-we-use-stacks-and-queues\"\u003eWhen should we use Stacks and Queues?\u003c/h2\u003e\n                    \u003cp\u003eAt this point, we've done a lot of work understanding the ins and outs of Stacks and Queues, but\n                        we still\n                        haven't\n                        really discussed what we can use them for. The answer is actually…a lot!\u003c/p\u003e\n                    \u003cp\u003eFor one, Stacks and Queues can be used as intermediate data structures while implementing some of\n                        the\n                        more\n                        complicated data structures and methods we'll see in some of our upcoming sections.\u003c/p\u003e\n                    \u003cp\u003eFor example, the implementation of the breadth-first Tree traversal algorithm takes advantage of\n                        a Queue\n                        instance, and the depth-first Graph traversal algorithm exploits the benefits of a Stack\n                        instance.\u003c/p\u003e\n                    \u003cp\u003eAdditionally, Stacks and Queues serve as the essential underlying data structures to a wide\n                        variety of\n                        applications you use all the time. Just to name a few:\u003c/p\u003e\n                    \u003ch4 id=\"stacks\"\u003eStacks:\u003c/h4\u003e\n                    \u003cul\u003e\n                        \u003cli\u003eThe Call Stack is a Stack data structure, and is used to manage the order of function\n                            invocations in\n                            your\n                            code.\u003c/li\u003e\n                        \u003cli\u003eBrowser History is often implemented using a Stack, with one great example being the browser\n                            history\n                            object\n                            in the very popular React Router module.\u003c/li\u003e\n                        \u003cli\u003eUndo/Redo functionality in just about any application. For example:\n                            \u003cul\u003e\n                                \u003cli\u003eWhen you're coding in your text editor, each of the actions you take on your\n                                    keyboard are\n                                    recorded\n                                    by \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003epush\u003c/code\u003eing that\n                                    event to a Stack.\u003c/li\u003e\n                                \u003cli\u003eWhen you hit [cmd + z] to undo your most recent action, that event is\n                                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003epop\u003c/code\u003eed off\n                                    the\n                                    Stack, because the last event that occured should be the first one to be undone\n                                    (LIFO).\n                                \u003c/li\u003e\n                                \u003cli\u003eWhen you hit [cmd + y] to redo your most recent action, that event is\n                                    \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003epush\u003c/code\u003eed\n                                    back\n                                    onto\n                                    the Stack.\n                                \u003c/li\u003e\n                            \u003c/ul\u003e\n                        \u003c/li\u003e\n                    \u003c/ul\u003e\n                    \u003ch4 id=\"queues\"\u003eQueues:\u003c/h4\u003e\n                    \u003cul\u003e\n                        \u003cli\u003ePrinters use a Queue to manage incoming jobs to ensure that documents are printed in the\n                            order they\n                            are\n                            received.\u003c/li\u003e\n                        \u003cli\u003eChat rooms, online video games, and customer service phone lines use a Queue to ensure that\n                            patrons\n                            are\n                            served in the order they arrive.\n                            \u003cul\u003e\n                                \u003cli\u003eIn the case of a Chat Room, to be admitted to a size-limited room.\u003c/li\u003e\n                                \u003cli\u003eIn the case of an Online Multi-Player Game, players wait in a lobby until there is\n                                    enough\n                                    space\n                                    and\n                                    it is their turn to be admitted to a game.\u003c/li\u003e\n                                \u003cli\u003eIn the case of a Customer Service Phone Line…you get the point.\u003c/li\u003e\n                            \u003c/ul\u003e\n                        \u003c/li\u003e\n                        \u003cli\u003eAs a more advanced use case, Queues are often used as components or services in the system\n                            design of\n                            a\n                            service-oriented architecture. A very popular and easy to use example of this is Amazon's\n                            Simple\n                            Queue\n                            Service (SQS), which is a part of their Amazon Web Services (AWS) offering.\n                            \u003cul\u003e\n                                \u003cli\u003eYou would add this service to your system between two other services, one that is\n                                    sending\n                                    information for processing, and one that is receiving information to be processed,\n                                    when the\n                                    volume\n                                    of incoming requests is high and the integrity of the order with which those\n                                    requests are\n                                    processed\n                                    must be maintained.\u003c/li\u003e\n                            \u003c/ul\u003e\n                        \u003c/li\u003e\n                    \u003c/ul\u003e\n                    \u003chr /\u003e\n\n\n                    \u003chr /\u003e\n                    \u003ch1 id=\"graphs-and-heaps-\"\u003eGraphs and Heaps \u003c/h1\u003e\n                    \u003cp\u003e\u003cstrong\u003eThe objective of this lesson\u003c/strong\u003e is for you to become comfortable with implementing\n                        common\n                        data\n                        structures. This is important because questions about data structures are incredibly likely to\n                        be\n                        interview\n                        questions for software engineers from junior to senior levels. Moreover, understanding how\n                        different\n                        data\n                        structures work will influence the libraries and frameworks that you choose when writing\n                        software.\u003c/p\u003e\n                    \u003cp\u003eWhen you are done, you will be able to:\u003c/p\u003e\n                    \u003col type=\"1\"\u003e\n                        \u003cli\u003eExplain and implement a Heap.\u003c/li\u003e\n                        \u003cli\u003eExplain and implement a Graph.table with implementing common data structures. This is\n                            important\n                            because\n                            questions about data structures are incredibly likely to be interview questions for software\n                            engineers\n                            from\n                            junior to senior levels. Moreover, understanding how different data structures work will\n                            influence\n                            the\n                            libraries and frameworks that you choose when writing software.\u003c/li\u003e\n                    \u003c/ol\u003e\n                    \u003cp\u003eWhen you are done, you will be able to:\u003c/p\u003e\n                    \u003col type=\"1\"\u003e\n                        \u003cli\u003eExplain and implement a Heap.\u003c/li\u003e\n                        \u003cli\u003eExplain and implement a Graph.\u003c/li\u003e\n                    \u003c/ol\u003e\n                    \u003chr /\u003e\n                    \u003ch1 id=\"introduction-to-heaps\"\u003eIntroduction to Heaps\u003c/h1\u003e\n                    \u003cp\u003eLet's explore the \u003cstrong\u003eHeap\u003c/strong\u003e data structure! In particular, we'll explore\n                        \u003cstrong\u003eBinary\n                            Heaps\u003c/strong\u003e. A binary heap is a type of binary tree. However, a heap is not a binary\n                        \u003cem\u003esearch\u003c/em\u003e\n                        tree. A heap is a partially ordered data structure, whereas a BST has full order. In a heap, the\n                        root of\n                        the\n                        tree will be the maximum (max heap) or the minimum (min heap). Below is an example of a max\n                        heap:\n                    \u003c/p\u003e\n                    \u003cfigure\u003e\n                        \u003cimg src=\"images/max_heap.png\" alt=\"max_heap\" /\u003e\n                        \u003cfigcaption\u003emax_heap\u003c/figcaption\u003e\n                    \u003c/figure\u003e\n                    \u003cp\u003eNotice that the heap above does not follow search tree property where all values to the left of a\n                        node\n                        are\n                        less\n                        and all values to the right are greater or equal. Instead, the max heap invariant is:\u003c/p\u003e\n                    \u003cul\u003e\n                        \u003cli\u003egiven any node, its children must be less than or equal to the node\u003c/li\u003e\n                    \u003c/ul\u003e\n                    \u003cp\u003eThis constraint makes heaps much more relaxed in structure compared to a search tree. There is no\n                        guaranteed\n                        order among \"siblings\" or \"cousins\" in a heap. The relationship only flows down the tree from\n                        parent to\n                        child.\n                        In other words, in a max heap, a node will be greater than all of it's children, it's\n                        grandchildren, its\n                        great-grandchildren, and so on. A consequence of this is the root being the absolute maximum of\n                        the\n                        entire\n                        tree.\n                        We'll be exploring max heaps together, but these arguments are symmetric for a min heap.\u003c/p\u003e\n                    \u003ch3 id=\"complete-trees\"\u003eComplete Trees\u003c/h3\u003e\n                    \u003cp\u003eWe'll eventually implement a max heap together, but first we'll need to take a quick detour. Our\n                        design\n                        goal\n                        is\n                        to implement a data structure with efficient operations. Since a heap is a type of binary tree,\n                        recall\n                        the\n                        circumstances where we had a \"best case\" binary tree. We'll need to ensure our heap has minimal\n                        height,\n                        that\n                        is,\n                        it must be a balanced tree!\u003c/p\u003e\n                    \u003cp\u003eOur heap implementation will not only be balanced, but it will also be \u003cstrong\u003ecomplete\u003c/strong\u003e.\n                        To\n                        clarify,\n                        \u003cstrong\u003eevery complete tree is also a balanced tree\u003c/strong\u003e, but not every balanced tree is\n                        also\n                        complete.\n                        Our\n                        definition of a complete tree is:\n                    \u003c/p\u003e\n                    \u003cul\u003e\n                        \u003cli\u003ea tree where all levels have the maximal number of nodes, except the bottom the level\u003c/li\u003e\n                        \u003cli\u003eAND the bottom level has all nodes filled as far left as possible\u003c/li\u003e\n                    \u003c/ul\u003e\n                    \u003cp\u003eHere are few examples of the definition:\u003c/p\u003e\n                    \u003cfigure\u003e\n                        \u003cimg src=\"images/complete_tree.png\" alt=\"complete_tree\" /\u003e\n                        \u003cfigcaption\u003ecomplete_tree\u003c/figcaption\u003e\n                    \u003c/figure\u003e\n                    \u003cp\u003eNotice that the tree is on the right fails the second point of our definition because there is a\n                        gap in\n                        the\n                        last\n                        level. Informally, you can think about a complete tree as packing its nodes as closely together\n                        as\n                        possible.\n                        This line of thinking will come into play when we code heaps later.\u003c/p\u003e\n                    \u003ch3 id=\"when-to-use-heaps\"\u003eWhen to Use Heaps?\u003c/h3\u003e\n                    \u003cp\u003eHeaps are the most useful when attacking problems that require you to \"partially sort\" data. This\n                        usually\n                        takes\n                        form in problems that have us calculate the largest or smallest n numbers of a collection. For\n                        example:\n                        What\n                        if\n                        you were asked to find the largest 5 numbers in an array in linear time, O(n)? The fastest\n                        sorting\n                        algorithms\n                        are O(n logn), so none of those algorithms will be good enough. However, we can use a heap to\n                        solve this\n                        problem\n                        in linear time.\u003c/p\u003e\n                    \u003cp\u003eWe'll analyze this in depth when we implement a heap in the next section!\u003c/p\u003e\n                    \u003cp\u003eOne of the most common uses of a binary heap is to implement a \"\u003ca\n                            href=\"https://en.wikipedia.org/wiki/Priority_queue\"\u003epriority queue\u003c/a\u003e\". We learned before\n                        that a\n                        queue\n                        is a\n                        FIFO (First In, First Out) data structure. With a priority queue, items are removed from the\n                        queue based\n                        on\n                        a\n                        priority number. The priority number is used to place the items into the heap and pull them out\n                        in the\n                        correct\n                        priority order!\u003c/p\u003e\n                    \u003chr /\u003e\n                    \u003ch2 id=\"binary-heap-implementation\"\u003eBinary Heap Implementation\u003c/h2\u003e\n                    \u003cp\u003eNow that we are familiar with the structure of a heap, let's implement one! What may be\n                        surprising is\n                        that\n                        the\n                        usual way to implement a heap is by simply using an array. That is, we won't need to create a\n                        node class\n                        with\n                        pointers. Instead, each index of the array will represent a node, with the root being at index\n                        1. We'll\n                        avoid\n                        using index 0 of the array so our math works out nicely. From this point, we'll use the\n                        following rules\n                        to\n                        interpret the array as a heap:\u003c/p\u003e\n                    \u003cul\u003e\n                        \u003cli\u003eindex \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ei\u003c/code\u003e represents a node in\n                            the heap\u003c/li\u003e\n                        \u003cli\u003ethe left child of node \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ei\u003c/code\u003e can\n                            be found at index \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e2 * i\u003c/code\u003e\u003c/li\u003e\n                        \u003cli\u003ethe right child of code \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ei\u003c/code\u003e\n                            can be found at index \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e2 * i +\n                                1\u003c/code\u003e\u003c/li\u003e\n                    \u003c/ul\u003e\n                    \u003cp\u003eIn other words, the array \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e[null, 42, 32,\n                            24, 30, 9, 20, 18, 2, 7]\u003c/code\u003e represents the\n                        heap below.\n                        Take a\n                        moment to analyze how the array indices work out to represent left and right children.\u003c/p\u003e\n                    \u003cfigure\u003e\n                        \u003cimg src=\"https://s3-us-west-1.amazonaws.com/appacademy-open-assets/data_structures_algorithms/heaps/images/max_heap.png\"\n                            alt=\"max_heap\" /\u003e\n                        \u003cfigcaption\u003emax_heap\u003c/figcaption\u003e\n                    \u003c/figure\u003e\n                    \u003cp\u003ePretty clever math right? We can also describe the relationship from child to parent node. Say we\n                        are\n                        given a\n                        node at index \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ei\u003c/code\u003e in the heap,\n                        then it's parent is found at index \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003eMath.floor(i\n                            /\n                            2)\u003c/code\u003e.\n                    \u003c/p\u003e\n                    \u003cp\u003eIt's useful to visualize heap algorithms using the classic image of nodes and edges, but we'll\n                        translate\n                        that\n                        into array index operations.\u003c/p\u003e\n                    \u003ch3 id=\"insert\"\u003eInsert\u003c/h3\u003e\n                    \u003cp\u003eWhat's a heap if we can't add data into it? We'll need a \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003einsert\u003c/code\u003e method that will add\n                        a new\n                        value\n                        into the heap without voiding our heap property. In our \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003eMaxHeap\u003c/code\u003e, the property\n                        states that a\n                        node\n                        must be greater than its children.\u003c/p\u003e\n                    \u003ch4 id=\"visualizing-our-heap-as-a-tree-of-nodes\"\u003eVisualizing our heap as a tree of nodes:\u003c/h4\u003e\n                    \u003col type=\"1\"\u003e\n                        \u003cli\u003eWe begin an insertion by adding the new node to the bottom leaf level of the heap,\n                            preferring to\n                            place\n                            the\n                            new node as far left in the level as possible. This ensures the tree remains complete.\u003c/li\u003e\n                        \u003cli\u003ePlacing the new node there may momentarily break our heap property, so we need to restore it\n                            by\n                            moving\n                            the\n                            node up the tree into a legal position. Restoring the heap property is a matter of\n                            continually\n                            swapping\n                            the\n                            new node with it's parent while it's parent contains a smaller value. We refer to this\n                            process as\n                            \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003esiftUp\u003c/code\u003e\n                        \u003c/li\u003e\n                    \u003c/ol\u003e\n                    \u003ch4 id=\"translating-that-into-array-operations\"\u003eTranslating that into array operations:\u003c/h4\u003e\n                    \u003col type=\"1\"\u003e\n                        \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003epush\u003c/code\u003e the new value to the\n                            end of the array\u003c/li\u003e\n                        \u003cli\u003econtinually swap that value toward the front of the array (following our child-parent index\n                            rules)\n                            until\n                            heap property is restored\u003c/li\u003e\n                    \u003c/ol\u003e\n                    \u003ch3 id=\"deletemax\"\u003eDeleteMax\u003c/h3\u003e\n                    \u003cp\u003eThis is the \"fetch\" operation of a heap. Since we maintain heap property throughout, the root of\n                        the heap\n                        will\n                        always be the maximum value. We want to delete and return the root, whilst keeping the heap\n                        property.\n                    \u003c/p\u003e\n                    \u003ch4 id=\"visualizing-our-heap-as-a-tree-of-nodes-1\"\u003eVisualizing our heap as a tree of nodes:\u003c/h4\u003e\n                    \u003col type=\"1\"\u003e\n                        \u003cli\u003eWe begin the deletion by saving a reference to the root value (the max) to return later. We\n                            then\n                            locate\n                            the\n                            right most node of the bottom level and copy it's value into the root of the tree. We easily\n                            delete\n                            the\n                            duplicate node at the leaf level. This ensures the tree remains complete.\u003c/li\u003e\n                        \u003cli\u003eCopying that value into the root may momentarily break our heap property, so we need to\n                            restore it\n                            by\n                            moving\n                            the node down the tree into a legal position. Restoring the heap property is a matter of\n                            continually\n                            swapping the node with the greater of it's two children. We refer to this process as\n                            \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003esiftDown\u003c/code\u003e.\n                        \u003c/li\u003e\n                    \u003c/ol\u003e\n                    \u003ch4 id=\"translating-that-into-array-operations-1\"\u003eTranslating that into array operations:\u003c/h4\u003e\n                    \u003col type=\"1\"\u003e\n                        \u003cli\u003eThe root is at index 1, so save it to return later. The right most node of the bottom level\n                            would\n                            just\n                            be\n                            the very last element of the array. Copy the last element into index 1, and pop off the last\n                            element\n                            (since\n                            it now appears at the root).\u003c/li\u003e\n                        \u003cli\u003eContinually swap the new root toward the back of the array (following our parent-child index\n                            rules)\n                            until\n                            heap property is restored. A node can have two children, so we should always prefer to swap\n                            with the\n                            greater\n                            child.\u003c/li\u003e\n                    \u003c/ol\u003e\n                    \u003ch3 id=\"time-complexity-analysis-1\"\u003eTime Complexity Analysis\u003c/h3\u003e\n                    \u003cul\u003e\n                        \u003cli\u003einsert: \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(log(n))\u003c/code\u003e\u003c/li\u003e\n                        \u003cli\u003edeleteMax: \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(log(n))\u003c/code\u003e\u003c/li\u003e\n                    \u003c/ul\u003e\n                    \u003cp\u003eRecall that our heap will be a complete/balanced tree. This means it's height is\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003elog(n)\u003c/code\u003e\n                        where\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003en\u003c/code\u003e is the number of items. Both\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003einsert\u003c/code\u003e and \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003edeleteMax\u003c/code\u003e have\n                        a time\n                        complexity of \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003elog(n)\u003c/code\u003e because of\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003esiftUp\u003c/code\u003e and \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003esiftDown\u003c/code\u003e\n                        respectively.\n                        In\n                        worst case \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003einsert\u003c/code\u003e, we will have\n                        to \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003esiftUp\u003c/code\u003e a leaf all the way to\n                        the\n                        root of\n                        the\n                        tree.\n                        In the worst case \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003edeleteMax\u003c/code\u003e, we\n                        will have to \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003esiftDown\u003c/code\u003e the new\n                        root all\n                        the way\n                        down to\n                        the leaf level. In either case, we'll have to traverse the full height of the tree,\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003elog(n)\u003c/code\u003e.\n                    \u003c/p\u003e\n                    \u003ch4 id=\"array-heapify-analysis\"\u003eArray Heapify Analysis\u003c/h4\u003e\n                    \u003cp\u003eNow that we have established \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003eO(log(n))\u003c/code\u003e for a single insertion, let's analyze the\n                        time\n                        complexity\n                        for\n                        turning an array into a heap (we call this heapify, coming in the next project :)). The\n                        algorithm itself\n                        is\n                        simple, just perform an \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003einsert\u003c/code\u003e\n                        for every element. Since there are \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003en\u003c/code\u003e\n                        elements\n                        and\n                        each\n                        insert requires \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003elog(n)\u003c/code\u003e time, our\n                        total complexity for heapify is\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(nlog(n))\u003c/code\u003e…\n                        Or is\n                        it?\n                        There is actually a tighter bound on heapify. The proof requires some math that you won't find\n                        valuable\n                        in\n                        your\n                        job search, but do understand that the true time complexity of heapify is amortized\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(n)\u003c/code\u003e.\n                        Amortized\n                        refers to the fact that our analysis is about performance over many insertions.\n                    \u003c/p\u003e\n                    \u003ch3 id=\"space-complexity-analysis\"\u003eSpace Complexity Analysis\u003c/h3\u003e\n                    \u003cul\u003e\n                        \u003cli\u003e\n                            \u003cp\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(n)\u003c/code\u003e, since we use a\n                                single array to store heap data.heap, let's implement\n                                one! What\n                                may\n                                be\n                                surprising is that the usual way to implement a heap is by simply using an array. That\n                                is, we\n                                won't\n                                need\n                                to create a node class with pointers. Instead, each index of the array will represent a\n                                node,\n                                with\n                                the\n                                root being at index 1. We'll avoid using index 0 of the array so our math works out\n                                nicely. From\n                                this\n                                point, we'll use the following rules to interpret the array as a heap:\u003c/p\u003e\n                        \u003c/li\u003e\n                        \u003cli\u003eindex \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ei\u003c/code\u003e represents a node in\n                            the heap\u003c/li\u003e\n                        \u003cli\u003ethe left child of node \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ei\u003c/code\u003e can\n                            be found at index \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e2 * i\u003c/code\u003e\u003c/li\u003e\n                        \u003cli\u003e\n                            \u003cp\u003ethe right child of code \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ei\u003c/code\u003e\n                                can be found at index \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e2 * i +\n                                    1\u003c/code\u003e\u003c/p\u003e\n                        \u003c/li\u003e\n                    \u003c/ul\u003e\n                    \u003cp\u003eIn other words, the array \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e[null, 42, 32,\n                            24, 30, 9, 20, 18, 2, 7]\u003c/code\u003e represents the\n                        heap below.\n                        Take a\n                        moment to analyze how the array indices work out to represent left and right children.\u003c/p\u003e\n                    \u003cfigure\u003e\n                        \u003cimg src=\"https://s3-us-west-1.amazonaws.com/appacademy-open-assets/data_structures_algorithms/heaps/images/max_heap.png\"\n                            alt=\"max_heap\" /\u003e\n                        \u003cfigcaption\u003emax_heap\u003c/figcaption\u003e\n                    \u003c/figure\u003e\n                    \u003cp\u003ePretty clever math right? We can also describe the relationship from child to parent node. Say we\n                        are\n                        given a\n                        node at index \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ei\u003c/code\u003e in the heap,\n                        then it's parent is found at index \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003eMath.floor(i\n                            /\n                            2)\u003c/code\u003e.\n                    \u003c/p\u003e\n                    \u003cp\u003eIt's useful to visualize heap algorithms using the classic image of nodes and edges, but we'll\n                        translate\n                        that\n                        into array index operations.\u003c/p\u003e\n                    \u003ch3 id=\"insert-1\"\u003eInsert\u003c/h3\u003e\n                    \u003cp\u003eWhat's a heap if we can't add data into it? We'll need a \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003einsert\u003c/code\u003e method that will add\n                        a new\n                        value\n                        into the heap without voiding our heap property. In our \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003eMaxHeap\u003c/code\u003e, the property\n                        states that a\n                        node\n                        must be greater than its children.\u003c/p\u003e\n                    \u003ch4 id=\"visualizing-our-heap-as-a-tree-of-nodes-2\"\u003eVisualizing our heap as a tree of nodes:\u003c/h4\u003e\n                    \u003col type=\"1\"\u003e\n                        \u003cli\u003eWe begin an insertion by adding the new node to the bottom leaf level of the heap,\n                            preferring to\n                            place\n                            the\n                            new node as far left in the level as possible. This ensures the tree remains complete.\u003c/li\u003e\n                        \u003cli\u003ePlacing the new node there may momentarily break our heap property, so we need to restore it\n                            by\n                            moving\n                            the\n                            node up the tree into a legal position. Restoring the heap property is a matter of\n                            continually\n                            swapping\n                            the\n                            new node with it's parent while it's parent contains a smaller value. We refer to this\n                            process as\n                            \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003esiftUp\u003c/code\u003e\n                        \u003c/li\u003e\n                    \u003c/ol\u003e\n                    \u003ch4 id=\"translating-that-into-array-operations-2\"\u003eTranslating that into array operations:\u003c/h4\u003e\n                    \u003col type=\"1\"\u003e\n                        \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003epush\u003c/code\u003e the new value to the\n                            end of the array\u003c/li\u003e\n                        \u003cli\u003econtinually swap that value toward the front of the array (following our child-parent index\n                            rules)\n                            until\n                            heap property is restored\u003c/li\u003e\n                    \u003c/ol\u003e\n                    \u003ch3 id=\"deletemax-1\"\u003eDeleteMax\u003c/h3\u003e\n                    \u003cp\u003eThis is the \"fetch\" operation of a heap. Since we maintain heap property throughout, the root of\n                        the heap\n                        will\n                        always be the maximum value. We want to delete and return the root, whilst keeping the heap\n                        property.\n                    \u003c/p\u003e\n                    \u003ch4 id=\"visualizing-our-heap-as-a-tree-of-nodes-3\"\u003eVisualizing our heap as a tree of nodes:\u003c/h4\u003e\n                    \u003col type=\"1\"\u003e\n                        \u003cli\u003eWe begin the deletion by saving a reference to the root value (the max) to return later. We\n                            then\n                            locate\n                            the\n                            right most node of the bottom level and copy it's value into the root of the tree. We easily\n                            delete\n                            the\n                            duplicate node at the leaf level. This ensures the tree remains complete.\u003c/li\u003e\n                        \u003cli\u003eCopying that value into the root may momentarily break our heap property, so we need to\n                            restore it\n                            by\n                            moving\n                            the node down the tree into a legal position. Restoring the heap property is a matter of\n                            continually\n                            swapping the node with the greater of it's two children. We refer to this process as\n                            \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003esiftDown\u003c/code\u003e.\n                        \u003c/li\u003e\n                    \u003c/ol\u003e\n                    \u003ch4 id=\"translating-that-into-array-operations-3\"\u003eTranslating that into array operations:\u003c/h4\u003e\n                    \u003col type=\"1\"\u003e\n                        \u003cli\u003eThe root is at index 1, so save it to return later. The right most node of the bottom level\n                            would\n                            just\n                            be\n                            the very last element of the array. Copy the last element into index 1, and pop off the last\n                            element\n                            (since\n                            it now appears at the root).\u003c/li\u003e\n                        \u003cli\u003eContinually swap the new root toward the back of the array (following our parent-child index\n                            rules)\n                            until\n                            heap property is restored. A node can have two children, so we should always prefer to swap\n                            with the\n                            greater\n                            child.\u003c/li\u003e\n                    \u003c/ol\u003e\n                    \u003ch3 id=\"time-complexity-analysis-2\"\u003eTime Complexity Analysis\u003c/h3\u003e\n                    \u003cul\u003e\n                        \u003cli\u003einsert: \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(log(n))\u003c/code\u003e\u003c/li\u003e\n                        \u003cli\u003edeleteMax: \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(log(n))\u003c/code\u003e\u003c/li\u003e\n                    \u003c/ul\u003e\n                    \u003cp\u003eRecall that our heap will be a complete/balanced tree. This means it's height is\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003elog(n)\u003c/code\u003e\n                        where\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003en\u003c/code\u003e is the number of items. Both\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003einsert\u003c/code\u003e and \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003edeleteMax\u003c/code\u003e have\n                        a time\n                        complexity of \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003elog(n)\u003c/code\u003e because of\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003esiftUp\u003c/code\u003e and \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003esiftDown\u003c/code\u003e\n                        respectively.\n                        In\n                        worst case \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003einsert\u003c/code\u003e, we will have\n                        to \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003esiftUp\u003c/code\u003e a leaf all the way to\n                        the\n                        root of\n                        the\n                        tree.\n                        In the worst case \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003edeleteMax\u003c/code\u003e, we\n                        will have to \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003esiftDown\u003c/code\u003e the new\n                        root all\n                        the way\n                        down to\n                        the leaf level. In either case, we'll have to traverse the full height of the tree,\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003elog(n)\u003c/code\u003e.\n                    \u003c/p\u003e\n                    \u003ch4 id=\"array-heapify-analysis-1\"\u003eArray Heapify Analysis\u003c/h4\u003e\n                    \u003cp\u003eNow that we have established \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003eO(log(n))\u003c/code\u003e for a single insertion, let's analyze the\n                        time\n                        complexity\n                        for\n                        turning an array into a heap (we call this heapify, coming in the next project :)). The\n                        algorithm itself\n                        is\n                        simple, just perform an \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003einsert\u003c/code\u003e\n                        for every element. Since there are \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003en\u003c/code\u003e\n                        elements\n                        and\n                        each\n                        insert requires \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003elog(n)\u003c/code\u003e time, our\n                        total complexity for heapify is\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(nlog(n))\u003c/code\u003e…\n                        Or is\n                        it?\n                        There is actually a tighter bound on heapify. The proof requires some math that you won't find\n                        valuable\n                        in\n                        your\n                        job search, but do understand that the true time complexity of heapify is amortized\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(n)\u003c/code\u003e.\n                        Amortized\n                        refers to the fact that our analysis is about performance over many insertions.\n                    \u003c/p\u003e\n                    \u003ch3 id=\"space-complexity-analysis-1\"\u003eSpace Complexity Analysis\u003c/h3\u003e\n                    \u003cul\u003e\n                        \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(n)\u003c/code\u003e, since we use a single\n                            array to store heap data.\u003c/li\u003e\n                    \u003c/ul\u003e\n                    \u003chr /\u003e\n                    \u003ch2 id=\"heap-sort\"\u003eHeap Sort\u003c/h2\u003e\n                    \u003cp\u003eWe've emphasized heavily that heaps are a \u003cem\u003epartially ordered\u003c/em\u003e data structure. However, we\n                        can\n                        still\n                        leverage heaps in a sorting algorithm to end up with fully sorted array. The strategy is simple\n                        using\n                        our\n                        previous \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eMaxHeap\u003c/code\u003e implementation:\n                    \u003c/p\u003e\n                    \u003col type=\"1\"\u003e\n                        \u003cli\u003ebuild the heap: \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003einsert\u003c/code\u003e all\n                            elements of the array into a \u003ccode class=\"language-javascript  highlight\"\n                                id=\"button\"\u003eMaxHeap\u003c/code\u003e\n                        \u003c/li\u003e\n                        \u003cli\u003econstruct the sorted list: continue to \u003ccode class=\"language-javascript  highlight\"\n                                id=\"button\"\u003edeleteMax\u003c/code\u003e until the heap is empty, every\n                            deletion\n                            will\n                            return the next element in decreasing order\u003c/li\u003e\n                    \u003c/ol\u003e\n                    \u003cp\u003eThe code is straightforward:\u003c/p\u003e\n                    \u003cdiv class=\"sourceCode\" id=\"cb65\"\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=\"cb65-1\" title=\"1\"\u003e\u003cspan class=\"co\"\u003e// assuming our `MaxHeap` from the previous section\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb65-2\" title=\"2\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb65-3\" title=\"3\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003eheapSort\u003c/span\u003e(array) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb65-4\" title=\"4\"\u003e    \u003cspan class=\"co\"\u003e// Step 1: build the heap\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb65-5\" title=\"5\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e heap \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"kw\"\u003enew\u003c/span\u003e \u003cspan class=\"at\"\u003eMaxHeap\u003c/span\u003e()\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb65-6\" title=\"6\"\u003e    \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003eforEach\u003c/span\u003e(num \u003cspan class=\"kw\"\u003e=\u0026gt;\u003c/span\u003e \u003cspan class=\"va\"\u003eheap\u003c/span\u003e.\u003cspan class=\"at\"\u003einsert\u003c/span\u003e(num))\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb65-7\" title=\"7\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb65-8\" title=\"8\"\u003e    \u003cspan class=\"co\"\u003e// Step 2: constructed the sorted array\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb65-9\" title=\"9\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e sorted \u003cspan class=\"op\"\u003e=\u003c/span\u003e []\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb65-10\" title=\"10\"\u003e    \u003cspan class=\"cf\"\u003ewhile\u003c/span\u003e (\u003cspan class=\"va\"\u003eheap\u003c/span\u003e.\u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \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=\"cb65-11\" title=\"11\"\u003e        \u003cspan class=\"va\"\u003esorted\u003c/span\u003e.\u003cspan class=\"at\"\u003epush\u003c/span\u003e(\u003cspan class=\"va\"\u003eheap\u003c/span\u003e.\u003cspan class=\"at\"\u003edeleteMax\u003c/span\u003e())\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb65-12\" title=\"12\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb65-13\" title=\"13\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb65-14\" title=\"14\"\u003e    \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e sorted\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb65-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                    \u003ch3 id=\"time-complexity-analysis-onlogn\"\u003eTime Complexity Analysis: O(nlog(n))\u003c/h3\u003e\n                    \u003cul\u003e\n                        \u003cli\u003e\u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003en\u003c/code\u003e is the size of the input\n                            array\u003c/li\u003e\n                        \u003cli\u003estep-1 requires \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(n)\u003c/code\u003e time as\n                            previously discussed\u003c/li\u003e\n                        \u003cli\u003estep-2's while loop requires \u003ccode class=\"language-javascript  highlight\"\n                                id=\"button\"\u003en\u003c/code\u003e steps in isolation and each\n                            \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003edeleteMax\u003c/code\u003e will\n                            require\n                            \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003elog(n)\u003c/code\u003e steps to restore max\n                            heap property (due to sifting-down). This means\n                            step 2\n                            costs\n                            \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eO(nlog(n))\u003c/code\u003e\n                        \u003c/li\u003e\n                        \u003cli\u003ethe total time complexity of the algorithm is \u003ccode class=\"language-javascript  highlight\"\n                                id=\"button\"\u003eO(n + nlog(n)) = O(nlog(n))\u003c/code\u003e\u003c/li\u003e\n                    \u003c/ul\u003e\n                    \u003ch3 id=\"space-complexity-analysis-2\"\u003eSpace Complexity Analysis:\u003c/h3\u003e\n                    \u003cp\u003eSo \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eheapSort\u003c/code\u003e performs as fast as\n                        our other efficient sorting algorithms, but how does\n                        it fair\n                        in\n                        space complexity? Our implementation above requires an extra \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003eO(n)\u003c/code\u003e amount of space\n                        because\n                        the\n                        heap\n                        is maintained separately from the input array. If we can figure out a way to do all of these\n                        heap\n                        operations\n                        in-place we can get constant \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003eO(1)\u003c/code\u003e space! Let's work on this now.\u003c/p\u003e\n                    \u003ch2 id=\"in-place-heap-sort\"\u003eIn-Place Heap Sort\u003c/h2\u003e\n                    \u003cp\u003eThe in-place algorithm will have the same 2 steps, but it will differ in the implementation\n                        details.\n                        Since we\n                        need to have all operations take place in a single array, we're going to have to denote two\n                        regions of\n                        the\n                        array. That is, we'll need a heap region and a sorted region. We begin by turning the entire\n                        region into\n                        a\n                        heap.\n                        Then we continually delete max to get the next element in increasing order. As the heap region\n                        shrinks,\n                        the\n                        sorted region will grow.\u003c/p\u003e\n                    \u003ch3 id=\"heapify\"\u003eHeapify\u003c/h3\u003e\n                    \u003cp\u003eLet's focus on designing step-1 as an in-place algorithm. In other words, we'll need to reorder\n                        elements\n                        of\n                        the\n                        input array so they follow max heap property. This is usually refered to as\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eheapify\u003c/code\u003e. Our\n                        \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eheapify\u003c/code\u003e will use much of the\n                        same logic as \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eMaxHeap#siftDown\u003c/code\u003e.\n                    \u003c/p\u003e\n                    \u003cdiv class=\"sourceCode\" id=\"cb66\"\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=\"cb66-1\" title=\"1\"\u003e\u003cspan class=\"co\"\u003e// swap the elements at indices i and j of array\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-2\" title=\"2\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003eswap\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e i\u003cspan class=\"op\"\u003e,\u003c/span\u003e j) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-3\" title=\"3\"\u003e    [ array[i]\u003cspan class=\"op\"\u003e,\u003c/span\u003e array[j] ] \u003cspan class=\"op\"\u003e=\u003c/span\u003e [ array[j]\u003cspan class=\"op\"\u003e,\u003c/span\u003e array[i] ]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-4\" title=\"4\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-5\" title=\"5\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-6\" title=\"6\"\u003e\u003cspan class=\"co\"\u003e// sift-down the node at index i until max heap property is restored\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-7\" title=\"7\"\u003e\u003cspan class=\"co\"\u003e// n represents the size of the heap\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-8\" title=\"8\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003eheapify\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e n\u003cspan class=\"op\"\u003e,\u003c/span\u003e i) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-9\" title=\"9\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e leftIdx \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"dv\"\u003e2\u003c/span\u003e \u003cspan class=\"op\"\u003e*\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\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-10\" title=\"10\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e rightIdx \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"dv\"\u003e2\u003c/span\u003e \u003cspan class=\"op\"\u003e*\u003c/span\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=\"cb66-11\" title=\"11\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-12\" title=\"12\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e leftVal \u003cspan class=\"op\"\u003e=\u003c/span\u003e array[leftIdx]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-13\" title=\"13\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e rightVal \u003cspan class=\"op\"\u003e=\u003c/span\u003e array[rightIdx]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-14\" title=\"14\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-15\" title=\"15\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (leftIdx \u003cspan class=\"op\"\u003e\u0026gt;=\u003c/span\u003e n) leftVal \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"op\"\u003e-\u003c/span\u003e\u003cspan class=\"kw\"\u003eInfinity\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-16\" title=\"16\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (rightIdx \u003cspan class=\"op\"\u003e\u0026gt;=\u003c/span\u003e n) rightVal \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"op\"\u003e-\u003c/span\u003e\u003cspan class=\"kw\"\u003eInfinity\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-17\" title=\"17\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-18\" title=\"18\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (array[i] \u003cspan class=\"op\"\u003e\u0026gt;\u003c/span\u003e leftVal \u003cspan class=\"op\"\u003e\u0026amp;\u0026amp;\u003c/span\u003e array[i] \u003cspan class=\"op\"\u003e\u0026gt;\u003c/span\u003e rightVal) \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-19\" title=\"19\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-20\" title=\"20\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e swapIdx\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-21\" title=\"21\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (leftVal \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e rightVal) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-22\" title=\"22\"\u003e        swapIdx \u003cspan class=\"op\"\u003e=\u003c/span\u003e rightIdx\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-23\" title=\"23\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e \u003cspan class=\"cf\"\u003eelse\u003c/span\u003e \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-24\" title=\"24\"\u003e        swapIdx \u003cspan class=\"op\"\u003e=\u003c/span\u003e leftIdx\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-25\" title=\"25\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-26\" title=\"26\"\u003e    \u003cspan class=\"at\"\u003eswap\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e i\u003cspan class=\"op\"\u003e,\u003c/span\u003e swapIdx)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-27\" title=\"27\"\u003e    \u003cspan class=\"at\"\u003eheapify\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e n\u003cspan class=\"op\"\u003e,\u003c/span\u003e swapIdx)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb66-28\" title=\"28\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                    \u003c/div\u003e\n                    \u003cp\u003eWe weren't kidding when we said this would be similar to \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003eMaxHeap#siftDown\u003c/code\u003e. If you\n                        are not\n                        convinced,\n                        flip to the previous section and take a look! The few differences we want to emphasize are:\u003c/p\u003e\n                    \u003cul\u003e\n                        \u003cli\u003eGiven a node at index \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003ei\u003c/code\u003e,\n                            it's left index is \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e2 * i + 1\u003c/code\u003e\n                            and it's\n                            right index\n                            is\n                            \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003e2 * i + 2\u003c/code\u003e\n                            \u003cul\u003e\n                                \u003cli\u003eUsing these as our child index formulas will allow us to avoid using a placeholder\n                                    element\n                                    at\n                                    index\n                                    0. The root of the heap will be at index 0.\u003c/li\u003e\n                            \u003c/ul\u003e\n                        \u003c/li\u003e\n                        \u003cli\u003eThe parameter \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003en\u003c/code\u003e represents\n                            the number of nodes in the heap\n                            \u003cul\u003e\n                                \u003cli\u003eYou may feel that \u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003earray.length\u003c/code\u003e also represents the number of nodes in\n                                    the heap.\n                                    That is\n                                    true, but only in step-1. Later we will need to dynamically state the size of the\n                                    heap.\n                                    Remember, we\n                                    are trying to do this without creating any extra arrays. We'll need to separate the\n                                    heap and\n                                    sorted\n                                    regions of the array and \u003ccode class=\"language-javascript  highlight\"\n                                        id=\"button\"\u003en\u003c/code\u003e will dictate the end of the heap.\u003c/li\u003e\n                            \u003c/ul\u003e\n                        \u003c/li\u003e\n                        \u003cli\u003eWe created a separate \u003ccode class=\"language-javascript  highlight\" id=\"button\"\u003eswap\u003c/code\u003e\n                            helper function.\n                            \u003cul\u003e\n                                \u003cli\u003eNothing fancy here. Swapping will be valuable in step-2 of the algorithm as well, so\n                                    we'll\n                                    want\n                                    to\n                                    keep our code DRY (don't repeat yourself).\u003c/li\u003e\n                            \u003c/ul\u003e\n                        \u003c/li\u003e\n                    \u003c/ul\u003e\n                    \u003cp\u003eTo correctly convert the input array into a heap, we'll need to call \u003ccode\n                            class=\"language-javascript  highlight\" id=\"button\"\u003eheapify\u003c/code\u003e on\n                        children\n                        nodes\n                        before their parents. This is easy to do, just call \u003ccode class=\"language-javascript  highlight\"\n                            id=\"button\"\u003eheapify\u003c/code\u003e on each element\n                        right-to-left\n                        in\n                        the\n                        array:\u003c/p\u003e\n                    \u003cdiv class=\"sourceCode\" id=\"cb67\"\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=\"cb67-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003eheapSort\u003c/span\u003e(array) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb67-2\" title=\"2\"\u003e    \u003cspan class=\"co\"\u003e// heapify the tree from the bottom up\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb67-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=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \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\u0026gt;=\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\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=\"cb67-4\" title=\"4\"\u003e        \u003cspan class=\"at\"\u003eheapify\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\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\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb67-5\" title=\"5\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb67-6\" title=\"6\"\u003e    \u003cspan class=\"co\"\u003e// the entire array is now a heap\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb67-7\" title=\"7\"\u003e    \u003cspan class=\"co\"\u003e// ...\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb67-8\" title=\"8\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                    \u003c/div\u003e\n                    \u003cp\u003eNice! Now the elements of the array have been moved around to obey max heap property.\u003c/p\u003e\n                    \u003ch3 id=\"construct-the-sorted-array\"\u003eConstruct the Sorted Array\u003c/h3\u003e\n                    \u003cp\u003eTo put everything together, we'll need to continually \"delete max\" from our heap. From our\n                        previous\n                        lecture,\n                        we\n                        learned the steps for deletion are to swap the last node of the heap into the root and then sift\n                        the new\n                        root\n                        down to restore max heap property. We'll follow the same logic here, except we'll need to\n                        account for\n                        the\n                        sorted\n                        region of the array. The array will contain the heap region in the front and the sorted region\n                        at the\n                        rear:\n                    \u003c/p\u003e\n                    \u003cdiv class=\"sourceCode\" id=\"cb68\"\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=\"cb68-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003eheapSort\u003c/span\u003e(array) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb68-2\" title=\"2\"\u003e    \u003cspan class=\"co\"\u003e// heapify the tree from the bottom up\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb68-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=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \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\u0026gt;=\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\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=\"cb68-4\" title=\"4\"\u003e        \u003cspan class=\"at\"\u003eheapify\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\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\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb68-5\" title=\"5\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb68-6\" title=\"6\"\u003e    \u003cspan class=\"co\"\u003e// the entire array is now a heap\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb68-7\" title=\"7\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb68-8\" title=\"8\"\u003e    \u003cspan class=\"co\"\u003e// until the heap is empty, continue to \u0026quot;delete max\u0026quot;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb68-9\" title=\"9\"\u003e    \u003cspan class=\"cf\"\u003efor\u003c/span\u003e (\u003cspan class=\"kw\"\u003elet\u003c/span\u003e endOfHeap \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e-\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e endOfHeap \u003cspan class=\"op\"\u003e\u0026gt;=\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e endOfHeap\u003cspan class=\"op\"\u003e--\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb68-10\" title=\"10\"\u003e        \u003cspan class=\"co\"\u003e// swap the root of the heap with the last element of the heap,\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb68-11\" title=\"11\"\u003e        \u003cspan class=\"co\"\u003e// this effecively shrinks the heap by one and grows the sorted array by one\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb68-12\" title=\"12\"\u003e        \u003cspan class=\"at\"\u003eswap\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e endOfHeap\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb68-13\" title=\"13\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb68-14\" title=\"14\"\u003e        \u003cspan class=\"co\"\u003e// sift down the new root, but not past the end of the heap\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb68-15\" title=\"15\"\u003e        \u003cspan class=\"at\"\u003eheapify\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e endOfHeap\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb68-16\" title=\"16\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb68-17\" title=\"17\"\u003e    \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e array\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb68-18\" title=\"18\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                    \u003c/div\u003e\n                    \u003cp\u003eYou'll definitely want to watch the lecture that follows this reading to get a visual of how the\n                        array is\n                        divided\n                        into the heap and sorted regions.\u003c/p\u003e\n                    \u003ch3 id=\"in-place-heap-sort-javascript-implementation\"\u003eIn-Place Heap Sort JavaScript Implementation\n                    \u003c/h3\u003e\n                    \u003cp\u003eHere is the full code for your reference:\u003c/p\u003e\n                    \u003cdiv class=\"sourceCode\" id=\"cb69\"\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=\"cb69-1\" title=\"1\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003eheapSort\u003c/span\u003e(array) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-2\" title=\"2\"\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=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \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\u0026gt;=\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\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=\"cb69-3\" title=\"3\"\u003e        \u003cspan class=\"at\"\u003eheapify\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\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\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-4\" title=\"4\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-5\" title=\"5\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-6\" title=\"6\"\u003e    \u003cspan class=\"cf\"\u003efor\u003c/span\u003e (\u003cspan class=\"kw\"\u003elet\u003c/span\u003e endOfHeap \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"va\"\u003earray\u003c/span\u003e.\u003cspan class=\"at\"\u003elength\u003c/span\u003e \u003cspan class=\"op\"\u003e-\u003c/span\u003e \u003cspan class=\"dv\"\u003e1\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e endOfHeap \u003cspan class=\"op\"\u003e\u0026gt;=\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e endOfHeap\u003cspan class=\"op\"\u003e--\u003c/span\u003e) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-7\" title=\"7\"\u003e        \u003cspan class=\"at\"\u003eswap\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e endOfHeap\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-8\" title=\"8\"\u003e        \u003cspan class=\"at\"\u003eheapify\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e endOfHeap\u003cspan class=\"op\"\u003e,\u003c/span\u003e \u003cspan class=\"dv\"\u003e0\u003c/span\u003e)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-9\" title=\"9\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-10\" title=\"10\"\u003e    \u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-11\" title=\"11\"\u003e    \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e array\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-12\" title=\"12\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-13\" title=\"13\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-14\" title=\"14\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003eheapify\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e n\u003cspan class=\"op\"\u003e,\u003c/span\u003e i) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-15\" title=\"15\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e leftIdx \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"dv\"\u003e2\u003c/span\u003e \u003cspan class=\"op\"\u003e*\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\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-16\" title=\"16\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e rightIdx \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"dv\"\u003e2\u003c/span\u003e \u003cspan class=\"op\"\u003e*\u003c/span\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=\"cb69-17\" title=\"17\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-18\" title=\"18\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e leftVal \u003cspan class=\"op\"\u003e=\u003c/span\u003e array[leftIdx]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-19\" title=\"19\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e rightVal \u003cspan class=\"op\"\u003e=\u003c/span\u003e array[rightIdx]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-20\" title=\"20\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-21\" title=\"21\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (leftIdx \u003cspan class=\"op\"\u003e\u0026gt;=\u003c/span\u003e n) leftVal \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"op\"\u003e-\u003c/span\u003e\u003cspan class=\"kw\"\u003eInfinity\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-22\" title=\"22\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (rightIdx \u003cspan class=\"op\"\u003e\u0026gt;=\u003c/span\u003e n) rightVal \u003cspan class=\"op\"\u003e=\u003c/span\u003e \u003cspan class=\"op\"\u003e-\u003c/span\u003e\u003cspan class=\"kw\"\u003eInfinity\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-23\" title=\"23\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-24\" title=\"24\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (array[i] \u003cspan class=\"op\"\u003e\u0026gt;\u003c/span\u003e leftVal \u003cspan class=\"op\"\u003e\u0026amp;\u0026amp;\u003c/span\u003e array[i] \u003cspan class=\"op\"\u003e\u0026gt;\u003c/span\u003e rightVal) \u003cspan class=\"cf\"\u003ereturn\u003c/span\u003e\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-25\" title=\"25\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-26\" title=\"26\"\u003e    \u003cspan class=\"kw\"\u003elet\u003c/span\u003e swapIdx\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-27\" title=\"27\"\u003e    \u003cspan class=\"cf\"\u003eif\u003c/span\u003e (leftVal \u003cspan class=\"op\"\u003e\u0026lt;\u003c/span\u003e rightVal) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-28\" title=\"28\"\u003e        swapIdx \u003cspan class=\"op\"\u003e=\u003c/span\u003e rightIdx\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-29\" title=\"29\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e \u003cspan class=\"cf\"\u003eelse\u003c/span\u003e \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-30\" title=\"30\"\u003e        swapIdx \u003cspan class=\"op\"\u003e=\u003c/span\u003e leftIdx\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-31\" title=\"31\"\u003e    \u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-32\" title=\"32\"\u003e    \u003cspan class=\"at\"\u003eswap\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e i\u003cspan class=\"op\"\u003e,\u003c/span\u003e swapIdx)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-33\" title=\"33\"\u003e    \u003cspan class=\"at\"\u003eheapify\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e n\u003cspan class=\"op\"\u003e,\u003c/span\u003e swapIdx)\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-34\" title=\"34\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-35\" title=\"35\"\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-36\" title=\"36\"\u003e\u003cspan class=\"kw\"\u003efunction\u003c/span\u003e \u003cspan class=\"at\"\u003eswap\u003c/span\u003e(array\u003cspan class=\"op\"\u003e,\u003c/span\u003e i\u003cspan class=\"op\"\u003e,\u003c/span\u003e j) \u003cspan class=\"op\"\u003e{\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-37\" title=\"37\"\u003e    [ array[i]\u003cspan class=\"op\"\u003e,\u003c/span\u003e array[j] ] \u003cspan class=\"op\"\u003e=\u003c/span\u003e [ array[j]\u003cspan class=\"op\"\u003e,\u003c/span\u003e array[i] ]\u003cspan class=\"op\"\u003e;\u003c/span\u003e\u003c/a\u003e\n\u003ca class=\"sourceLine\" id=\"cb69-38\" title=\"38\"\u003e\u003cspan class=\"op\"\u003e}\u003c/span\u003e\u003c/a\u003e\u003c/code\u003e\u003c/pre\u003e\n                    \u003c/div\u003e\n                    \n                    \n                    \n                    \n                    \n                    \u003cscript async defer\u003e\n                        $( document ).ready( function () {\n                            $( 'code, pre' ).append(\n                                '\u003cspan class=\"command-copy\"\u003e\u003ci class=\"fa fa-clipboard\" aria-hidden=\"true\"\u003e\u003c/i\u003e\u003c/span\u003e'\n                            );\n\n                            $( 'code span.command-copy' ).click( function ( e ) {\n                                var text = $( this ).parent().text().trim(); //.text();\n                                var copyHex = document.createElement( 'input' );\n                                copyHex.value = text\n                                document.body.appendChild( copyHex );\n                                copyHex.select();\n                                document.execCommand( 'copy' );\n                                console.log( copyHex.value )\n                                document.body.removeChild( copyHex );\n                            } );\n\n\n                            $( 'pre span.command-copy' ).click( function ( e ) {\n                                var text = $( this ).parent().text().trim();\n                                var copyHex = document.createElement( 'input' );\n                                copyHex.value = text\n                                document.body.appendChild( copyHex );\n                                copyHex.select();\n                                document.execCommand( 'copy' );\n                                console.log( copyHex.value )\n                                document.body.removeChild( copyHex );\n                            } );\n                        } )\n                    \u003c/script\u003e\n                    \u003cscript async defer\u003e\n                        document.querySelectorAll( 'pre \u003e code' ).forEach( function ( codeBlock ) {\n                            var button = document.createElement( 'button' );\n                            button.className = 'copy-code-button';\n                            button.type = 'button';\n                            button.innerText = 'Copy';\n\n                            var pre = codeBlock.parentNode;\n                            if ( pre.parentNode.classList.contains( 'highlight' ) ) {\n                                var highlight = pre.parentNode;\n                                highlight.parentNode.insertBefore( button, highlight );\n                            } else {\n                                pre.parentNode.insertBefore( button, pre );\n                            }\n                        } );\n                        document.querySelector( 'button' ).addEventListener( 'click', () =\u003e {\n                            const code = document.getElementByTagName( 'code' );\n                            navigator.clipboard.writeText( code.innerHTML );\n                        } );\n                    \u003c/script\u003e\n\n\u003c/body\u003e\n\n\u003c/html\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"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"}