<?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: CONRAD</title>
    <description>The latest articles on DEV Community by CONRAD (@conrad96).</description>
    <link>https://dev.to/conrad96</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%2F309828%2F14b06a20-4e8a-4430-8e12-05157ba8ec2e.jpeg</url>
      <title>DEV Community: CONRAD</title>
      <link>https://dev.to/conrad96</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/conrad96"/>
    <language>en</language>
    <item>
      <title>Insertion Sort</title>
      <dc:creator>CONRAD</dc:creator>
      <pubDate>Fri, 31 Mar 2023 08:55:19 +0000</pubDate>
      <link>https://dev.to/conrad96/insertion-sort-28kb</link>
      <guid>https://dev.to/conrad96/insertion-sort-28kb</guid>
      <description>&lt;p&gt;Insertion sort builds the final sorted array one item at a time by comparisons. works best on small datasets and is much less efficient than quicksort, mergesort.&lt;/p&gt;

&lt;p&gt;My understanding on this is that insertion sort removes one item from the input array/list, finds the location it belongs within the sorted list, and inserts it there. it repeats until no input elements remain.&lt;/p&gt;

&lt;p&gt;below is the solution, using a recursive approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const insertionSort = (arr, n) =&amp;gt; {
    if(n &amp;gt; 0) {     //base case
        insertionSort(arr, (n - 1))

        let x = arr[n];
        let j = (n - 1);

        while (j &amp;gt;=0 &amp;amp;&amp;amp; arr[j] &amp;gt; x) {
            //swap 
            arr[j+1] = arr[j];
            j = j - 1;
        }
        arr[j+1] = x;

        console.log(arr, '\n');
    }
}
const inputArr = [5,10,1,25];
insertionSort(inputArr, (inputArr.length - 1));  // [1, 5, 10, 25]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Worst/Average case: O(n^2)&lt;br&gt;
However insertion sort is the fastest in sorting small datasets, even faster than quicksort. but terrible with large datasets.&lt;/p&gt;

</description>
      <category>insertionsort</category>
      <category>algorithms</category>
      <category>datastructure</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Bubble sort algorithm</title>
      <dc:creator>CONRAD</dc:creator>
      <pubDate>Thu, 30 Mar 2023 09:18:25 +0000</pubDate>
      <link>https://dev.to/conrad96/bubble-sort-algorithm-3kc6</link>
      <guid>https://dev.to/conrad96/bubble-sort-algorithm-3kc6</guid>
      <description>&lt;p&gt;The Bubble sort algorithm is a simple sorting algorithm that operates by comparing pairs of values in an array. It works by iterating through the array and comparing adjacent values, swapping them if the value on the left is greater than the value on the right. This process is repeated until there are no more values that are greater than the highest value in the array, resulting in a sorted list/array in ascending order.&lt;/p&gt;

&lt;p&gt;While Bubble sort is a straightforward algorithm, it can be slow and inefficient for large arrays. Despite its limitations, it is still a useful algorithm to understand as it forms the foundation for more complex sorting algorithms.&lt;/p&gt;

&lt;p&gt;In summary, the Bubble sort algorithm compares pairs of values and swaps the greatest with the least until the array is sorted in ascending order.&lt;/p&gt;

&lt;p&gt;so the steps are simple:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;nested loop to run through input array, using 2 variables (i and j). such that 0≤i≤(n-1) and 0≤j≤(n-i-1)&lt;/li&gt;
&lt;li&gt;if arr[j] is greater than arr[j+1] then swap the adjacent value else so on.&lt;/li&gt;
&lt;li&gt;return sorted array.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Below is the solution in javascript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const swap = (arr, pos1, pos2) =&amp;gt;  {
    //swap pos2 val with pos1 val
    let temp = arr[pos1];
    arr[pos1] = arr[pos2];
    arr[pos2] = temp;
}

