<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alibek</title>
    <description>The latest articles on DEV Community by Alibek (@alikawp).</description>
    <link>https://dev.to/alikawp</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2986750%2F69b9af01-aafc-4fea-b9cc-5e69f45c6891.jpg</url>
      <title>DEV Community: Alibek</title>
      <link>https://dev.to/alikawp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alikawp"/>
    <language>en</language>
    <item>
      <title>Go for Sort: Part 2 - Expanding the Fundamentals and Implementing Few Other Sorting Algorithms</title>
      <dc:creator>Alibek</dc:creator>
      <pubDate>Tue, 29 Apr 2025 14:49:11 +0000</pubDate>
      <link>https://dev.to/alikawp/go-for-sort-part-2-expanding-the-fundamentals-and-implementing-few-other-sorting-algorithms-3hbp</link>
      <guid>https://dev.to/alikawp/go-for-sort-part-2-expanding-the-fundamentals-and-implementing-few-other-sorting-algorithms-3hbp</guid>
      <description>&lt;p&gt;Hello, fellow developers! Welcome back to the "Go for Sort" series. In the previous &lt;a href="https://dev.to/alikawp/go-for-sort-part-1-exploring-the-fundamentals-and-implementing-insertion-sort-2go5"&gt;post&lt;/a&gt;, we laid the groundwork by exploring the fundamentals of sorting and implementing the Insertion Sort algorithm. Today, we're taking the next step in our journey to understand common sorting techniques by diving deep into &lt;strong&gt;Selection Sort and Bubble Sort&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As a friendly reminder, all code examples in this series are provided in Go. However, the underlying logic and concepts are universal, so if you're familiar with any programming language, you should be able to follow along without any issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Selection Sort: Finding the Minimum
&lt;/h2&gt;

&lt;p&gt;Let's start with &lt;strong&gt;Selection Sort&lt;/strong&gt;. The name itself gives us a strong hint about how this algorithm works: it repeatedly &lt;em&gt;selects&lt;/em&gt; something. But what exactly does it select?&lt;/p&gt;

&lt;p&gt;At its core, Selection Sort focuses on finding the element with the &lt;strong&gt;minimum value&lt;/strong&gt; in the unsorted portion of the array at each iteration. Imagine you're organizing a deck of cards and want to put them in ascending order. Selection Sort is like scanning through the unsorted cards, picking out the smallest one, and placing it at the beginning of the sorted pile.&lt;/p&gt;

&lt;p&gt;Here's how it works step-by-step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Iteration:&lt;/strong&gt; We iterate through the input array using an outer loop, from the first element up to the second-to-last element.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Finding the Minimum:&lt;/strong&gt; For each position in the outer loop (let's say at index &lt;code&gt;i&lt;/code&gt;), we scan the rest of the unsorted part of the array (from index &lt;code&gt;i + 1&lt;/code&gt; to the end) to find the index of the smallest element. We'll call this &lt;code&gt;minIndex&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Swapping:&lt;/strong&gt; Once we've found the &lt;code&gt;minIndex&lt;/code&gt;, if it's different from the current index &lt;code&gt;i&lt;/code&gt;, we swap the element at &lt;code&gt;i&lt;/code&gt; with the element at &lt;code&gt;minIndex&lt;/code&gt;. This places the smallest element found so far into its correct sorted position at the beginning of the unsorted portion.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This process ensures that with each pass of the outer loop, the smallest remaining element is moved to its correct sorted position.&lt;/p&gt;

&lt;p&gt;Here's the Go implementation of Selection Sort:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;SelectionSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;minIndex&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;minIndex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;minIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;minIndex&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;minIndex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;minIndex&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="m"&gt;78&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;89&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;345&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;123&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Unsorted:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;SelectionSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sorted:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Time Complexity of Selection Sort
&lt;/h2&gt;

&lt;p&gt;The time complexity of Selection Sort is O(n²) in all cases (best, average, and worst). This is because the outer loop runs &lt;code&gt;n-1&lt;/code&gt; times, and for each iteration of the outer loop, the inner loop runs approximately &lt;code&gt;n-i&lt;/code&gt; times. Even if the array is already sorted, the algorithm will still perform the same number of comparisons. While it's simple to understand and implement, its quadratic time complexity makes it inefficient for large datasets.&lt;/p&gt;

