Skip to content

Commit

Permalink
Deployed 7bd2dae with MkDocs version: 1.5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
cheung4843 committed Feb 26, 2024
1 parent 6080967 commit 8135db8
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 49 deletions.
184 changes: 184 additions & 0 deletions algorithm/basic_algo/binary_search/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,17 @@



<label class="md-nav__link md-nav__link--active" for="__toc">


<span class="md-ellipsis">
Binary Search
</span>


<span class="md-nav__icon md-icon"></span>
</label>

<a href="./" class="md-nav__link md-nav__link--active">


Expand All @@ -1315,6 +1326,52 @@

</a>



<nav class="md-nav md-nav--secondary" aria-label="目錄">






<label class="md-nav__title" for="__toc">
<span class="md-nav__icon md-icon"></span>
目錄
</label>
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>

<li class="md-nav__item">
<a href="#introduction" class="md-nav__link">
<span class="md-ellipsis">
Introduction
</span>
</a>

</li>

<li class="md-nav__item">
<a href="#complexity" class="md-nav__link">
<span class="md-ellipsis">
Complexity
</span>
</a>

</li>

<li class="md-nav__item">
<a href="#implementation" class="md-nav__link">
<span class="md-ellipsis">
Implementation
</span>
</a>

</li>

</ul>

</nav>

</li>


Expand Down Expand Up @@ -1754,6 +1811,41 @@



<label class="md-nav__title" for="__toc">
<span class="md-nav__icon md-icon"></span>
目錄
</label>
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>

<li class="md-nav__item">
<a href="#introduction" class="md-nav__link">
<span class="md-ellipsis">
Introduction
</span>
</a>

</li>

<li class="md-nav__item">
<a href="#complexity" class="md-nav__link">
<span class="md-ellipsis">
Complexity
</span>
</a>

</li>

<li class="md-nav__item">
<a href="#implementation" class="md-nav__link">
<span class="md-ellipsis">
Implementation
</span>
</a>

</li>

</ul>

</nav>
</div>
</div>
Expand Down Expand Up @@ -1784,6 +1876,98 @@