const bubbleSort = (arr = []) =&amp;gt; {
    let len = arr.length;

    for(let i=0; i&amp;lt;len; i++) {
        for(let j=0; j&amp;lt;(len - i - 1); j++ ) {  

            //swap arrayValue with its adjacent if its greater than it.
            if(arr[j] &amp;gt; arr[j+1]) {
                swap(arr, j, j+1); 
            }
        }
    }
    return arr;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Best case scenario is when the input array is sorted in ascending order, worst case scenario is when the input array is sorted in descending order. &lt;br&gt;
Time complexity for Best case O(n);&lt;br&gt;
Time complexity for Worst case O(n^2);&lt;/p&gt;

&lt;p&gt;Worst case scenario analysis:&lt;br&gt;
If the input array is in descending order, that means that many iterations/passes will be required to swap elements&lt;br&gt;
total number of iterations on an input array (n-1)&lt;/p&gt;

&lt;p&gt;At iteration 1:  Number of comparisons = (n-1)&lt;br&gt;
                     Number of swaps = (n-1)&lt;/p&gt;

&lt;p&gt;At iteration 2:  Number of comparisons = (n-2)&lt;br&gt;
                     Number of swaps = (n-2)&lt;/p&gt;

&lt;p&gt;At iteration 3:  Number of comparisons = (n-3)&lt;br&gt;
                    Number of swaps = (n-3)   .... and so on&lt;br&gt;
= (n-1) + (n-2) +  (n-3) + . . . 2 + 1&lt;br&gt;
= (n-1)*(n-1+1)/2  { using sum of n natural Number formula }&lt;br&gt;
= n(n-1)/2&lt;br&gt;&lt;br&gt;
= n^2&lt;br&gt;
therefore Total number of swaps = Total number of comparisons&lt;br&gt;
Worst case scenario: O(n^2)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input:
[6,4,1]
Output:
[1, 4, 6]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bubblesort is relatively easy to implement but due to its time complexity of O(n^2) it can be very slow for large datasets.&lt;/p&gt;

</description>
      <category>sort</category>
      <category>programming</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Learning Datastructure &amp; algorithms all over again.</title>
      <dc:creator>CONRAD</dc:creator>
      <pubDate>Thu, 30 Mar 2023 08:16:51 +0000</pubDate>
      <link>https://dev.to/conrad96/learning-datastructure-algorithms-all-over-again-429l</link>
      <guid>https://dev.to/conrad96/learning-datastructure-algorithms-all-over-again-429l</guid>
      <description>&lt;p&gt;I completed my undergraduate studies in computer science with honors six years ago. However, due to my professional work in various projects for over five years, I did not have frequent exposure to theories on data structures and algorithms. Consequently, I did not deem it necessary to refresh my knowledge or skills in this area. Regrettably, I now realize that this decision has had a negative impact on my job search for software development positions, as many tech companies require such knowledge.&lt;/p&gt;

&lt;p&gt;To address this gap in my knowledge, I have decided to embark on a journey to re-learn and practice various algorithms. Blogging is an excellent platform for me to document my learning process and share my experiences with others.&lt;/p&gt;

&lt;p&gt;In particular, I intend to focus on a variety of algorithms such as sorting algorithms, search algorithms, and graph algorithms. My goal is to provide detailed explanations and code snippets for each algorithm that I research. I plan to post daily updates on my progress towards achieving this objective.&lt;/p&gt;

&lt;p&gt;Here are some of the algorithms that I will be exploring in detail:&lt;/p&gt;

&lt;p&gt;Sorting Algorithms:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;- Bubble sort&lt;/li&gt;
&lt;li&gt;- Insertion sort&lt;/li&gt;
&lt;li&gt;- Merge sort&lt;/li&gt;
&lt;li&gt;- Quick sort&lt;/li&gt;
&lt;li&gt;- Heap sort&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Search Algorithms:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Linear Search&lt;/li&gt;
&lt;li&gt;Binary Search&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Graph Algorithms:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Depth-First-Search(DFS) &amp;amp; Breadth-First-Search algorithms&lt;/li&gt;
&lt;li&gt;Dijkstra's Algorithm&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I hope that this blogging endeavor will not only help me to refresh my knowledge of algorithms but also aid others who may be in a similar situation.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>computerscience</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