&lt;h2&gt;
  
  
  When Might You Use Selection Sort?
&lt;/h2&gt;

&lt;p&gt;While not the most efficient general-purpose sorting algorithm, Selection Sort has some niche advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Simplicity&lt;/strong&gt;: It's very easy to understand and implement.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Minimal Swaps&lt;/strong&gt;: Selection Sort performs a minimal number of swaps (O(n) in the worst case). This can be beneficial in scenarios where swapping elements is a costly operation.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Bubble Sort: Let the Largest Elements Rise
&lt;/h2&gt;

&lt;p&gt;Now, let's move on to our second algorithm for today: Bubble Sort. The name of this algorithm comes from the way larger elements "bubble" to the end of the array with each pass. As the heading suggests, Selection Sort focuses on finding the minimum value, while Bubble Sort focuses on moving the largest to the end. Other than that, both algorithms perform swaps after finding smallest and largest elements respectively. But there's a slight difference in how they perform swaps — let's dive in!&lt;/p&gt;

&lt;p&gt;Imagine again our deck of cards (I know, next time I'll use something funnier as an example). Now, instead of picking out the smallest card directly, think of going through the deck, one pair at a time. If two adjacent cards are in the wrong order (e.g., a 7 comes before a 3 when we want ascending order), we swap them. We repeat this process, going through the deck multiple times. With each pass, the larger (or smaller, depending on the desired order) cards will gradually "bubble" their way towards their correct position at the end of the deck.&lt;/p&gt;

&lt;p&gt;Here's the process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Iteration&lt;/strong&gt;: We iterate through the array multiple times.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Comparison and Swapping&lt;/strong&gt;: In each pass, we compare each pair of adjacent elements. If the element on the left is greater than the element on the right, we swap them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optimization (Optional)&lt;/strong&gt;: After each pass, the largest unsorted element is guaranteed to be in its correct position at the end of the unsorted portion. This means we can reduce the number of comparisons in subsequent passes. If no swaps occur during a pass, it indicates that the array is already sorted, and we can terminate the algorithm early.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here's the Go implementation of Bubble Sort:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;BubbleSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;swapped&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;swapped&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;swapped&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                &lt;span class="n"&gt;swapped&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="c"&gt;// Optimization: Reduce the number of comparisons in subsequent passes&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="m"&gt;78&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;89&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;345&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;123&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Unsorted:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;BubbleSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sorted:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Time Complexity of Bubble Sort
&lt;/h2&gt;

&lt;p&gt;The time complexity of Bubble Sort is O(n²) in the average and worst cases. This is due to the nested loops that compare and potentially swap adjacent elements.&lt;/p&gt;

&lt;p&gt;However, Bubble Sort has a best-case time complexity of O(n) when the input array is already sorted. In this scenario, the algorithm will make one pass through the array without any swaps, and the swapped flag will remain false, causing the algorithm to terminate.&lt;/p&gt;

&lt;h2&gt;
  
  
  When Might You Use Bubble Sort?
&lt;/h2&gt;

&lt;p&gt;Like Selection Sort, Bubble Sort is generally not the most efficient algorithm for large datasets due to its quadratic time complexity. However, it can be useful in the following situations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Small Datasets&lt;/strong&gt;: For very small arrays, the simplicity of Bubble Sort might outweigh the slight performance overhead compared to more complex algorithms.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Educational Purposes&lt;/strong&gt;: It's a very intuitive and easy-to-understand algorithm, making it a great starting point for learning about sorting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nearly Sorted Data&lt;/strong&gt;: If you know that your data is almost sorted, Bubble Sort can perform quite well (approaching O(n) with the optimization).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this post, we've expanded our sorting knowledge by exploring two more fundamental algorithms: Selection Sort and Bubble Sort. We've seen how they work, looked at their Go implementations, and discussed their time complexities and potential use cases.&lt;/p&gt;

&lt;p&gt;Stay tuned for the next part of our "Go for Sort" series, where we'll delve into more efficient sorting algorithms!&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>go</category>
      <category>selectionsort</category>
      <category>bubblesort</category>
    </item>
    <item>
      <title>Go for Sort: Part 1 - Exploring the Fundamentals and Implementing Insertion Sort</title>
      <dc:creator>Alibek</dc:creator>
      <pubDate>Mon, 28 Apr 2025 07:22:57 +0000</pubDate>
      <link>https://dev.to/alikawp/go-for-sort-part-1-exploring-the-fundamentals-and-implementing-insertion-sort-2go5</link>
      <guid>https://dev.to/alikawp/go-for-sort-part-1-exploring-the-fundamentals-and-implementing-insertion-sort-2go5</guid>
      <description>&lt;p&gt;Hello! Welcome to this post where we'll explore the fascinating world of sorting algorithms. As a follow-up to my &lt;a href="https://dev.to/alikawp/linear-search-vs-binary-search-a-practical-guide-in-go-12ch"&gt;previous post&lt;/a&gt; on search algorithms, particularly binary search, it's crucial to understand that binary search requires sorted input. In this article, we'll define what it means for data to be sorted and explore various methods to achieve this ordering.&lt;/p&gt;

&lt;p&gt;As usual, all the code examples are provided in the Go programming language. However, I am confident that even if you're not familiar with Go but have programming experience, you'll be able to understand the examples with minimal effort.&lt;/p&gt;

&lt;p&gt;Alright, here we go. What does it mean for something to be "sorted"? Well, first thing that comes to my mind is that something has some kind of an order, or there is the sequence of the things that follows predefined rules. Consider a box of apples of various colors. If we decide to group them into separate boxes based on their color, what is that process called? That's right – sorting! With numbers, the concept of order is intuitive: 1 is less than 2, 2 is less than 3, and this pattern continues. How can we leverage this natural ordering? Well, we can create a sequence where numbers follow this rule: starting with the smallest, ending with the largest, and ensuring that each element is greater than or equal to the preceding one and less than or equal to the following one. Because sorting enables the efficient arrangement of massive datasets, making information retrieval a breeze, it stands as a cornerstone of computer science, underpinning countless applications from database management to search engines.&lt;/p&gt;

&lt;p&gt;In this series of posts, we are going to look at a few different algorithms, and to start, let's dive into something called Insertion Sort. This sorting algorithm is quite intuitive because it reminds us of how we might sort a hand of playing cards.&lt;/p&gt;

&lt;p&gt;Imagine you're holding a hand of cards dealt one by one. As you receive each new card, you look at the cards you're already holding and insert the new card into its correct position within your hand, ensuring that the cards in your hand are always sorted. You might shift some of the existing cards to the right to make space for the new one.&lt;/p&gt;

&lt;p&gt;Insertion Sort works in a similar way with arrays or lists. It iterates through the elements, and for each element, it compares it with the elements before it. If the current element is smaller than the preceding elements, it "inserts" it into the correct sorted position by shifting the larger elements to the right.&lt;/p&gt;

&lt;p&gt;Let's illustrate this with an example. Suppose we have the following unsorted array of numbers: &lt;code&gt;[5, 2, 4, 6, 1, 3]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's how Insertion Sort would process it step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Start with the second element (2).&lt;/strong&gt; Compare it with the element before it (5). Since 2 is smaller than 5, we move 5 to the right and insert 2 in its place: &lt;code&gt;[2, 5, 4, 6, 1, 3]&lt;/code&gt;. The first two elements are now sorted.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Move to the third element (4).&lt;/strong&gt; Compare it with the elements before it (5 and 2). 4 is smaller than 5, so we shift 5 to the right. Now compare 4 with 2. Since 4 is greater than 2, we insert 4 in its current position: &lt;code&gt;[2, 4, 5, 6, 1, 3]&lt;/code&gt;. The first three elements are now sorted.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Consider the fourth element (6).&lt;/strong&gt; Compare it with the elements before it (5, 4, and 2). 6 is greater than all of them, so it remains in its current position: &lt;code&gt;[2, 4, 5, 6, 1, 3]&lt;/code&gt;. The first four elements are sorted.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Move to the fifth element (1).&lt;/strong&gt; Compare it with 6, 5, 4, and 2. 1 is smaller than all of them. We shift 6, 5, 4, and 2 to the right and insert 1 at the beginning: &lt;code&gt;[1, 2, 4, 5, 6, 3]&lt;/code&gt;. The first five elements are sorted.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Finally, consider the last element (3).&lt;/strong&gt; Compare it with 6, 5, 4, 2, and 1. 3 is smaller than 6, 5, and 4, so we shift them to the right. 3 is greater than 2, so we insert 3 in its correct position: &lt;code&gt;[1, 2, 3, 4, 5, 6]&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now the entire array is sorted!&lt;/p&gt;

&lt;p&gt;This step-by-step process of taking an element and inserting it into its correct sorted position within the already sorted portion of the array is the essence of Insertion Sort. Now to the coding example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;

&lt;span class="c"&gt;// InsertionSort sorts an integer slice&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;InsertionSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; 
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;unsortedArray&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Unsorted array:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;unsortedArray&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;InsertionSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unsortedArray&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sorted array:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;unsortedArray&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you run this Go code, you will see the following output:&lt;br&gt;
&lt;code&gt;Unsorted array: [5 2 4 6 1 3]&lt;/code&gt;&lt;br&gt;
&lt;code&gt;Sorted array: [1 2 3 4 5 6]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This clearly shows how the &lt;code&gt;InsertionSort&lt;/code&gt; function takes an unsorted array and modifies it in-place to produce a sorted array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Time Complexity of Insertion Sort:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To understand how efficient Insertion Sort is, we need to consider its time complexity in different scenarios:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Worst-case:&lt;/strong&gt; The worst-case scenario occurs when the input array is sorted in reverse order. In this case, for each element, we have to compare it with all the preceding elements and shift them to the right. This results in approximately &lt;strong&gt;n(n-1)/2&lt;/strong&gt; comparisons and swaps, where &lt;strong&gt;n&lt;/strong&gt; is the number of elements in the array. Therefore, the worst-case time complexity of Insertion Sort is &lt;strong&gt;O(n²)&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Best-case:&lt;/strong&gt; The best-case scenario happens when the input array is already sorted. In this situation, for each element, we only need to perform one comparison with the element before it, and no swaps are needed. Thus, the best-case time complexity of Insertion Sort is &lt;strong&gt;O(n)&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Average-case:&lt;/strong&gt; In the average case, where the elements in the input array are in a random order, we can expect to perform approximately half the number of comparisons and shifts as in the worst case. Therefore, the average-case time complexity of Insertion Sort is also &lt;strong&gt;O(n²)&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Insertion Sort is efficient for small datasets or for datasets that are nearly sorted. Its simplicity and low overhead make it a good choice in such cases. However, for larger, unsorted datasets, algorithms with better average and worst-case time complexities, like Merge Sort or Quick Sort, are generally preferred.&lt;/p&gt;

&lt;p&gt;That wraps up our exploration of Insertion Sort. In the next post, we'll delve into the characteristics and implementation of another fundamental sorting algorithm. Stay tuned!&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>go</category>
      <category>sorting</category>
      <category>insertionsort</category>
    </item>
    <item>
      <title>Linear Search vs. Binary Search: A Practical Guide in Go</title>
      <dc:creator>Alibek</dc:creator>
      <pubDate>Mon, 21 Apr 2025 08:42:18 +0000</pubDate>
      <link>https://dev.to/alikawp/linear-search-vs-binary-search-a-practical-guide-in-go-12ch</link>
      <guid>https://dev.to/alikawp/linear-search-vs-binary-search-a-practical-guide-in-go-12ch</guid>
      <description>&lt;p&gt;Hello, in this post I am going to talk about different types of search algorithms. In essence, I am going to focus on linear search and binary search, illustrating how these algorithms work and comparing them in terms of time complexity, also known as Big O Notation.&lt;/p&gt;

&lt;p&gt;All the code examples are provided in the Go programming language. However, I am confident that even if you're not familiar with Go but have programming experience, you'll be able to understand the examples with minimal effort.&lt;/p&gt;

&lt;p&gt;Alright, let's dive in. Firstly, we'll start with linear search, which is one of the simplest (perhaps even the most straightforward) algorithms available. I would even call this algorithm intuitive because, in most cases when faced with the task of finding a specific element within a collection, our initial thought is often to examine each element sequentially. This algorithm works exactly like that: we begin at the start of the list or array and compare each element with the target element one by one, step by step, until we find it. If the target element is not present in the list, we can simply return -1 or nil to indicate its absence.&lt;/p&gt;

&lt;p&gt;Here is an example implementation in Go:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;linearSearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="c"&gt;// Return the index if the target is found&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="c"&gt;// Return -1 if the target is not found&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;6&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;
    &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;linearSearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Element %d found at index %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Element %d not found in the array&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;
    &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;linearSearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Element %d found at index %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Element %d not found in the array&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this Go code, the &lt;code&gt;linearSearch&lt;/code&gt; function takes an integer slice &lt;code&gt;arr&lt;/code&gt; and an integer &lt;code&gt;target&lt;/code&gt; as input. It iterates through each element and its corresponding index in the slice. If the element matches the &lt;code&gt;target&lt;/code&gt;, the function immediately returns the index. If the loop completes without finding the &lt;code&gt;target&lt;/code&gt;, the function returns &lt;code&gt;-1&lt;/code&gt;. The &lt;code&gt;main&lt;/code&gt; function demonstrates how to use the &lt;code&gt;linearSearch&lt;/code&gt; function with a sample array and target values.&lt;/p&gt;

&lt;p&gt;Why is it called linear search, though? Remember Big O notation? Well, it turns out that Big O describes the so-called time complexity of this algorithm, which represents the time required for this algorithm to perform the operation, in our case, finding the element in the collection. Does it tell us the exact time needed to complete the search? No, of course not. Even though this term is called time complexity, it doesn't actually describe the precise time required to do the work. Instead, it tells us the number of operations this algorithm would require to complete the given task.&lt;/p&gt;

&lt;p&gt;This algorithm can have different scenarios or cases. Let's say the best-case scenario is when the target element is the first in the collection (or the element with the index of 0). In that case, we don't need to go through the entire collection to find the target; it will be found in the first iteration, meaning we would require just one operation.&lt;/p&gt;

&lt;p&gt;Now, let's consider the worst case. This basically means that the target element is at the end of the collection. And yes, as you might have guessed, we will need to compare every element in the collection with our target to find out that the target is the last element (or the element with the index of &lt;code&gt;len(arr)-1&lt;/code&gt;). In that case, we can say that we require N operations to complete the task, where N is the size of our array. If there are 10 elements in the array, that's not a big deal; our code iterates 10 times to find the target. But what happens if the size of the array is 1 million elements or even worse, 1 billion elements, and the target is still the last element? Well, the code will iterate either 1 million or 1 billion times, thus resulting in linear time complexity—the number of operations required to complete the task grows linearly with the size of the input. With that, we can say that the time complexity of linear search in the worst case is proportional to the size of the input, which is N, or simply O(n). That's how Big O notation looks :) One more thing: Big O often describes the worst-case scenario.&lt;/p&gt;

&lt;p&gt;If linear search has a time complexity of O(n) and can indeed be slow with very large inputs, is there a more efficient alternative? Yes, there is, and it's called binary search. Binary search is significantly faster compared to linear search, boasting a time complexity of O(log n). This means that the number of operations required to complete the task grows logarithmically with the input size.&lt;/p&gt;

&lt;p&gt;What does O(log n) actually mean? Let's recall some basic math. What is 2 raised to the power of 2? The answer is 4. What is 2 raised to the power of 3? It's 8. Now, what is a logarithm? Simply put, it's the inverse operation of exponentiation. For example, the logarithm base 2 of 8 (written as log₂(8)) is 3, because 2 raised to the power of 3 equals 8.&lt;/p&gt;

&lt;p&gt;So, why is this relevant to search algorithms? Imagine we have a sorted collection of integers with a size of 128, and we need to find the element 78. With linear search, we would potentially have to examine every element until we find the target, which isn't very efficient. Binary search offers a much better approach.&lt;/p&gt;

&lt;p&gt;To use binary search, the collection &lt;strong&gt;must be sorted&lt;/strong&gt; in ascending order (from the smallest to the largest). So, our input would look something like this: &lt;code&gt;[0, 1, 2, 3, 4, 5, ..., 127]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Looking at this sorted array, we can think about dividing it into two roughly equal halves. The first half would be from index 0 to 63 (since the middle index of 128 elements is around 63.5), and the second half from index 64 to 127. Where do we think our target, 78, might be? It's clearly in the second half. So, do we still need to consider the first half? Not really; we can focus our search on the second half (indices 64-127). With just one comparison (with the middle element), we've effectively halved the search space from N (128) to N/2 (64).&lt;/p&gt;

&lt;p&gt;What do we do next? We repeat the same process, but now within our new search boundaries (indices 64 to 127). We find the middle element of this sub-array (around index 95). We compare our target (78) with this middle element. Since 78 is smaller than 95, we know our target must lie in the first half of this sub-array (indices 64 to 94). Again, we've halved our search space.&lt;/p&gt;

&lt;p&gt;We continue this process of dividing the search interval in half. In each step, we compare the target value with the middle element of the current interval. If the target is equal to the middle element, we've found it. If the target is smaller than the middle element, we narrow our search to the left half. If the target is larger, we focus on the right half. We keep doing this until the target is found or the search interval becomes empty (meaning the target is not present in the array).&lt;/p&gt;

&lt;p&gt;Because we are halving the search space in each step, the number of steps required to find the target (or determine its absence) grows logarithmically with the size of the input. For an array of size N, in the worst case, binary search will take approximately log₂(N) steps. For our example of 128 elements, log₂(128) is 7, which is significantly fewer operations than the potential 128 operations in linear search!&lt;/p&gt;

&lt;p&gt;Here's an example implementation of binary search in Go:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
    &lt;span class="s"&gt;"sort"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;binarySearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
    &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="c"&gt;// finding the middle index&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="c"&gt;// Target found at the middle index&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="c"&gt;// Target is in the right half&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="c"&gt;// Target is in the left half&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="c"&gt;// Target not found&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="m"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;6&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Ints&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Important: Binary search requires a sorted array&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sorted array:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;7&lt;/span&gt;
    &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;binarySearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Element %d found at index %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Element %d not found in the array&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;11&lt;/span&gt;
    &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;binarySearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Element %d found at index %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Element %d not found in the array&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this Go code, the &lt;code&gt;binarySearch&lt;/code&gt; function takes a sorted integer slice &lt;code&gt;arr&lt;/code&gt; and an integer target as input. It initializes &lt;code&gt;left&lt;/code&gt; and &lt;code&gt;right&lt;/code&gt; pointers to the beginning and end of the slice, respectively. The for loop continues as long as the &lt;code&gt;left&lt;/code&gt; pointer is less than or equal to the &lt;code&gt;right&lt;/code&gt; pointer. In each iteration, it calculates the middle index &lt;code&gt;mid&lt;/code&gt;. If the element at &lt;code&gt;arr[mid]&lt;/code&gt; is equal to the &lt;code&gt;target&lt;/code&gt;, the function returns &lt;code&gt;mid&lt;/code&gt;. If &lt;code&gt;arr[mid]&lt;/code&gt; is less than the &lt;code&gt;target&lt;/code&gt;, it means the &lt;code&gt;target&lt;/code&gt; must be in the right half of the slice, so we update &lt;code&gt;left&lt;/code&gt; to &lt;code&gt;mid + 1&lt;/code&gt;. Otherwise, if &lt;code&gt;arr[mid]&lt;/code&gt; is greater than the &lt;code&gt;target&lt;/code&gt;, the &lt;code&gt;target&lt;/code&gt; must be in the left half, and we update &lt;code&gt;right&lt;/code&gt; to &lt;code&gt;mid - 1&lt;/code&gt;. If the loop finishes without finding the &lt;code&gt;target&lt;/code&gt;, the function returns &lt;code&gt;-1&lt;/code&gt;. The &lt;code&gt;main&lt;/code&gt; function demonstrates the usage of &lt;code&gt;binarySearch&lt;/code&gt; with a sample (unsorted) array, which is then sorted using &lt;code&gt;sort.Ints&lt;/code&gt; before the search.&lt;/p&gt;

&lt;p&gt;In conclusion, we've explored two fundamental search algorithms: linear search and binary search. While linear search offers a straightforward and intuitive approach by examining each element sequentially, its O(n) time complexity makes it less efficient for large datasets. Binary search, on the other hand, leverages the power of a sorted input to achieve a significantly faster O(log n) time complexity by repeatedly halving the search space. Understanding the strengths and weaknesses of each algorithm, particularly their time complexities, is crucial for choosing the most appropriate search method for a given task and dataset size. As we've seen, even a seemingly simple change in approach can lead to substantial performance improvements, especially when dealing with vast amounts of data.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>go</category>
      <category>linearsearch</category>
      <category>binarysearch</category>
    </item>
  </channel>
</rss>