<h1 id="binary_search">Binary Search<a class="headerlink" href="#binary_search" title="Permanent link"></a></h1>
<h2 id="introduction">Introduction<a class="headerlink" href="#introduction" title="Permanent link"></a></h2>
<p>Binary Search(二元搜尋法) 是<em>分治法(Divide and Conquer)</em>的一種應用,非常有效率,但只適用在<strong>單調(Monotonic)</strong>的序列或函數上,也就是說,必須是遞增或遞減的,或是你簡單理解成<em>排序過(sorted)</em>的,舉幾個例子:</p>
<ol>
<li><code>[1, 2, 3, 4, 5]</code></li>
<li><code>['a', 'b', 'c', 'd', 'e']</code></li>
<li><code>[32, 16, 8, 4, 2, 1]</code></li>
<li><span class="arithmatex">\(f(x)=x^2, \ x \ge 0,\ [f(1), f(2), f(3), f(4), f(5)]\)</span></li>
</ol>
<p>函數 <span class="arithmatex">\(f(x)=x^2\)</span><span class="arithmatex">\(x \ge 0\)</span> 時是遞增的:</p>
<p><img alt="" src="../media/binary_search_1.png" /></p>
<div class="language-python highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-0-1">1</a></span>
<span class="normal"><a href="#__codelineno-0-2">2</a></span>
<span class="normal"><a href="#__codelineno-0-3">3</a></span>
<span class="normal"><a href="#__codelineno-0-4">4</a></span>
<span class="normal"><a href="#__codelineno-0-5">5</a></span>
<span class="normal"><a href="#__codelineno-0-6">6</a></span>
<span class="normal"><a href="#__codelineno-0-7">7</a></span>
<span class="normal"><a href="#__codelineno-0-8">8</a></span>
<span class="normal"><a href="#__codelineno-0-9">9</a></span></pre></div></td><td class="code"><div><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1"></a><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
</span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2"></a> <span class="k">return</span> <span class="n">x</span> <span class="o">**</span> <span class="mi">2</span>
</span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3"></a>
</span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4"></a>
</span><span id="__span-0-5"><a id="__codelineno-0-5" name="__codelineno-0-5"></a><span class="n">xs</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">]</span>
</span><span id="__span-0-6"><a id="__codelineno-0-6" name="__codelineno-0-6"></a><span class="nb">print</span><span class="p">(</span><span class="n">xs</span><span class="p">)</span>
</span><span id="__span-0-7"><a id="__codelineno-0-7" name="__codelineno-0-7"></a><span class="c1"># [f(1), f(2), f(3), f(4), f(5)]</span>
</span><span id="__span-0-8"><a id="__codelineno-0-8" name="__codelineno-0-8"></a><span class="n">ys</span> <span class="o">=</span> <span class="p">[</span><span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">xs</span><span class="p">]</span>
</span><span id="__span-0-9"><a id="__codelineno-0-9" name="__codelineno-0-9"></a><span class="nb">print</span><span class="p">(</span><span class="n">ys</span><span class="p">)</span>
</span></code></pre></div></td></tr></table></div>
<div class="language-text highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">Output</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-1-1">1</a></span>
<span class="normal"><a href="#__codelineno-1-2">2</a></span></pre></div></td><td class="code"><div><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1"></a>[1, 2, 3, 4, 5]
</span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2"></a>[1, 4, 9, 16, 25]
</span></code></pre></div></td></tr></table></div>
<p>最後一個例子是想跟你強調,<em>函數也可以用二元搜尋法</em>,例如想要找第一個 <span class="arithmatex">\(f(x) \ge 10\)</span> 的整數 <span class="arithmatex">\(x\)</span>,我們可以用二元搜尋法找到 <span class="arithmatex">\(x=4\)</span></p>
<h2 id="complexity">Complexity<a class="headerlink" href="#complexity" title="Permanent link"></a></h2>
<p>玩過猜數字嗎?你每猜一次,我會告訴你猜的數字是太大還是太小,讓你從 <span class="arithmatex">\([1, 100]\)</span> 間猜一個數字,怎麼猜會最有效率?如果你第一次就猜 <span class="arithmatex">\(50\)</span>,我告訴你猜的數字太大,你就可以知道答案一定在 <span class="arithmatex">\([1, 49]\)</span> 之間,這樣你就可以<em>省下一半</em>的時間,你可以再猜 <span class="arithmatex">\(25\)</span>,而我告訴你猜的數字太小,那麼答案一定在 <span class="arithmatex">\([26, 49]\)</span> 之間,你又省下一半的時間,這就是二元搜尋法的精神。</p>
<video muted controls autoplay loop>
<source src="../../basic_algo/media/binary_search_2.mp4" type="video/mp4">
</video>

<p>那最多要猜幾次呢?假設你真的很衰,猜到剩下 <span class="arithmatex">\([47, 48]\)</span> 這兩個數字在選的時候又猜錯,你猜了 <span class="arithmatex">\(47\)</span> 而我告訴你猜太小,那就只剩下 <span class="arithmatex">\(48\)</span> 了,也就是區間 <span class="arithmatex">\([48, 48]\)</span> <strong>剩下一個元素,這是我們的終止條件。</strong></p>
<p>考慮一般的情況,有 <span class="arithmatex">\(n\)</span> 個數字,每次猜都可以把區間縮小一半,設我們要猜 <span class="arithmatex">\(k\)</span> 次才能把區間縮小到剩下一個元素:</p>
<div class="arithmatex">\[
\frac{n}{2^k} = 1
\]</div>
<div class="arithmatex">\[
\Rightarrow n = 2^k
\]</div>
<div class="arithmatex">\[
\Rightarrow \log_2 n = k
\]</div>
<p>那麼時間複雜度就是 <span class="arithmatex">\(\mathcal{O}(k) = \mathcal{O}(\log n)\)</span></p>
<h2 id="implementation">Implementation<a class="headerlink" href="#implementation" title="Permanent link"></a></h2>
<p>在寫程式之前,先來看動畫,有一個已排序過的串列 <code>[1, 1, 4, 5, 7, 8, 9, 10, 11, 12]</code>,我們要找 <code>4</code>:</p>
<video muted controls autoplay loop>
<source src="../../basic_algo/media/binary_search_3.mp4" type="video/mp4">
</video>

<p>再來看程式碼:</p>
<div class="language-python highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">binary_search.py</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-2-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-2-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-2-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-2-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-2-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-2-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-2-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-2-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-2-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-2-10">10</a></span>
<span class="normal"><a href="#__codelineno-2-11">11</a></span>
<span class="normal"><a href="#__codelineno-2-12">12</a></span>
<span class="normal"><a href="#__codelineno-2-13">13</a></span>
<span class="normal"><a href="#__codelineno-2-14">14</a></span>
<span class="normal"><a href="#__codelineno-2-15">15</a></span>
<span class="normal"><a href="#__codelineno-2-16">16</a></span></pre></div></td><td class="code"><div><pre><span></span><code><span id="__span-2-1"><a id="__codelineno-2-1" name="__codelineno-2-1"></a><span class="k">def</span> <span class="nf">binary_search</span><span class="p">(</span><span class="n">lst</span><span class="p">,</span> <span class="n">target</span><span class="p">):</span>
</span><span id="__span-2-2"><a id="__codelineno-2-2" name="__codelineno-2-2"></a> <span class="n">left</span><span class="p">,</span> <span class="n">right</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">lst</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
</span><span id="__span-2-3"><a id="__codelineno-2-3" name="__codelineno-2-3"></a>
</span><span id="__span-2-4"><a id="__codelineno-2-4" name="__codelineno-2-4"></a> <span class="k">while</span> <span class="n">left</span> <span class="o">&lt;=</span> <span class="n">right</span><span class="p">:</span>
</span><span id="__span-2-5"><a id="__codelineno-2-5" name="__codelineno-2-5"></a> <span class="n">mid</span> <span class="o">=</span> <span class="p">(</span><span class="n">left</span> <span class="o">+</span> <span class="n">right</span><span class="p">)</span> <span class="o">//</span> <span class="mi">2</span>
</span><span id="__span-2-6"><a id="__codelineno-2-6" name="__codelineno-2-6"></a> <span class="k">if</span> <span class="n">lst</span><span class="p">[</span><span class="n">mid</span><span class="p">]</span> <span class="o">==</span> <span class="n">target</span><span class="p">:</span>
</span><span id="__span-2-7"><a id="__codelineno-2-7" name="__codelineno-2-7"></a> <span class="k">return</span> <span class="n">mid</span>
</span><span id="__span-2-8"><a id="__codelineno-2-8" name="__codelineno-2-8"></a> <span class="k">elif</span> <span class="n">lst</span><span class="p">[</span><span class="n">mid</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">target</span><span class="p">:</span>
</span><span id="__span-2-9"><a id="__codelineno-2-9" name="__codelineno-2-9"></a> <span class="n">left</span> <span class="o">=</span> <span class="n">mid</span> <span class="o">+</span> <span class="mi">1</span>
</span><span id="__span-2-10"><a id="__codelineno-2-10" name="__codelineno-2-10"></a> <span class="k">else</span><span class="p">:</span>
</span><span id="__span-2-11"><a id="__codelineno-2-11" name="__codelineno-2-11"></a> <span class="n">right</span> <span class="o">=</span> <span class="n">mid</span> <span class="o">-</span> <span class="mi">1</span>
</span><span id="__span-2-12"><a id="__codelineno-2-12" name="__codelineno-2-12"></a>
</span><span id="__span-2-13"><a id="__codelineno-2-13" name="__codelineno-2-13"></a> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span>
</span><span id="__span-2-14"><a id="__codelineno-2-14" name="__codelineno-2-14"></a>
</span><span id="__span-2-15"><a id="__codelineno-2-15" name="__codelineno-2-15"></a><span class="n">nums</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">]</span>
</span><span id="__span-2-16"><a id="__codelineno-2-16" name="__codelineno-2-16"></a><span class="nb">print</span><span class="p">(</span><span class="n">binary_search</span><span class="p">(</span><span class="n">nums</span><span class="p">,</span> <span class="mi">4</span><span class="p">))</span>
</span></code></pre></div></td></tr></table></div>
<div class="language-text highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">Output</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-3-1">1</a></span></pre></div></td><td class="code"><div><pre><span></span><code><span id="__span-3-1"><a id="__codelineno-3-1" name="__codelineno-3-1"></a>2
</span></code></pre></div></td></tr></table></div>



Expand Down
2 changes: 1 addition & 1 deletion algorithm/basic_algo/linear_search/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1916,7 +1916,7 @@ <h2 id="introduction">Introduction<a class="headerlink" href="#introduction" tit
<p>這應該是最簡單的演算法之一,不過我們可以來複習上一章所學的時間複雜度。</p>
<h2 id="implementation">Implementation<a class="headerlink" href="#implementation" title="Permanent link"></a></h2>
<p>直接來看程式碼,定義 <code>linear_search(lst, target)</code> 函式,這個函式接受一個串列 <code>lst</code> 和一個目標值 <code>target</code>,回傳目標值在串列中的索引,如果目標值不存在就回傳 <code>-1</code></p>
<div class="language-python highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-0-1"> 1</a></span>
<div class="language-python highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">linear_search.py</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-0-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-0-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-0-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-0-4"> 4</a></span>
Expand Down
Binary file added algorithm/basic_algo/media/binary_search_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added algorithm/basic_algo/media/binary_search_2.mp4
Binary file not shown.
Binary file added algorithm/basic_algo/media/binary_search_3.mp4
Binary file not shown.
Loading

0 comments on commit 8135db8

Please sign in to comment.