51 lines
12 KiB
HTML
51 lines
12 KiB
HTML
|
<!DOCTYPE html><html lang="en"> <head><meta charset="UTF-8"><meta name="description" content="Evalyte"><meta name="viewport" content="width=device-width"><!--<link rel="icon" type="image/svg+xml" href="/favicon.svg" />--><meta name="generator" content="Astro v5.9.2"><title>undefined - Evalyte</title><script defer src="https://umami.evalyte.net/script.js" data-website-id="4fd77337-0f39-4eac-b19b-c09ebf8c8db3"></script><style>@media (prefers-color-scheme: dark){.markdown-alert{--color-border-default: #30363d;--color-accent-fg: #58a6ff;--color-accent-emphasis: #1f6feb;--color-danger-fg: #f85149;--color-danger-emphasis: #da3633;--color-attention-fg: #d29922;--color-attention-emphasis: #9e6a03;--color-done-fg: #a371f7;--color-done-emphasis: #8957e5;--color-success-fg: #3fb950;--color-success-emphasis: #238636}}@media (prefers-color-scheme: light){.markdown-alert{--color-border-default: #d0d7de;--color-accent-fg: #0969da;--color-accent-emphasis: #0969da;--color-danger-fg: #d1242f;--color-danger-emphasis: #cf222e;--color-attention-fg: #9a6700;--color-attention-emphasis: #9a6700;--color-done-fg: #8250df;--color-done-emphasis: #8250df;--color-success-fg: #1a7f37;--color-success-emphasis: #1f883d}}.markdown-alert{border-left:.25em solid var(--borderColor-default, var(--color-border-default));color:inherit;margin-bottom:16px;padding:.5rem 1em}.markdown-alert>:last-child{margin-bottom:0!important}.markdown-alert .markdown-alert-title{align-items:center;display:flex;font-size:14px;font-weight:500;line-height:1}.markdown-alert .markdown-alert-title svg.octicon{margin-right:8px!important;margin-right:var(--base-size-8,8px)!important;fill:currentColor}.markdown-alert.markdown-alert-note{border-left-color:var(--borderColor-accent-emphasis,var(--color-accent-emphasis))}.markdown-alert.markdown-alert-note .markdown-alert-title{color:var(--color-accent-fg);color:var(--fgColor-accent,var(--color-accent-fg))}.markdown-alert.markdown-alert-tip{border-left-color:var(--borderColor-success-emphasis,var(--color-success-emphasis))}.markdown-alert.markdown-alert-tip .markdown-alert-title{color:var(--color-success-fg);color:var(--fgColor-success,var(--color-success-fg))}.markdown-alert.markdown-alert-important{border-left-color:var(--borderColor-done-emphasis,var(--color-done-emphasis))}.markdown-alert.markdown-alert-important .markdown-alert-title{color:var(--color-done-fg);color:var(--fgColor-done,var(--color-done-fg))}.markdown-alert.markdown-alert-warning{border-left-color:var(--borderColor-attention-emphasis,var(--color-attention-emphasis))}.markdown-alert.markdown-alert-warning .markdown-alert-title{color:var(--color-attention-fg);color:var(--fgColor-attention,var(--color-attention-fg))}.markdown-alert.markdown-alert-caution{border-left-color:var(--borderColor-danger-emphasis,var(--color-danger-emphasis))}.markdown-alert.markdown-alert-caution .markdown-alert-title{color:var(--color-danger-fg);color:var(--fgColor-danger,var(--color-danger-fg))}main[data-astro-cid-hhdr4bp3]{padding:32px;display:flex;flex-direction:column;align-items:center;margin:auto;max-width:1200px;gap:32px}
|
|||
|
@font-face{font-family:Comfortaa;src:url(/fonts/Comfortaa-Regular.ttf);font-weight:400}@font-face{font-family:Comfortaa;src:url(/fonts/Comfortaa-Bold.ttf);font-weight:700}:root{--primary: #5d9eda;--secondary: #b1d0ee;--background: #282c34;--pane: #21252b;--surface: #31353f;--surface2: #51555f;--text: #d1d1d1;--headline: #ffffff;--hint: #797979;color:var(--text);--default-gap: 16px}a{text-decoration:none;color:var(--secondary)}nav>a{color:var(--text)}body{margin:0;min-height:100vh;display:flex;flex-direction:column}html{margin:0;font-family:system-ui,sans-serif;background:var(--background)}main{flex:1}header{background:var(--pane)}header>div{display:flex;flex-direction:row;align-items:center;justify-content:space-between;gap:2rem;margin:auto;padding:16px;font-family:Comfortaa;font-weight:700;font-size:1rem;@media (min-width: 1200px){width:1200px}}nav{display:flex;flex-direction:row;align-items:center;gap:2rem}#logo{font-size:2rem;color:var(--primary);text-wrap:nowrap}h1,h2,h3,h4{font-family:Comfortaa;font-weight:400;color:var(--headline);padding:0}h1{color:var(--primary);font-size:3rem}h2{font-size:2rem}h3{font-size:1.5rem}h4{font-size:1.25rem}code{font-family:Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace}code:not(pre>code){background-color:#21252b;font-size:.9rem;padding:0 4px;border-radius:4px}.astro-code{padding:16px;max-width:80vw}pre{background-color:#21252b!important}.markdown-alert-title{margin-bottom:8px}footer{color:var(--hint);text-align:center;margin-bottom:16px}
|
|||
|
</style></head> <body> <header> <div> <a href="/" id="logo">> _</a> <nav> <a href="https://code.evalyte.net" target="_blank">Repositories</a> <!--<a href="/devlog">Devlog</a>--> <a href="/notes">Notes</a> <!--<a href="/utilities">Utilities</a>--> <!--<a href="/services">Services</a>--> <a href="/about">About</a> </nav> </div> </header> <main data-astro-cid-hhdr4bp3> <div style="display:flex;flex-direction:column;gap:var(--default-gap);justify-content:center;align-items:start"> <div style="display:flex;flex-direction:column;gap:var(--default-gap);justify-content:center;align-items:start"> <h1 data-astro-cid-hhdr4bp3>Quick Sort algorithm</h1> </div> <div style="flex:1"> <p data-astro-cid-hhdr4bp3> <p>To explain the Quick Sort algorithm, let’s take a simple example:</p>
|
|||
|
<p>We have an array of number (integer) in random order, but we want them in ascending order, so we have to sort them, here comes the Quick Sort algorithm.</p>
|
|||
|
<p><em>Note: The range of the numbers and the length of the array have no importance for the algorithm itself, but it will have a performance cost.</em></p>
|
|||
|
<h2 id="algorithm">Algorithm</h2>
|
|||
|
<p>Select a number, called “pivot”, chosen arbritrary from the array itself, put all the numbers lower than the pivot before it and all the numbers greater after it, split the current partition into 2 new partitions (the ones before and after the pivot), and repeat the procedure for each partition until there is no partition left (when the array length is 1).</p>
|
|||
|
<p>The Quick Sort algorithm steps are:</p>
|
|||
|
<ul>
|
|||
|
<li>Select a pivot value from the partition and move it at the end of the partition (P, the pivot value).</li>
|
|||
|
<li>Remember the start index of the current partition (S, the start index).</li>
|
|||
|
<li>for each element (E), if E is lower or equal than P, then swap value E with the value at index S, then increment S.</li>
|
|||
|
<li>Use the pivot P to divide the partition into 2 new partitions.</li>
|
|||
|
<li>Repeat the algorithm for the 2 newly created partitions until there is no partition left.</li>
|
|||
|
</ul>
|
|||
|
<h2 id="implementation">Implementation</h2>
|
|||
|
<h4 id="go">Go</h4>
|
|||
|
<pre class="astro-code github-dark" style="background-color:#24292e;color:#e1e4e8; overflow-x: auto;" tabindex="0" data-language="go"><code><span class="line"><span style="color:#F97583">func</span><span style="color:#B392F0"> Swap</span><span style="color:#E1E4E8">(</span><span style="color:#FFAB70">partition</span><span style="color:#E1E4E8"> []</span><span style="color:#F97583">int</span><span style="color:#E1E4E8">, </span><span style="color:#FFAB70">i</span><span style="color:#F97583"> int</span><span style="color:#E1E4E8">, </span><span style="color:#FFAB70">j</span><span style="color:#F97583"> int</span><span style="color:#E1E4E8">) {</span></span>
|
|||
|
<span class="line"><span style="color:#E1E4E8"> partition[i], partition[j] </span><span style="color:#F97583">=</span><span style="color:#E1E4E8"> partition[j], partition[i]</span></span>
|
|||
|
<span class="line"><span style="color:#E1E4E8">}</span></span>
|
|||
|
<span class="line"></span>
|
|||
|
<span class="line"><span style="color:#F97583">func</span><span style="color:#B392F0"> QuickSort</span><span style="color:#E1E4E8">(</span><span style="color:#FFAB70">partition</span><span style="color:#E1E4E8"> []</span><span style="color:#F97583">int</span><span style="color:#E1E4E8">) {</span></span>
|
|||
|
<span class="line"><span style="color:#B392F0"> Swap</span><span style="color:#E1E4E8">(partition, </span><span style="color:#B392F0">len</span><span style="color:#E1E4E8">(partition) </span><span style="color:#F97583">/</span><span style="color:#79B8FF"> 2</span><span style="color:#E1E4E8">, </span><span style="color:#B392F0">len</span><span style="color:#E1E4E8">(partition) </span><span style="color:#F97583">-</span><span style="color:#79B8FF"> 1</span><span style="color:#E1E4E8">) </span><span style="color:#6A737D">// Select the pivot from the middle (arbitrary) of the partition and swap it with the last element</span></span>
|
|||
|
<span class="line"></span>
|
|||
|
<span class="line"><span style="color:#E1E4E8"> startIndex </span><span style="color:#F97583">:=</span><span style="color:#79B8FF"> 0</span></span>
|
|||
|
<span class="line"><span style="color:#E1E4E8"> pivot </span><span style="color:#F97583">:=</span><span style="color:#E1E4E8"> partition[</span><span style="color:#B392F0">len</span><span style="color:#E1E4E8">(partition) </span><span style="color:#F97583">-</span><span style="color:#79B8FF"> 1</span><span style="color:#E1E4E8">] </span><span style="color:#6A737D">// Since we already swapped the pivot, we get the value directly from the end of the partition</span></span>
|
|||
|
<span class="line"></span>
|
|||
|
<span class="line"><span style="color:#F97583"> for</span><span style="color:#E1E4E8"> i </span><span style="color:#F97583">:=</span><span style="color:#E1E4E8"> startIndex; i </span><span style="color:#F97583"><</span><span style="color:#B392F0"> len</span><span style="color:#E1E4E8">(partition); i</span><span style="color:#F97583">++</span><span style="color:#E1E4E8"> {</span></span>
|
|||
|
<span class="line"><span style="color:#F97583"> if</span><span style="color:#E1E4E8"> partition[i] </span><span style="color:#F97583"><=</span><span style="color:#E1E4E8"> pivot {</span></span>
|
|||
|
<span class="line"><span style="color:#B392F0"> Swap</span><span style="color:#E1E4E8">(partition, i, startIndex)</span></span>
|
|||
|
<span class="line"><span style="color:#E1E4E8"> startIndex</span><span style="color:#F97583">++</span></span>
|
|||
|
<span class="line"><span style="color:#E1E4E8"> }</span></span>
|
|||
|
<span class="line"><span style="color:#E1E4E8"> }</span></span>
|
|||
|
<span class="line"></span>
|
|||
|
<span class="line"><span style="color:#6A737D"> // startIndex now point at the first element of the values greater or equal than the pivot</span></span>
|
|||
|
<span class="line"><span style="color:#F97583"> if</span><span style="color:#E1E4E8"> startIndex </span><span style="color:#F97583">></span><span style="color:#79B8FF"> 1</span><span style="color:#E1E4E8"> {</span></span>
|
|||
|
<span class="line"><span style="color:#B392F0"> QuickSort</span><span style="color:#E1E4E8">(partition[:startIndex </span><span style="color:#F97583">-</span><span style="color:#79B8FF"> 1</span><span style="color:#E1E4E8">])</span></span>
|
|||
|
<span class="line"><span style="color:#E1E4E8"> }</span></span>
|
|||
|
<span class="line"></span>
|
|||
|
<span class="line"><span style="color:#F97583"> if</span><span style="color:#B392F0"> len</span><span style="color:#E1E4E8">(partition) </span><span style="color:#F97583">-</span><span style="color:#E1E4E8"> startIndex </span><span style="color:#F97583">></span><span style="color:#79B8FF"> 1</span><span style="color:#E1E4E8"> {</span></span>
|
|||
|
<span class="line"><span style="color:#B392F0"> QuickSort</span><span style="color:#E1E4E8">(partition[startIndex:])</span></span>
|
|||
|
<span class="line"><span style="color:#E1E4E8"> }</span></span>
|
|||
|
<span class="line"><span style="color:#E1E4E8">}</span></span></code></pre>
|
|||
|
<h2 id="performance-speed">Performance (speed)</h2>
|
|||
|
<p>Average: O(n*log(n))<br>
|
|||
|
Best case: O(n*log(n))<br>
|
|||
|
Worst case: O(n²)</p>
|
|||
|
<h2 id="sources">Sources</h2>
|
|||
|
<p><a href="https://fr.wikipedia.org/wiki/Tri_rapide">https://fr.wikipedia.org/wiki/Tri_rapide</a> (fr)<br>
|
|||
|
<a href="https://en.wikipedia.org/wiki/Quicksort">https://en.wikipedia.org/wiki/Quicksort</a> (en)<br>
|
|||
|
My experience.</p> </p> </div> </div> </main> <footer>© 2024 Evalyte</footer> </body></html>
|