import{_ as a,o as l,c as p,k as s,a as o,X as n}from"./chunks/framework.6e839c56.js";const S=JSON.parse('{"title":"Algorithm","description":"","frontmatter":{},"headers":[],"relativePath":"en/api/packages/algorithm.md","filePath":"en/api/packages/algorithm.md"}'),e={name:"en/api/packages/algorithm.md"},t=s("h1",{id:"Algorithm",tabindex:"-1"},[o("Algorithm "),s("a",{class:"header-anchor",href:"#Algorithm","aria-label":'Permalink to "Algorithm"'},"​")],-1),c=s("p",null,"Package algorithm implements some basic algorithm. eg. sort, search.",-1),r=s("div",{STYLE:"page-break-after: always;"},null,-1),y=n('<h2 id="Source" tabindex="-1">Source <a class="header-anchor" href="#Source" aria-label="Permalink to &quot;Source&quot;">​</a></h2><ul><li><a href="https://github.com/duke-git/lancet/blob/main/algorithm/sort.go" target="_blank" rel="noreferrer">https://github.com/duke-git/lancet/blob/main/algorithm/sort.go</a></li><li><a href="https://github.com/duke-git/lancet/blob/main/algorithm/search.go" target="_blank" rel="noreferrer">https://github.com/duke-git/lancet/blob/main/algorithm/search.go</a></li><li><a href="https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go" target="_blank" rel="noreferrer">https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go</a></li></ul>',2),F=s("div",{STYLE:"page-break-after: always;"},null,-1),i=n(`<link rel="stylesheet" type="text/css" href="/styles/api_doc.css"><h2 id="Usage" tabindex="-1">Usage <a class="header-anchor" href="#Usage" aria-label="Permalink to &quot;Usage&quot;">​</a></h2><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span></code></pre></div>`,3),A=s("div",{STYLE:"page-break-after: always;"},null,-1),E=n('<h2 id="Index" tabindex="-1">Index <a class="header-anchor" href="#Index" aria-label="Permalink to &quot;Index&quot;">​</a></h2><ul><li><a href="#BubbleSort">BubbleSort</a></li><li><a href="#InsertionSort">InsertionSort</a></li><li><a href="#SelectionSort">SelectionSort</a></li><li><a href="#ShellSort">ShellSort</a></li><li><a href="#QuickSort">QuickSort</a></li><li><a href="#HeapSort">HeapSort</a></li><li><a href="#MergeSort">MergeSort</a></li><li><a href="#CountSort">CountSort</a></li><li><a href="#BinarySearch">BinarySearch</a></li><li><a href="#BinaryIterativeSearch">BinaryIterativeSearch</a></li><li><a href="#LinearSearch">LinearSearch</a></li><li><a href="#LRUCache">LRUCache</a></li></ul>',2),u=s("div",{STYLE:"page-break-after: always;"},null,-1),m=n(`<h2 id="Documentation" tabindex="-1">Documentation <a class="header-anchor" href="#Documentation" aria-label="Permalink to &quot;Documentation&quot;">​</a></h2><h3 id="BubbleSort" tabindex="-1"><span id="BubbleSort">BubbleSort</span> <a class="header-anchor" href="#BubbleSort" aria-label="Permalink to &quot;&lt;span id=&quot;BubbleSort&quot;&gt;BubbleSort&lt;/span&gt;&quot;">​</a></h3><p>Sort slice with bubble sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> BubbleSort</span><span style="color:#999999;">[</span><span style="color:#393A34;">T any</span><span style="color:#999999;">](</span><span style="color:#393A34;">slice </span><span style="color:#999999;">[]</span><span style="color:#393A34;">T</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator lancetconstraints</span><span style="color:#999999;">.</span><span style="color:#393A34;">Comparator</span><span style="color:#999999;">)</span></span></code></pre></div><p><b>Example: <span class="run-container"><a href="https://go.dev/play/p/GNdv7Jg2Taj" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">intComparator</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">intComparator) </span><span style="color:#62E884;">Compare</span><span style="color:#F6F6F4;">(v1 any, v2 any) </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">    val1, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v1.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    val2, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v2.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">//ascending order</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    } </span><span style="color:#F286C4;">else</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&gt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    numbers </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> []</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">6</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">}</span></span>
<span class="line"><span style="color:#F6F6F4;">    comparator </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">&amp;</span><span style="color:#F6F6F4;">intComparator{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    algorithm.</span><span style="color:#97E1F1;">BubbleSort</span><span style="color:#F6F6F4;">(numbers, comparator)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(numbers)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// [1 2 3 4 5 6]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">intComparator</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Compare</span><span style="color:#999999;">(</span><span style="color:#393A34;">v1 any</span><span style="color:#999999;">,</span><span style="color:#393A34;"> v2 any</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">val1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v1</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">val2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v2</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">//ascending order</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">else</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&gt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">numbers</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">6</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">comparator</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&amp;</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    algorithm</span><span style="color:#999999;">.</span><span style="color:#998418;">BubbleSort</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// [1 2 3 4 5 6]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="InsertionSort" tabindex="-1"><span id="InsertionSort">InsertionSort</span> <a class="header-anchor" href="#InsertionSort" aria-label="Permalink to &quot;&lt;span id=&quot;InsertionSort&quot;&gt;InsertionSort&lt;/span&gt;&quot;">​</a></h3><p>Sort slice with insertion sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> InsertionSort</span><span style="color:#999999;">[</span><span style="color:#393A34;">T any</span><span style="color:#999999;">](</span><span style="color:#393A34;">slice </span><span style="color:#999999;">[]</span><span style="color:#393A34;">T</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator lancetconstraints</span><span style="color:#999999;">.</span><span style="color:#393A34;">Comparator</span><span style="color:#999999;">)</span></span></code></pre></div><p><b>Example: <span class="run-container"><a href="https://go.dev/play/p/G5LJiWgJJW6" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">people</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">    Name </span><span style="color:#97E1F1;font-style:italic;">string</span></span>
<span class="line"><span style="color:#F6F6F4;">    Age  </span><span style="color:#97E1F1;font-style:italic;">int</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7B7F8B;">// PeopleAageComparator sort people slice by age field</span></span>
<span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">peopleAgeComparator</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7B7F8B;">// Compare implements github.com/duke-git/lancet/lancetconstraints/constraints.go/Comparator</span></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (pc </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">peopleAgeComparator) </span><span style="color:#62E884;">Compare</span><span style="color:#F6F6F4;">(v1 any, v2 any) </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">    p1, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v1.(people)</span></span>
<span class="line"><span style="color:#F6F6F4;">    p2, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v2.(people)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">//ascending order</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> p1.Age </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> p2.Age {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    } </span><span style="color:#F286C4;">else</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> p1.Age </span><span style="color:#F286C4;">&gt;</span><span style="color:#F6F6F4;"> p2.Age {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    peoples </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> []people{</span></span>
<span class="line"><span style="color:#F6F6F4;">        {Name: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, Age: </span><span style="color:#BF9EEE;">20</span><span style="color:#F6F6F4;">},</span></span>
<span class="line"><span style="color:#F6F6F4;">        {Name: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, Age: </span><span style="color:#BF9EEE;">10</span><span style="color:#F6F6F4;">},</span></span>
<span class="line"><span style="color:#F6F6F4;">        {Name: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, Age: </span><span style="color:#BF9EEE;">17</span><span style="color:#F6F6F4;">},</span></span>
<span class="line"><span style="color:#F6F6F4;">        {Name: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, Age: </span><span style="color:#BF9EEE;">8</span><span style="color:#F6F6F4;">},</span></span>
<span class="line"><span style="color:#F6F6F4;">        {Name: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">e</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, Age: </span><span style="color:#BF9EEE;">28</span><span style="color:#F6F6F4;">},</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    comparator </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">&amp;</span><span style="color:#F6F6F4;">peopleAgeComparator{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    algorithm.</span><span style="color:#97E1F1;">InsertionSort</span><span style="color:#F6F6F4;">(peoples, comparator)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(peoples)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// [{d 8} {b 10} {c 17} {a 20} {e 28}]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">people</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    Name </span><span style="color:#AB5959;">string</span></span>
<span class="line"><span style="color:#393A34;">    Age  </span><span style="color:#AB5959;">int</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A0ADA0;">// PeopleAageComparator sort people slice by age field</span></span>
<span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">peopleAgeComparator</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#A0ADA0;">// Compare implements github.com/duke-git/lancet/lancetconstraints/constraints.go/Comparator</span></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">pc </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">peopleAgeComparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Compare</span><span style="color:#999999;">(</span><span style="color:#393A34;">v1 any</span><span style="color:#999999;">,</span><span style="color:#393A34;"> v2 any</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">p1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v1</span><span style="color:#999999;">.(</span><span style="color:#393A34;">people</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">p2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v2</span><span style="color:#999999;">.(</span><span style="color:#393A34;">people</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">//ascending order</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> p1</span><span style="color:#999999;">.</span><span style="color:#393A34;">Age </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> p2</span><span style="color:#999999;">.</span><span style="color:#393A34;">Age </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">else</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> p1</span><span style="color:#999999;">.</span><span style="color:#393A34;">Age </span><span style="color:#AB5959;">&gt;</span><span style="color:#393A34;"> p2</span><span style="color:#999999;">.</span><span style="color:#393A34;">Age </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">peoples</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#393A34;">people</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">{</span><span style="color:#393A34;">Name</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> Age</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">20</span><span style="color:#999999;">},</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">{</span><span style="color:#393A34;">Name</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> Age</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">10</span><span style="color:#999999;">},</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">{</span><span style="color:#393A34;">Name</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> Age</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">17</span><span style="color:#999999;">},</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">{</span><span style="color:#393A34;">Name</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> Age</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">8</span><span style="color:#999999;">},</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">{</span><span style="color:#393A34;">Name</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">e</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> Age</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">28</span><span style="color:#999999;">},</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">comparator</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&amp;</span><span style="color:#393A34;">peopleAgeComparator</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    algorithm</span><span style="color:#999999;">.</span><span style="color:#998418;">InsertionSort</span><span style="color:#999999;">(</span><span style="color:#393A34;">peoples</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">peoples</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// [{d 8} {b 10} {c 17} {a 20} {e 28}]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="SelectionSort" tabindex="-1"><span id="SelectionSort">SelectionSort</span> <a class="header-anchor" href="#SelectionSort" aria-label="Permalink to &quot;&lt;span id=&quot;SelectionSort&quot;&gt;SelectionSort&lt;/span&gt;&quot;">​</a></h3><p>Sort slice with selection sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> SelectionSort</span><span style="color:#999999;">[</span><span style="color:#393A34;">T any</span><span style="color:#999999;">](</span><span style="color:#393A34;">slice </span><span style="color:#999999;">[]</span><span style="color:#393A34;">T</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator lancetconstraints</span><span style="color:#999999;">.</span><span style="color:#393A34;">Comparator</span><span style="color:#999999;">)</span></span></code></pre></div><p><b>Example: <span class="run-container"><a href="https://go.dev/play/p/oXovbkekayS" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">intComparator</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">intComparator) </span><span style="color:#62E884;">Compare</span><span style="color:#F6F6F4;">(v1 any, v2 any) </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">    val1, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v1.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    val2, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v2.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">//ascending order</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    } </span><span style="color:#F286C4;">else</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&gt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    numbers </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> []</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">6</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">}</span></span>
<span class="line"><span style="color:#F6F6F4;">    comparator </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">&amp;</span><span style="color:#F6F6F4;">intComparator{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    algorithm.</span><span style="color:#97E1F1;">SelectionSort</span><span style="color:#F6F6F4;">(numbers, comparator)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(numbers)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// [1 2 3 4 5 6]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">intComparator</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Compare</span><span style="color:#999999;">(</span><span style="color:#393A34;">v1 any</span><span style="color:#999999;">,</span><span style="color:#393A34;"> v2 any</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">val1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v1</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">val2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v2</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">//ascending order</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">else</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&gt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">numbers</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">6</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">comparator</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&amp;</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    algorithm</span><span style="color:#999999;">.</span><span style="color:#998418;">SelectionSort</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// [1 2 3 4 5 6]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="ShellSort" tabindex="-1"><span id="ShellSort">ShellSort</span> <a class="header-anchor" href="#ShellSort" aria-label="Permalink to &quot;&lt;span id=&quot;ShellSort&quot;&gt;ShellSort&lt;/span&gt;&quot;">​</a></h3><p>Sort slice with shell sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> ShellSort[T any](slice []T, comparator lancetconstraints.Comparator)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> ShellSort</span><span style="color:#999999;">[</span><span style="color:#393A34;">T any</span><span style="color:#999999;">](</span><span style="color:#393A34;">slice </span><span style="color:#999999;">[]</span><span style="color:#393A34;">T</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator lancetconstraints</span><span style="color:#999999;">.</span><span style="color:#393A34;">Comparator</span><span style="color:#999999;">)</span></span></code></pre></div><p><b>Example: <span class="run-container"><a href="https://go.dev/play/p/3ibkszpJEu3" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">intComparator</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">intComparator) </span><span style="color:#62E884;">Compare</span><span style="color:#F6F6F4;">(v1 any, v2 any) </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">    val1, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v1.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    val2, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v2.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">//ascending order</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    } </span><span style="color:#F286C4;">else</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&gt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    numbers </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> []</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">6</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">}</span></span>
<span class="line"><span style="color:#F6F6F4;">    comparator </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">&amp;</span><span style="color:#F6F6F4;">intComparator{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    algorithm.</span><span style="color:#97E1F1;">ShellSort</span><span style="color:#F6F6F4;">(numbers, comparator)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(numbers)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// [1 2 3 4 5 6]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">intComparator</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Compare</span><span style="color:#999999;">(</span><span style="color:#393A34;">v1 any</span><span style="color:#999999;">,</span><span style="color:#393A34;"> v2 any</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">val1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v1</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">val2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v2</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">//ascending order</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">else</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&gt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">numbers</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">6</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">comparator</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&amp;</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    algorithm</span><span style="color:#999999;">.</span><span style="color:#998418;">ShellSort</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// [1 2 3 4 5 6]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="QuickSort" tabindex="-1"><span id="QuickSort">QuickSort</span> <a class="header-anchor" href="#QuickSort" aria-label="Permalink to &quot;&lt;span id=&quot;QuickSort&quot;&gt;QuickSort&lt;/span&gt;&quot;">​</a></h3><p>Sort slice with quick sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> QuickSort[T any](slice []T comparator lancetconstraints.Comparator)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> QuickSort</span><span style="color:#999999;">[</span><span style="color:#393A34;">T any</span><span style="color:#999999;">](</span><span style="color:#393A34;">slice </span><span style="color:#999999;">[]</span><span style="color:#393A34;">T comparator lancetconstraints</span><span style="color:#999999;">.</span><span style="color:#393A34;">Comparator</span><span style="color:#999999;">)</span></span></code></pre></div><p><b>Example:<span class="run-container"><a href="https://go.dev/play/p/7Y7c1Elk3ax" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">intComparator</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">intComparator) </span><span style="color:#62E884;">Compare</span><span style="color:#F6F6F4;">(v1 any, v2 any) </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">    val1, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v1.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    val2, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v2.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">//ascending order</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    } </span><span style="color:#F286C4;">else</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&gt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    numbers </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> []</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">6</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">}</span></span>
<span class="line"><span style="color:#F6F6F4;">    comparator </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">&amp;</span><span style="color:#F6F6F4;">intComparator{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    algorithm.</span><span style="color:#97E1F1;">QuickSort</span><span style="color:#F6F6F4;">(numbers, comparator)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(numbers)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// [1 2 3 4 5 6]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">intComparator</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Compare</span><span style="color:#999999;">(</span><span style="color:#393A34;">v1 any</span><span style="color:#999999;">,</span><span style="color:#393A34;"> v2 any</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">val1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v1</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">val2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v2</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">//ascending order</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">else</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&gt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">numbers</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">6</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">comparator</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&amp;</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    algorithm</span><span style="color:#999999;">.</span><span style="color:#998418;">QuickSort</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// [1 2 3 4 5 6]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="HeapSort" tabindex="-1"><span id="HeapSort">HeapSort</span> <a class="header-anchor" href="#HeapSort" aria-label="Permalink to &quot;&lt;span id=&quot;HeapSort&quot;&gt;HeapSort&lt;/span&gt;&quot;">​</a></h3><p>Sort slice with heap sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> HeapSort[T any](slice []T, comparator lancetconstraints.Comparator)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> HeapSort</span><span style="color:#999999;">[</span><span style="color:#393A34;">T any</span><span style="color:#999999;">](</span><span style="color:#393A34;">slice </span><span style="color:#999999;">[]</span><span style="color:#393A34;">T</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator lancetconstraints</span><span style="color:#999999;">.</span><span style="color:#393A34;">Comparator</span><span style="color:#999999;">)</span></span></code></pre></div><p><b>Example: <span class="run-container"><a href="https://go.dev/play/p/u6Iwa1VZS_f" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">intComparator</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">intComparator) </span><span style="color:#62E884;">Compare</span><span style="color:#F6F6F4;">(v1 any, v2 any) </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">    val1, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v1.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    val2, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v2.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">//ascending order</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    } </span><span style="color:#F286C4;">else</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&gt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    numbers </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> []</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">6</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">}</span></span>
<span class="line"><span style="color:#F6F6F4;">    comparator </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">&amp;</span><span style="color:#F6F6F4;">intComparator{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    algorithm.</span><span style="color:#97E1F1;">HeapSort</span><span style="color:#F6F6F4;">(numbers, comparator)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(numbers)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// [1 2 3 4 5 6]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">intComparator</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Compare</span><span style="color:#999999;">(</span><span style="color:#393A34;">v1 any</span><span style="color:#999999;">,</span><span style="color:#393A34;"> v2 any</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">val1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v1</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">val2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v2</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">//ascending order</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">else</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&gt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">numbers</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">6</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">comparator</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&amp;</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    algorithm</span><span style="color:#999999;">.</span><span style="color:#998418;">HeapSort</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// [1 2 3 4 5 6]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="MergeSort" tabindex="-1"><span id="MergeSort">MergeSort</span> <a class="header-anchor" href="#MergeSort" aria-label="Permalink to &quot;&lt;span id=&quot;MergeSort&quot;&gt;MergeSort&lt;/span&gt;&quot;">​</a></h3><p>Sort slice with merge sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> MergeSort[T any](slice []T, comparator lancetconstraints.Comparator)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> MergeSort</span><span style="color:#999999;">[</span><span style="color:#393A34;">T any</span><span style="color:#999999;">](</span><span style="color:#393A34;">slice </span><span style="color:#999999;">[]</span><span style="color:#393A34;">T</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator lancetconstraints</span><span style="color:#999999;">.</span><span style="color:#393A34;">Comparator</span><span style="color:#999999;">)</span></span></code></pre></div><p><b>Example: <span class="run-container"><a href="https://go.dev/play/p/ydinn9YzUJn" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">intComparator</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">intComparator) </span><span style="color:#62E884;">Compare</span><span style="color:#F6F6F4;">(v1 any, v2 any) </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">    val1, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v1.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    val2, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v2.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">//ascending order</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    } </span><span style="color:#F286C4;">else</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&gt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    numbers </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> []</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">6</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">}</span></span>
<span class="line"><span style="color:#F6F6F4;">    comparator </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">&amp;</span><span style="color:#F6F6F4;">intComparator{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    algorithm.</span><span style="color:#97E1F1;">MergeSort</span><span style="color:#F6F6F4;">(numbers, comparator)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(numbers)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// [1 2 3 4 5 6]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">intComparator</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Compare</span><span style="color:#999999;">(</span><span style="color:#393A34;">v1 any</span><span style="color:#999999;">,</span><span style="color:#393A34;"> v2 any</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">val1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v1</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">val2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v2</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">//ascending order</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">else</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&gt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">numbers</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">6</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">comparator</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&amp;</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    algorithm</span><span style="color:#999999;">.</span><span style="color:#998418;">MergeSort</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// [1 2 3 4 5 6]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="CountSort" tabindex="-1"><span id="CountSort">CountSort</span> <a class="header-anchor" href="#CountSort" aria-label="Permalink to &quot;&lt;span id=&quot;CountSort&quot;&gt;CountSort&lt;/span&gt;&quot;">​</a></h3><p>Sort slice with count sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> CountSort</span><span style="color:#999999;">[</span><span style="color:#393A34;">T any</span><span style="color:#999999;">](</span><span style="color:#393A34;">slice </span><span style="color:#999999;">[]</span><span style="color:#393A34;">T</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator lancetconstraints</span><span style="color:#999999;">.</span><span style="color:#393A34;">Comparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#393A34;">T</span></span></code></pre></div><p><b>Example: <span class="run-container"><a href="https://go.dev/play/p/tB-Umgm0DrP" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">intComparator</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">intComparator) </span><span style="color:#62E884;">Compare</span><span style="color:#F6F6F4;">(v1 any, v2 any) </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">    val1, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v1.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    val2, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v2.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">//ascending order</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    } </span><span style="color:#F286C4;">else</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&gt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    numbers </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> []</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">6</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">}</span></span>
<span class="line"><span style="color:#F6F6F4;">    comparator </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">&amp;</span><span style="color:#F6F6F4;">intComparator{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    sortedNums </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> algorithm.</span><span style="color:#97E1F1;">CountSort</span><span style="color:#F6F6F4;">(numbers, comparator)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(sortedNums)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// [1 2 3 4 5 6]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">intComparator</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Compare</span><span style="color:#999999;">(</span><span style="color:#393A34;">v1 any</span><span style="color:#999999;">,</span><span style="color:#393A34;"> v2 any</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">val1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v1</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">val2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v2</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">//ascending order</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">else</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&gt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">numbers</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">6</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">comparator</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&amp;</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">sortedNums</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> algorithm</span><span style="color:#999999;">.</span><span style="color:#998418;">CountSort</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">sortedNums</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// [1 2 3 4 5 6]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="BinarySearch" tabindex="-1"><span id="BinarySearch">BinarySearch</span> <a class="header-anchor" href="#BinarySearch" aria-label="Permalink to &quot;&lt;span id=&quot;BinarySearch&quot;&gt;BinarySearch&lt;/span&gt;&quot;">​</a></h3><p>BinarySearch search for target within a sorted slice, recursive call itself. If a target is found, the index of the target is returned. Else the function return -1.</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">, comparator lancetconstraints.Comparator) </span><span style="color:#97E1F1;font-style:italic;">int</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> BinarySearch</span><span style="color:#999999;">[</span><span style="color:#393A34;">T any</span><span style="color:#999999;">](</span><span style="color:#393A34;">sortedSlice </span><span style="color:#999999;">[]</span><span style="color:#393A34;">T</span><span style="color:#999999;">,</span><span style="color:#393A34;"> target T</span><span style="color:#999999;">,</span><span style="color:#393A34;"> lowIndex</span><span style="color:#999999;">,</span><span style="color:#393A34;"> highIndex </span><span style="color:#AB5959;">int</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator lancetconstraints</span><span style="color:#999999;">.</span><span style="color:#393A34;">Comparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span></span></code></pre></div><p><b>Example: <span class="run-container"><a href="https://go.dev/play/p/t6MeGiUSN47" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">intComparator</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">intComparator) </span><span style="color:#62E884;">Compare</span><span style="color:#F6F6F4;">(v1 any, v2 any) </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">    val1, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v1.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    val2, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v2.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">//ascending order</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    } </span><span style="color:#F286C4;">else</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&gt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    numbers </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> []</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">6</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">7</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">8</span><span style="color:#F6F6F4;">}</span></span>
<span class="line"><span style="color:#F6F6F4;">    comparator </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">&amp;</span><span style="color:#F6F6F4;">intComparator{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    result1 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> algorithm.</span><span style="color:#97E1F1;">BinarySearch</span><span style="color:#F6F6F4;">(numbers, </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">0</span><span style="color:#F6F6F4;">, </span><span style="color:#97E1F1;">len</span><span style="color:#F6F6F4;">(numbers)</span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, comparator)</span></span>
<span class="line"><span style="color:#F6F6F4;">    result2 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> algorithm.</span><span style="color:#97E1F1;">BinarySearch</span><span style="color:#F6F6F4;">(numbers, </span><span style="color:#BF9EEE;">9</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">0</span><span style="color:#F6F6F4;">, </span><span style="color:#97E1F1;">len</span><span style="color:#F6F6F4;">(numbers)</span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, comparator)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result1)</span></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result2)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 4</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// -1</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">intComparator</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Compare</span><span style="color:#999999;">(</span><span style="color:#393A34;">v1 any</span><span style="color:#999999;">,</span><span style="color:#393A34;"> v2 any</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">val1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v1</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">val2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v2</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">//ascending order</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">else</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&gt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">numbers</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">6</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">7</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">8</span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">comparator</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&amp;</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result1</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> algorithm</span><span style="color:#999999;">.</span><span style="color:#998418;">BinarySearch</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#998418;">len</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">)</span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result2</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> algorithm</span><span style="color:#999999;">.</span><span style="color:#998418;">BinarySearch</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">9</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#998418;">len</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">)</span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result1</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result2</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 4</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// -1</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="BinaryIterativeSearch" tabindex="-1"><span id="BinaryIterativeSearch">BinaryIterativeSearch</span> <a class="header-anchor" href="#BinaryIterativeSearch" aria-label="Permalink to &quot;&lt;span id=&quot;BinaryIterativeSearch&quot;&gt;BinaryIterativeSearch&lt;/span&gt;&quot;">​</a></h3><p>BinaryIterativeSearch search for target within a sorted slice, recursive call itself. If a target is found, the index of the target is returned. Else the function return -1.</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">, comparator lancetconstraints.Comparator) </span><span style="color:#97E1F1;font-style:italic;">int</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> BinaryIterativeSearch</span><span style="color:#999999;">[</span><span style="color:#393A34;">T any</span><span style="color:#999999;">](</span><span style="color:#393A34;">sortedSlice </span><span style="color:#999999;">[]</span><span style="color:#393A34;">T</span><span style="color:#999999;">,</span><span style="color:#393A34;"> target T</span><span style="color:#999999;">,</span><span style="color:#393A34;"> lowIndex</span><span style="color:#999999;">,</span><span style="color:#393A34;"> highIndex </span><span style="color:#AB5959;">int</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator lancetconstraints</span><span style="color:#999999;">.</span><span style="color:#393A34;">Comparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span></span></code></pre></div><p><b>Example: <span class="run-container"><a href="https://go.dev/play/p/Anozfr8ZLH3" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">intComparator</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (c </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">intComparator) </span><span style="color:#62E884;">Compare</span><span style="color:#F6F6F4;">(v1 any, v2 any) </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">    val1, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v1.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    val2, _ </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> v2.(</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">//ascending order</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    } </span><span style="color:#F286C4;">else</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> val1 </span><span style="color:#F286C4;">&gt;</span><span style="color:#F6F6F4;"> val2 {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    numbers </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> []</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">6</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">7</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">8</span><span style="color:#F6F6F4;">}</span></span>
<span class="line"><span style="color:#F6F6F4;">    comparator </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">&amp;</span><span style="color:#F6F6F4;">intComparator{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    result1 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> algorithm.</span><span style="color:#97E1F1;">BinaryIterativeSearch</span><span style="color:#F6F6F4;">(numbers, </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">0</span><span style="color:#F6F6F4;">, </span><span style="color:#97E1F1;">len</span><span style="color:#F6F6F4;">(numbers)</span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, comparator)</span></span>
<span class="line"><span style="color:#F6F6F4;">    result2 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> algorithm.</span><span style="color:#97E1F1;">BinaryIterativeSearch</span><span style="color:#F6F6F4;">(numbers, </span><span style="color:#BF9EEE;">9</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">0</span><span style="color:#F6F6F4;">, </span><span style="color:#97E1F1;">len</span><span style="color:#F6F6F4;">(numbers)</span><span style="color:#F286C4;">-</span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, comparator)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result1)</span></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result2)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 4</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// -1</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">intComparator</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">c </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Compare</span><span style="color:#999999;">(</span><span style="color:#393A34;">v1 any</span><span style="color:#999999;">,</span><span style="color:#393A34;"> v2 any</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">val1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v1</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">val2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">_</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> v2</span><span style="color:#999999;">.(</span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">//ascending order</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">else</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">if</span><span style="color:#393A34;"> val1 </span><span style="color:#AB5959;">&gt;</span><span style="color:#393A34;"> val2 </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">numbers</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">6</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">7</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">8</span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">comparator</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&amp;</span><span style="color:#393A34;">intComparator</span><span style="color:#999999;">{}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result1</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> algorithm</span><span style="color:#999999;">.</span><span style="color:#998418;">BinaryIterativeSearch</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#998418;">len</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">)</span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result2</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> algorithm</span><span style="color:#999999;">.</span><span style="color:#998418;">BinaryIterativeSearch</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">9</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#998418;">len</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">)</span><span style="color:#AB5959;">-</span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> comparator</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result1</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result2</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 4</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// -1</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="LinearSearch" tabindex="-1"><span id="LinearSearch">LinearSearch</span> <a class="header-anchor" href="#LinearSearch" aria-label="Permalink to &quot;&lt;span id=&quot;LinearSearch&quot;&gt;LinearSearch&lt;/span&gt;&quot;">​</a></h3><p>return the index of target in slice base on equal function.If a target is found, the index of the target is returned. Else the function return -1.</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> LinearSearch[T any](slice []T, target T, equal </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(a, b T) </span><span style="color:#97E1F1;font-style:italic;">bool</span><span style="color:#F6F6F4;">) </span><span style="color:#97E1F1;font-style:italic;">int</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> LinearSearch</span><span style="color:#999999;">[</span><span style="color:#393A34;">T any</span><span style="color:#999999;">](</span><span style="color:#393A34;">slice </span><span style="color:#999999;">[]</span><span style="color:#393A34;">T</span><span style="color:#999999;">,</span><span style="color:#393A34;"> target T</span><span style="color:#999999;">,</span><span style="color:#393A34;"> equal </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">a</span><span style="color:#999999;">,</span><span style="color:#393A34;"> b T</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">bool</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span></span></code></pre></div><p><b>Example: <span class="run-container"><a href="https://go.dev/play/p/IsS7rgn5s3x" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    numbers </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> []</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    equalFunc </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(a, b </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) </span><span style="color:#97E1F1;font-style:italic;">bool</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> a </span><span style="color:#F286C4;">==</span><span style="color:#F6F6F4;"> b</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    result1 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> algorithm.</span><span style="color:#97E1F1;">LinearSearch</span><span style="color:#F6F6F4;">(numbers, </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">, equalFunc)</span></span>
<span class="line"><span style="color:#F6F6F4;">    result2 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> algorithm.</span><span style="color:#97E1F1;">LinearSearch</span><span style="color:#F6F6F4;">(numbers, </span><span style="color:#BF9EEE;">6</span><span style="color:#F6F6F4;">, equalFunc)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result1)</span></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result2)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 0</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// -1</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">numbers</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">equalFunc</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">a</span><span style="color:#999999;">,</span><span style="color:#393A34;"> b </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">bool</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> a </span><span style="color:#AB5959;">==</span><span style="color:#393A34;"> b</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result1</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> algorithm</span><span style="color:#999999;">.</span><span style="color:#998418;">LinearSearch</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span><span style="color:#393A34;"> equalFunc</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result2</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> algorithm</span><span style="color:#999999;">.</span><span style="color:#998418;">LinearSearch</span><span style="color:#999999;">(</span><span style="color:#393A34;">numbers</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">6</span><span style="color:#999999;">,</span><span style="color:#393A34;"> equalFunc</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result1</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result2</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 0</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// -1</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="LRUCache" tabindex="-1"><span id="LRUCache">LRUCache</span> <a class="header-anchor" href="#LRUCache" aria-label="Permalink to &quot;&lt;span id=&quot;LRUCache&quot;&gt;LRUCache&lt;/span&gt;&quot;">​</a></h3><p>LRUCache implements mem cache with lru.</p><p><b>Signature:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> NewLRUCache[K comparable, V any](capacity </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">LRUCache[K, V]</span></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (l </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">LRUCache[K, V]) </span><span style="color:#62E884;">Get</span><span style="color:#F6F6F4;">(key K) (V, </span><span style="color:#97E1F1;font-style:italic;">bool</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (l </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">LRUCache[K, V]) </span><span style="color:#62E884;">Put</span><span style="color:#F6F6F4;">(key K, value V)</span></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (l </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">LRUCache[K, V]) </span><span style="color:#62E884;">Delete</span><span style="color:#F6F6F4;">(key K) </span><span style="color:#97E1F1;font-style:italic;">bool</span></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (l </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">LRUCache[K, V]) </span><span style="color:#62E884;">Len</span><span style="color:#F6F6F4;">() </span><span style="color:#97E1F1;font-style:italic;">int</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> NewLRUCache</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">](</span><span style="color:#393A34;">capacity </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">LRUCache</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V</span><span style="color:#999999;">]</span></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">l </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">LRUCache</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Get</span><span style="color:#999999;">(</span><span style="color:#393A34;">key K</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">V</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">bool</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">l </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">LRUCache</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Put</span><span style="color:#999999;">(</span><span style="color:#393A34;">key K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> value V</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">l </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">LRUCache</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Delete</span><span style="color:#999999;">(</span><span style="color:#393A34;">key K</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">bool</span></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">l </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">LRUCache</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Len</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span></span></code></pre></div><p><b>Example: <span class="run-container"><a href="https://go.dev/play/p/IsS7rgn5s3x" target="_blank" rel="noreferrer">Run</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    cache </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> algorithm.NewLRUCache[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">, </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">](</span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    cache.</span><span style="color:#97E1F1;">Put</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    cache.</span><span style="color:#97E1F1;">Put</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    result1, ok1 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> cache.</span><span style="color:#97E1F1;">Get</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    result2, ok2 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> cache.</span><span style="color:#97E1F1;">Get</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    result3, ok3 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> cache.</span><span style="color:#97E1F1;">Get</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result1, ok1)</span></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result2, ok2)</span></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result3, ok3)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(cache.</span><span style="color:#97E1F1;">Len</span><span style="color:#F6F6F4;">())</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    ok </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> cache.</span><span style="color:#97E1F1;">Delete</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(ok)</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 1 true</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 2 true</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 0 false</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 2</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// true</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/algorithm</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">cache</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> algorithm</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewLRUCache</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#999999;">](</span><span style="color:#2F798A;">2</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    cache</span><span style="color:#999999;">.</span><span style="color:#998418;">Put</span><span style="color:#999999;">(</span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    cache</span><span style="color:#999999;">.</span><span style="color:#998418;">Put</span><span style="color:#999999;">(</span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">ok1</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> cache</span><span style="color:#999999;">.</span><span style="color:#998418;">Get</span><span style="color:#999999;">(</span><span style="color:#2F798A;">1</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">ok2</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> cache</span><span style="color:#999999;">.</span><span style="color:#998418;">Get</span><span style="color:#999999;">(</span><span style="color:#2F798A;">2</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result3</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">ok3</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> cache</span><span style="color:#999999;">.</span><span style="color:#998418;">Get</span><span style="color:#999999;">(</span><span style="color:#2F798A;">3</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> ok1</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> ok2</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result3</span><span style="color:#999999;">,</span><span style="color:#393A34;"> ok3</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">cache</span><span style="color:#999999;">.</span><span style="color:#998418;">Len</span><span style="color:#999999;">())</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">ok</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> cache</span><span style="color:#999999;">.</span><span style="color:#998418;">Delete</span><span style="color:#999999;">(</span><span style="color:#2F798A;">2</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">ok</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 1 true</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 2 true</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 0 false</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 2</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// true</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div>`,73),g=[t,c,r,y,F,i,A,E,u,m];function d(h,B,C,v,f,b){return l(),p("div",null,g)}const D=a(e,[["render",d]]);export{S as __pageData,D as default};
