elements from <code>b</code>.</p><p>Time complexity: <spanclass="math math-inline"><spanclass="katex"><spanclass="katex-mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mimathvariant="script">O</mi><mostretchy="false">(</mo><mi>m</mi><mo>+</mo><mi>n</mi><mostretchy="false">)</mo></mrow><annotationencoding="application/x-tex">\mathcal{O}(m + n)</annotation></semantics></math></span><spanclass="katex-html"aria-hidden="true"><spanclass="base"><spanclass="strut"style="height:1em;vertical-align:-0.25em"></span><spanclass="mord mathcal"style="margin-right:0.02778em">O</span><spanclass="mopen">(</span><spanclass="mord mathnormal">m</span><spanclass="mspace"style="margin-right:0.2222em"></span><spanclass="mbin">+</span><spanclass="mspace"style="margin-right:0.2222em"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical-align:-0.25em"></span><spanclass="mord mathnormal">n</span><spanclass="mclose">)</span></span></span></span></span>, where <spanclass="math math-inline"><spanclass="katex"><spanclass="katex-mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi><moseparator="true">,</mo><mi>n</mi></mrow><annotationencoding="application/x-tex">m, n</annotation></semantics></math></span><spanclass="katex-html"aria-hidden="true"><spanclass="base"><spanclass="strut"style="height:0.625em;vertical-align:-0.1944em"></span><spanclass="mord mathnormal">m</span><spanclass="mpunct">,</span><spanclass="mspace"style="margin-right:0.1667em"></span><spanclass="mord mathnormal">n</span></span></span></span></span> denote the length of
<code>a</code> and <code>b</code> respectively.</p><p>Space complexity: <spanclass="math math-inline"><spanclass="katex"><spanclass="katex-mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mimathvariant="script">O</mi><mostretchy="false">(</mo><mi>m</mi><mo>+</mo><mi>n</mi><mostretchy="false">)</mo></mrow><annotationencoding="application/x-tex">\mathcal{O}(m + n)</annotation></semantics></math></span><spanclass="katex-html"aria-hidden="true"><spanclass="base"><spanclass="strut"style="height:1em;vertical-align:-0.25em"></span><spanclass="mord mathcal"style="margin-right:0.02778em">O</span><spanclass="mopen">(</span><spanclass="mord mathnormal">m</span><spanclass="mspace"style="margin-right:0.2222em"></span><spanclass="mbin">+</span><spanclass="mspace"style="margin-right:0.2222em"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical-align:-0.25em"></span><spanclass="mord mathnormal">n</span><spanclass="mclose">)</span></span></span></span></span>, where <spanclass="math math-inline"><spanclass="katex"><spanclass="katex-mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi><moseparator="true">,</mo><mi>n</mi></mrow><annotationencoding="application/x-tex">m, n</annotation></semantics></math></span><spanclass="katex-html"aria-hidden="true"><spanclass="base"><spanclass="strut"style="height:0.625em;vertical-align:-0.1944em"></span><spanclass="mord mathnormal">m</span><spanclass="mpunct">,</span><spanclass="mspace"style="margin-right:0.1667em"></span><spanclass="mord mathnormal">n</span></span></span></span></span> denote the length of
<imgloading="lazy"alt="Rendered construction of the list"src="/assets/images/construction_dark-fac28e7cafcc43d7e2fb5f0b6c25504e.svg#gh-dark-mode-only"width="851"height="276"class="img_ev3q"></p><p>Let us assume that you extend the result with the list that you get from the recursive call.</p><ul><li><p>B iterates through 1, 2 and 3; returns <code>[1, 2, 3]</code></p></li><li><p>C iterates through 4, 5 and 6; returns <code>[4, 5, 6]</code></p></li><li><p>D iterates through 7, 8 and 9; returns <code>[7, 8, 9]</code></p></li><li><p>now we return those lists to the calls from A), so each of the <code>extend</code> calls iterates through:</p><ul><li>1, 2, 3 that was returned from B</li><li>4, 5, 6 that was returned from C</li><li>7, 8, 9 that was returned from D</li></ul><p>and returns <code>[1, 2, 3, 4, 5, 6, 7, 8, 9]</code></p></li></ul><p>If the recursion had bigger depth and/or more elements, it would iterate through them more than twice, therefore it does not take constant time to do nor some constant multiple of the input, since it traverses all of the elements in each of the levels.</p><h2class="anchor anchorWithStickyNavbar_LWe7"id="implementation-of-extend">Implementation of <code>extend</code><ahref="#implementation-of-extend"class="hash-link"aria-label="Direct link to implementation-of-extend"title="Direct link to implementation-of-extend"></a></h2><p>There is an example of dynamic array:</p><ul><li><ahref="/files/ib002/time-complexity/extend/dynlist.h"target="_blank"rel="noopener noreferrer">interface (<code>dynlist.h</code>)</a></li><li><ahref="/files/ib002/time-complexity/extend/dynlist.c"target="_blank"rel="noopener noreferrer">implementation (<code>dynlist.c</code>)</a></li></ul><p>For the sake of <em>Algorithms and Data Structures I</em> we consider <code>APPEND</code> operation, i.e. adding the element to the end of the list, to have time complexity <spanclass="math math-inline"><spanclass="katex"><spanclass="katex-mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mimathvariant="script">O</mi><mostretchy="false">(</mo><mn>1</mn><mostretchy="false">)</mo></mrow><annotationencoding="application/x-tex">\mathcal{O}(1)</annotation></semantics></math></span><spanclass="katex-html"aria-hidden="true"><spanclass="base"><spanclass="strut"style="height:1em;vertical-align:-0.25em"></span><spanclass="mord mathcal"style="margin-right:0.02778em">O</span><spanclass="mopen">(</span><spanclass="mord">1</span><spanclass="mclose">)</span></span></span></span></span> (<strong>amortized</strong>; which is out of the scope of IB002).</p><p>If we have a look at the <code>extend</code> implementation in this dynamic array example:</p><divclass="language-c codeBlockContainer_Ckt0 theme-code-block"style="--prism-color:#000000;--prism-background-color:#ffffff"><divclass="codeBlockContent_biex"><pretabindex="0"class="prism-code language-c codeBlock_bY9V thin-scrollbar"><codeclass="codeBlockLines_e6Vv codeBlockLinesWithNumbering_o6Pm"><spanclass="token-line codeLine_lJS_"style="color:#000000"><spanclass="codeLineNumber_Tfdd"></span><spanclass="codeLineContent_feaV"><spanclass="token keyword"style="color:rgb(0, 0, 255)">void</span><spanclass="token plain"></span><spanclass="token function"style="color:rgb(0, 0, 255)">dynamic_array_extend</span><spanclass="token punctuation"style="color:rgb(4, 81, 165)">(</span><spanclass="token keyword"style="color:rgb(0, 0, 255)">struct</span><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(38, 127, 153)">dynamic_array_t</span><spanclass="token plain"></span><spanclass="token operator"style="color:rgb(0, 0, 0)">*</span><spanclass="token plain">arr</span><spanclass="token punctuation"style="color:rgb(4, 81, 165)">,</span><spanclass="token plain"></span><spanclass="token keyword"style="color:rgb(0, 0, 255)">struct</span><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(38, 127, 153)">dynamic_array_t</span><spanclass="token plain"></span><spanclas