<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Olawale Omosekeji on Medium]]></title>
        <description><![CDATA[Stories by Olawale Omosekeji on Medium]]></description>
        <link>https://medium.com/@narhzih?source=rss-e6e571c816f6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*VVXBXRO1_kOMFOEmQiX86w.jpeg</url>
            <title>Stories by Olawale Omosekeji on Medium</title>
            <link>https://medium.com/@narhzih?source=rss-e6e571c816f6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 15:31:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@narhzih/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Searching and Sorting Algorithms in Javascript — Part 9(Binary Search).]]></title>
            <link>https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-9-binary-search-53b977b02959?source=rss-e6e571c816f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/53b977b02959</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[binary-search-algorithm]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Olawale Omosekeji]]></dc:creator>
            <pubDate>Mon, 26 Sep 2022 03:33:43 GMT</pubDate>
            <atom:updated>2022-09-26T03:33:57.999Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9yOcKPpZUxuupXrs" /><figcaption>Photo by <a href="https://unsplash.com/@evgenit?utm_source=medium&amp;utm_medium=referral">Evgeni Tcherkasski</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Searching and Sorting Algorithms in Javascript — Part 9 (Binary Search).</h3><p>Binary search is another search algorithm that can be very handy in some cases. It can be so much faster than linear search because, it doesn’t necessary search through all the items in your list of items. What binary search does instead is, it uses a technique called <strong><em>divide and conquer. </em></strong>I’ll walk you through the algorithm, then show you a visual representation and then finally, show you the code.</p><h3>Algorithm Walkthrough</h3><p>Basically, binary search takes in a <strong>SORTED</strong> list of items (an array) and does the following:</p><ol><li>Takes your list of items, finds the middle element in that list of items and asks: “is this middle element equal to the element I’m looking for?”. If it is, it just returns the index of that element. If not, it can either be <strong>greater than</strong> or <strong>less than</strong> the element I’m looking for so, it moves to step 2.</li><li>If it’s greater than, that means the element I’m looking for is somewhere to the left of the middle element.</li><li>If it’s less than, that means the element I’m looking for is somewhere to the right.</li></ol><p>Take for instance this <strong>sorted</strong> array -&gt; [1, 2, 3, 4, 5, 6, 7]. If you’re to use binary search algorithm to look for <strong>5 </strong>in this array, binary search algorithm starts by picking the middle item, in this case 4, it then does the following:</p><ol><li>Is the middle element (in this case 4) equal to the element I’m looking for (which is 5). The answer is no, so, it proceeds.</li><li>Is it greater than the element I’m looking for? No (because, 4 is less than 5) so, it proceeds.</li><li>Is it less than the element I’m looking for? The answer is Yes (because 4 is less than 5) so, this means that the element I’m looking for is somewhere to the right of this middle element. It then discards all the element to the left of 4 (including 4) and and repeat the whole process on elements to the right of 4 (in this case). <br>If it doesn’t find the number after exhausting the items in the array, it just returns -1.</li></ol><h3>Visual Representation</h3><p>First, we start by taking our full list of element, find the midpoint which is 4 in this case, check <strong>4</strong> against our desired element (<strong>5</strong>). We return it if it’s what we’re looking for. <br>In this case, 5 (our desired element) is greater than 4 (our middle element) so, we only consider all elements to the right of 4 (since the element we’re looking for is greater than 4:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E9oOMQ0VUr7ZB8IuGdLSXw.png" /></figure><p>Next, our new middle element is now 6, we compare and we realize it’s not what we’re looking for. we then discard every element to the right of 6 (since the element we’re looking for is less than 6).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NsgKMgGg7d4Pp2MQWuLmHw.png" /></figure><p>Finally, our middle element is now 5 and that’s exactly what we’re looking for so, we return it.</p><h3>The Code</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e16bbd9fa78561e5ef7df3960384b091/href">https://medium.com/media/e16bbd9fa78561e5ef7df3960384b091/href</a></iframe><p>I hope the code is clear enough to show you how binarySearch is implemented.</p><p>Binary Search has a worst case time complexity of <strong>O(log N)</strong> and a best case time complexity of <strong>O(1)</strong> (which is way better than that of linear search). <br>This brings us to the end of this series. <br>Thank you for keeping up.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=53b977b02959" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Searching and Sorting Algorithms in Javascript — Part 8 (Linear Search).]]></title>
            <link>https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-8-linear-search-4b4ea03ecf2c?source=rss-e6e571c816f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b4ea03ecf2c</guid>
            <category><![CDATA[linear-search]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Olawale Omosekeji]]></dc:creator>
            <pubDate>Sun, 25 Sep 2022 18:21:04 GMT</pubDate>
            <atom:updated>2022-09-25T18:21:04.320Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*urgzcuEEttP8PbWe" /><figcaption>Photo by <a href="https://unsplash.com/@nlswss?utm_source=medium&amp;utm_medium=referral">Niels Weiss</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Searching and Sorting Algorithms in Javascript — Part 8 (Linear Search).</h3><p>I should have started this series with searching (as the title implies) but somehow, It felt right to do the sorting algorithms first. Over the span of 6–7 parts, we’ve been able to address the following sorting algorithms:</p><ol><li><a href="https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-2-bubble-sort-2a969bfe5aa8">Bubble Sort</a></li><li><a href="https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-3-selection-sort-fd96ae92c12">Selection Sort</a></li><li><a href="https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-3-insertion-sort-372bb8544805">Insertion Sort</a></li><li><a href="https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-5-merge-sort-7d0c313a9aeb">Merge Sort</a></li><li><a href="https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-6-quick-sort-9609074997cb">Quick Sort</a></li><li><a href="https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-7-radix-sort-9f708b3fba3d">Radix Sort</a></li></ol><p>In this part (or chapter), we’ll be looking at a searching algorithm called the Linear search algorithm. This algorithm is pretty straightforward and the implementation is also very straightforward.</p><p>Searching (in general) usually involve trying to locate an item in the midst of other items, e.g: Looking for a book in a physical library, Searching for your future parter admist over 7 billion people on earth, an so on…</p><p>If you’re to use linear search algorithm to locate an item, you can easily do that by looping through all the possible items and compare it to the item you’re looking for, if there’s a match at any point in time, return the item, if not, just return -1 (or something that indicates that what you’re looking for is not in the list provided).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/49e3388dede484bc19701364e8a44e8a/href">https://medium.com/media/49e3388dede484bc19701364e8a44e8a/href</a></iframe><p>The above implementation was structured to return an Object containing a message, the item that was found and the index of the first occurrence of the item. The commented returns in the code signifies what is usually returned from the algorithm in most cases.</p><p>You can quickly see that, as much as this is pretty straightfoward, it can be very slow if you’re searching through more complex data structures like an object in an array of objects or array of an array of arrays (a multidimensional array.</p><p>Anyways, that’s all there is to know about linear search, see you on the next one!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b4ea03ecf2c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Searching and Sorting Algorithms in Javascript — Part 7 (Radix Sort).]]></title>
            <link>https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-7-radix-sort-9f708b3fba3d?source=rss-e6e571c816f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/9f708b3fba3d</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[radix-sort]]></category>
            <category><![CDATA[sorting-algorithms]]></category>
            <dc:creator><![CDATA[Olawale Omosekeji]]></dc:creator>
            <pubDate>Fri, 23 Sep 2022 12:40:52 GMT</pubDate>
            <atom:updated>2022-09-26T03:42:46.280Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FUljXzeT-CwrDmBg" /><figcaption>Photo by <a href="https://unsplash.com/@kellysikkema?utm_source=medium&amp;utm_medium=referral">Kelly Sikkema</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Searching and Sorting Algorithms in Javascript — Part 7 (Radix Sort).</h3><p>If you’ve been been following this series from <a href="https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-1-intro-87eb26b4503a?source=user_profile---------8----------------------------">part one</a>, you must have realized that, in the end, we’re just looking for better ways to do the same thing. The method you choose to apply basically depends on the problem at hand.</p><p>Of the several ways there is to arrange (or sort) the items of a list (or an array),<strong> Radix Sort </strong>is one of the very unique ones. It takes a very different approach when performing this task of arranging items.</p><h3>INTRODUCTION TO RADIX SORT</h3><p>What is <strong>Radix sort</strong> exactly and how is it different from all the other algorithms we’ve mentioned so far?</p><p>If you carefully look at all the other algorithms we’ve discussed so far (from bubble sort to quick sort), you’ll quickly realize that at some point, we’re comparing 2 numbers and deciding which one is greater or smaller. These algorithms are all grouped under something called <strong>“the Comparison sorting algorithms”</strong>.</p><p>When dealing with Radix Sort Algorithm, we’re not actually comparing 2 numbers at any point in time to decide which is greater or lesser. It takes a very different approach, it uses the fact that the information about a given number is engraved in the number of digits the number has. Take a number for instance (say <strong>1022</strong>). <strong>1022</strong> is a <strong>4</strong> digit number and we can be very sure that <strong>1022</strong> (a <strong>4</strong> digit number) is always greater than any 3 digit number.</p><p>This basic information about the number of digits a particular number has can be very powerful (you’ll see when we start implementing the algorithm) and it forms the basis of the building blocks of the <strong>Radix Sort Algorithm. </strong>This might be a long read so, stick with me to the end and you’ll see the light at the end of the tunnel!</p><h3>A Microscopic View Into Radix Sort Algorithm</h3><p>Now, we’re getting into the technicalities of how Radix sort algorithm works.</p><p>The way Radix sort perform its job is to group its members. Like I said earlier, it doesn’t compare numbers, so, you might be wondering: “How exactly does it group these numbers?”. Let me walk you through with an illustration.</p><p>Say we want to sort an array of numbers: [4, 9, 28, 68, 86, 8156, 4132,408, 1556,3556,593].</p><p>First we create a list of buckets with a pointer to each bucket (from 0–9) like the diagram below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dLg-kl5gJwiYjOiBY1QC5g.png" /></figure><p>Next, we loop through our list of numbers and arrange them in the buckets using the following rules.</p><ol><li>Starting from the right, we take the first digit of the current number in the loop and we then push that number to the corresponding bucket. Say our current number is <strong>4132</strong>, the first digit (starting from the right) is <strong>2 </strong>so, we push <strong>4132 </strong>to the bucket with the pointer number <strong>2.<br></strong>On first pass, we have the following result.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yVdwymd9yAbOWq4FK101wA.png" /></figure><p>2. After arranging them, we then build a new array from the bucket. We’ll push elements into this new array following the order of our bucket.<br>Now, we have a new array that looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/776/1*hAF7GtQzUb_q6yvt_CxDFw.png" /></figure><p>3. We’ll group the numbers into the buckets again only that, this time around, we’ll be using the second digit (starting from the right). You might notice that some numbers are only one digit numbers, just add zeroes to make up for that so in this case, <strong>9</strong> and <strong>4</strong> will be <strong>09</strong> and <strong>04</strong> respectively which makes their second digit 0 (if you’re starting your count from the right). Always have in mind that we’re always counting from the right.</p><p>After the second pass, we then have the following group of numbers:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hoczrbJqOmnIRDLJrMP_fw.png" /></figure><p>4. We then rebuild another list from our new bucket of items. Now, we have a new array (or list) like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*jfXxhKf7P7awgC1zjd6xRw.png" /></figure><p>(You can see that our list is already getting sorted some how).</p><p>5. You’ll then repeat this process using the third digit of each number until you’ve exhausted all the possible digits in each number. The loop has a determinable number of times it should run. So, in your list of items, if the largest number has 7 digits, then you’ll run the loop 7 times. In simple terms, the loop runs as many times as the amount of digits in the largest number.</p><p>Does that make any sense? If it doesn’t, I assure you it’ll make more sense when you check the code implementation.</p><h3>NOW WHAT?</h3><p>Well, we’ve seen how the algorithm works so, let’s implement this in code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a544e79c5219f6ba8032fc4cc6aa323/href">https://medium.com/media/1a544e79c5219f6ba8032fc4cc6aa323/href</a></iframe><p>The code might look a bit intimidating at first but, If you understand the process initially explained, you shouldn’t really have much problem understanding how the code works.</p><p>Theoretically, Radix sort tend to be faster than the other sorts that uses comparison technique to sort their elements but, there are few arguments around that. Personally, I like radix sort as it takes a more intuitive approach to solving this problem. And it’s a fun one as well.<br>Radix Sort’s time complexity of <strong>O(nd)</strong>, where <strong>n</strong> is the size of the array and <strong>d</strong> is the number of digits in the largest number.</p><p>Well, that’s all I have to say about sorting algorithms. Now, we move to searching algorithms (starting with linear search).</p><p>See you on the next one!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9f708b3fba3d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Searching and Sorting Algorithms in Javascript — Part 6 (Quick Sort)]]></title>
            <link>https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-6-quick-sort-9609074997cb?source=rss-e6e571c816f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/9609074997cb</guid>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[quicksort]]></category>
            <category><![CDATA[sorting-algorithms]]></category>
            <dc:creator><![CDATA[Olawale Omosekeji]]></dc:creator>
            <pubDate>Thu, 22 Sep 2022 20:53:43 GMT</pubDate>
            <atom:updated>2022-09-24T20:30:56.262Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*WhQZUha8pEf_kRQ4" /><figcaption>Photo by <a href="https://unsplash.com/@harleydavidson?utm_source=medium&amp;utm_medium=referral">Harley-Davidson</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Searching and Sorting Algorithms in Javascript — Part 6 (Quick Sort)</h3><p>Quick sort is one of the “more advanced” sorting algorithms out there. It is very efficient and scale well for fairly large arrays. Of all these “more advanced” algorithms, quick sort just somehow doesn’t sit very well with me (using the words of my mentor: “my brain just doesn’t like the way quick sort works”). It’s just something I can’t always work with out of the box. I have to consult several materials every single time I want to use it or work with it. <br>Well, I believe you’re smarter than me so, you’ll understand pretty well. Let’s get into it…</p><h3>I mean, what is this big guy made of ? 🙄</h3><p>I will try to explain in words, show you some illustration then show you how the code works.</p><h4>In words…</h4><p>It follows the basic idea that merge sort use which is that: “<strong>An array that contains a single element is already sorted”</strong>… and that’s the only similarity between Merge sort and Quick sort.</p><p>Quick sort starts by picking an element in your array and puts that element in the right position. It does this by moving everything greater than the current element to the right of that element and everything less than it to the left of that element.</p><p>I’m sure you’re curious, what’s so hard about it then? Well, Quick sort doesn’t end there… Remember I said quick sort works by picking an element? Well, after putting that element in it’s right position, it then does the following:<br>1. Sort the left side of our current element using the same algorithm.</p><p>2. Sort the right side of the current element using the same algorithm.</p><h4>An Illustration…</h4><p>So say we have an array with elements [4, 5, 2, 1, 3]. Quick sort starts by taking 4 (the number at the beginning of the array) and put it in the right position. The way that works is this:</p><ol><li>It takes 4, loops through every element to right of 4 and compare 4 to each element.</li><li>If that number is lesser than 4, it has to end up somewhere at the left of 4. It doesn’t try to sort the left yet, it just makes sure that every number to the left of 4 is lesser than 4 and every number to the right of 4 is greater than 4.</li></ol><p>To elaborate on point 2, let’s go through each pass on the loop.</p><ol><li><strong>First Pass —</strong> the algorithm compares<strong> 4</strong> and <strong>5</strong> and checks: “is <strong>4</strong> greater than <strong>5</strong>?”. The answer is <strong>“NO” </strong>so it doesn’t do anything.</li><li><strong>Second Pass — </strong>the algorithm compares <strong>4 </strong>and <strong>2 </strong>and checks: “Is <strong>4 </strong>greater than <strong>2</strong>?”. The answer is <strong>“YES”</strong> so, it swaps <strong>2 </strong>and <strong>5 (</strong>Yes<strong>, 2 </strong>and<strong> 5. </strong>Not<strong> 2 </strong>and<strong> 4). </strong>We now have [4,2,5,1,3] as our new array. You might still be thinking of the swap we made in this step so , let me take my time to explain properly.<br>The aim of the algorithm is to record all the numbers lesser than <strong>4</strong>, push them close to each other. At the end of the loop, the algorithm then swap <strong>4 </strong>with the last element smaller than<strong> 4</strong>. I still don’t expect it to make sense at this point but, It will. Just hold on tight</li><li><strong>Third Pass </strong>— the algorithm compares <strong>4</strong> and <strong>1</strong> and checks: “Is <strong>4 </strong>greater than <strong>1</strong>?”. The answer is Yes so, it swaps <strong>1</strong> and <strong>5</strong>. So, we now have [4,2,1,5,3] as our new array. (Note that the algorithms has been putting every element less than <strong>4</strong> it has encountered behind the first thing it encountered that was greater than it which is 5 in this case ).</li><li><strong>Fourth Pass </strong>— the algorithm compares <strong>4</strong> and <strong>3</strong> and checks: “Is <strong>4 </strong>greater than <strong>3</strong>?” The answer is <strong>YES</strong> so, it swaps <strong>3 </strong>and <strong>5</strong>. Now we have [4,2,1,3,5] as our new array. The loop has come to an end at this point and <strong>3</strong> was the last element recorded that is smaller than <strong>4</strong>. The algorithm then swaps <strong>4</strong> and <strong>3</strong>. Now we have [3,2,1,4,5] as our new array. <br>You’ll realize that at this point, every element to the left of <strong>4</strong> is lesser than <strong>4</strong> and every element to right of <strong>4</strong> is greater than<strong> 4</strong>.</li></ol><p>It then takes a sub-array that contains all element to the left of <strong>4</strong> and repeats the same process until the sub-array is totally sorted. After that, it takes another sub-array that contains all element to the right of 4 and repeats the same process until the sub-array is totally sorted.</p><h4>In Code…</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18685d4a3305c788f397b26303e32088/href">https://medium.com/media/18685d4a3305c788f397b26303e32088/href</a></iframe><p>I hope the code does explains itself. Please, try to go over this article over and over again until it sits right with you.</p><h3>How efficient is this algorithm?</h3><p>This algorithm has an average time complexity of O<strong>(n log n) </strong>and a worst case time complexity of <strong>O(n²). The worst case occurs when the picked pivot is always an extreme (smallest or largest) element</strong>.</p><p>Overall it’s very efficient (most times more efficient than Merge sort) but I personally usually prefer merge sort (because it’s easier to implement than this guy).</p><p>I hope that helps, see you on the <a href="https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-7-radix-sort-9f708b3fba3d">next one</a>… 👍</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9609074997cb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Searching and Sorting Algorithms in Javascript — Part 5 (Merge Sort).]]></title>
            <link>https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-5-merge-sort-7d0c313a9aeb?source=rss-e6e571c816f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/7d0c313a9aeb</guid>
            <category><![CDATA[sorting-algorithms]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[merge-sort]]></category>
            <dc:creator><![CDATA[Olawale Omosekeji]]></dc:creator>
            <pubDate>Tue, 13 Sep 2022 01:44:56 GMT</pubDate>
            <atom:updated>2022-09-24T20:21:04.624Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LKVO9QHkymYQ2S3a" /><figcaption>Photo by <a href="https://unsplash.com/@lg17?utm_source=medium&amp;utm_medium=referral">Lance Grandahl</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Searching and Sorting Algorithms in Javascript — Part 5 (Merge Sort).</h3><p>Merge sort is one of the more “Complex” sorting algorithms. As far as algorithms go, most times, the efficiency of the algorithm is inversely proportional to the ease of implementation of the algorithm. What I mean by that is that, the more efficient the algorithm gets, the harder it’s implementation (or at least, the concept) gets.</p><h4>So, how does Merge sort work?</h4><p>The main Idea behind this algorithm is that, an array of exactly 1 element is already sorted. So, given an array of multiple elements, we start to split the array into half until we get to arrays of 1 element each, then we start to merge each array into one another. I know that sounds like rubbish but, Let me show you what I mean (Carefully take a look at the following pictorial representation. I’ll explain what it is below the image).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/824/1*NcaVZfAkbehL9N4MlzOBHA.png" /><figcaption>Pictorial representation of the array splitting process</figcaption></figure><p>We started with an array of 8 elements and we started halving until we arrived at arrays of single elements. Note that at each halving stage, there’s always a right and a left, we then proceed to separately half the left and right until we arrive at the last stage.</p><p>Next, we start joining our single arrays except that, while joining, we make sure we sort the items in that array. Below is a pictorial representation of the process:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*lkiou0msCN6HosRGgaWisw.png" /><figcaption>Pictorial representation of the array merging process</figcaption></figure><p>During this merging process, the following occurs:</p><ol><li>We take two arrays (which are each sorted) and we try to merge them together</li><li>The final array produced at any stage has it’s elements sorted</li></ol><p>If none of the above process is clear to you, I totally understand. It can be overwhelming at first, it was overwhelming for me at first as well but, I’ll encourage you to checkout the demonstration of this algorithm by watching this quick youtube video - <a href="https://www.youtube.com/watch?v=4VqmGXwpLqc">https://www.youtube.com/watch?v=4VqmGXwpLqc</a>. When you’ve finally digested how it works, come back and see how to write it in code.</p><p>At this point, I want to believe the algorithm is a bit clearer (if you took the time to checkout the referenced Youtube video referenced above). If not, just continue and read till the end.</p><p>How does the above pictorial representation work in code (you might ask)?</p><p>Well, we have 2 components in our code:</p><ol><li>A merge function: This is basically the function that will serve the purpose of merging 2 already SORTED arrays</li><li>A merge sort function that does the whole actual merging for us.</li></ol><p>The merge sort function itself is a recursive function as you’ll see in the code below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/82cc70f6c4b0d02c779431cb96c7b3dd/href">https://medium.com/media/82cc70f6c4b0d02c779431cb96c7b3dd/href</a></iframe><p>Please, make sure you understand how both function works. Just know that the aim of the first function is to merge 2 already SORTED arrays into a larger SORTED array.</p><p>The aim of the second function however (the mergeSort function) is to:</p><ol><li>Split the array until we get only single element arrays</li><li>Merge them till get our final sorted array.</li></ol><p>Wheew!!! That was quite a lot to take in. You might be dying to ask, why do we have to learn such a complicated algorithm since you already know selection, bubble and insertion sort?</p><p>Well, I’m happy to tell you that the merge sort algorithm scale well for very large arrays. If you compare it to bubble sort, it’s about 20x faster than bubble sort (😱). It has a time complexity of O(N log N) compared to O(n²) for bubble sort.</p><p>Well, that’s it guys… If you don’t understand yet, try to read again and watch the video recommended in this post. <br>See you on the <a href="https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-6-quick-sort-9609074997cb">next one</a> !!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7d0c313a9aeb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Searching and Sorting Algorithms in Javascript — Part 3 (Insertion Sort).]]></title>
            <link>https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-3-insertion-sort-372bb8544805?source=rss-e6e571c816f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/372bb8544805</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[insertion-sort]]></category>
            <dc:creator><![CDATA[Olawale Omosekeji]]></dc:creator>
            <pubDate>Fri, 09 Sep 2022 13:08:01 GMT</pubDate>
            <atom:updated>2022-09-24T20:16:30.382Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ok3cE6IKgrfa5PYB" /><figcaption>Photo by <a href="https://unsplash.com/@andrewtneel?utm_source=medium&amp;utm_medium=referral">Andrew Neel</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Searching and Sorting Algorithms in Javascript — Part 4(Insertion Sort).</h3><p>Insertion sort algorithm is another sorting algorithm that sits in the same rank with Bubble and Selection sort. I’ll tell you all about the differences and similarities later, then you can pick which one you prefer. For now, let’s focus on how it works…</p><h3>How does this guy work?</h3><p>One beautiful thing about this algorithm is how it does it’s sorting. It’s pretty neat in my opinion (p.s: Don’t say this to anyone outside, you might get stoned for it!!!).</p><p>It follows the main Idea that any number in an array with more than one element has numbers to the left of it except the first element of that array. Here’s what that means:</p><p>Say we have an array of 5 numbers — [1, 2, 3, 5, 6, 7]. Any number in the array except <strong>1</strong> has some number to the left of it. What we can make sure is that, starting from the second element in the array, we make sure that every number to the left of it is in the position it’s supposed to be in terms of rank. So:</p><ol><li>We loop through every number in our array (starting from the second number)</li><li>Start another Loop through all the elements to the left of that number and try to place that number where it belongs.</li></ol><p>Let’s see how this works in code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/abc807ad842fbc9f31a5615458cff575/href">https://medium.com/media/abc807ad842fbc9f31a5615458cff575/href</a></iframe><p>I hope the code above is clear enough. If somethings aren’t clear yet, do checkout the visual representation of the algorithm on visualgo — <a href="https://visualgo.net/en/sorting">https://visualgo.net/en/sortin</a>g.</p><p>That’s it for insertion sort guys………. Is it 👀?</p><h4>What’s the difference between the 3 (selection, bubble and insertion)?</h4><p>In terms of implementation, it’s up to you to pick which one makes more sense to you. But using <strong>Big O</strong> to judge, they all have a worst-case time complexity of <strong>O(n²)</strong>, average time complexity of <strong>O(n²)</strong>. For best case, bubble sort and insertion sort have a time-complexity of <strong>O(n) </strong>while selection sort has <strong>O(n²)</strong>. <br>Have it in mind that, in Big O terms, we don’t really care much about the average and best case time complexity. We’re always judging by the worst case so, it’s up to you now to decide which one you’re adding to your toolbox.</p><p>That’s it for insertion sort guys…. See you on the <a href="https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-5-merge-sort-7d0c313a9aeb">next one</a>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=372bb8544805" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Searching and Sorting algorithms in Javascript — Part 3 (Selection Sort)]]></title>
            <link>https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-3-selection-sort-fd96ae92c12?source=rss-e6e571c816f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/fd96ae92c12</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[interview-questions]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[selection-sort]]></category>
            <dc:creator><![CDATA[Olawale Omosekeji]]></dc:creator>
            <pubDate>Wed, 07 Sep 2022 21:56:27 GMT</pubDate>
            <atom:updated>2022-09-07T21:56:27.574Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LFhZTvbQtGJl46QK" /><figcaption>Photo by <a href="https://unsplash.com/@victoriano?utm_source=medium&amp;utm_medium=referral">Victoriano Izquierdo</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Searching and Sorting algorithms in Javascript — Part 3 (Selection Sort)</h3><p>Another very common sorting algorithm you should know about is the <strong>Selection Sort </strong>algorithm. It is also very easy to implement. Without taking much of your time, let’s get into how this works.</p><p>Given an array of numbers — [10, 8, 11, 1, 3, 80, 2, 7, 9], our selection sort function will take this array and return the following — [1, 2, 3, 7, 8, 9, 10, 11, 80]. The aim is to sort the array from lowest to highest so, let’s take a look at the steps involved:</p><ol><li>We loop through the array of numbers from start to finish (we’ll call this L1)</li><li>On each loop, we take the number at the current index i and save it somewhere.</li><li>We’ll start another loop within our initial loop (L1) which will loop over our array of numbers from i+1 to the end of the array. Let’s call this loop L2.</li><li>We’ll then compare our current number in L1 to each number in L2. We’ll be on the lookout for all numbers smaller than our current number in L1, take the smallest of those numbers (smallest of the smallest) and swap it with our current number in the original array. This will place the current number in L1 in the proper position. This means that by the end of each pass in L1, all numbers to the left are sorted.</li><li>Repeat this for all numbers in L1. By the time we get to the end of the loop in L1, all numbers will have been sorted.</li></ol><p>I understand this might sound gibberish right now but, let’s see this in code. Shall we??</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a0c734dbaeb97f656848f738131834b/href">https://medium.com/media/4a0c734dbaeb97f656848f738131834b/href</a></iframe><h3>How is this any better than Bubble Sort?</h3><p>The main difference between bubble sort and selection sort is that the<strong> bubble sort operates by repeatedly swapping the adjacent elements if they are in the wrong order while the selection sort arranges the element of an array by repeatedly finding the minimum element from the unsorted part and placing that at the beginning of the unsorted part.</strong></p><p>Another major difference is that Selection sort tend to be faster and more efficient than bubble sort. Although, they both have a <strong>worst case</strong> time complexity of <strong>O(n^2), </strong>selection sort still appears to be faster in most cases. This is due to the number of swaps performed in selection sort compared to bubble sort.</p><p>Personally, for small arrays ( _insert your definition for “small arrays”_), I’ll choose selection sort over bubble sort.</p><p>I hope that explains selection sort for you. If you still need more clarification, checkout this visual representation of selection sort at Visualgo — <a href="https://visualgo.net/en/sorting">https://visualgo.net/en/sorting</a>.</p><p>See you on the next one!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd96ae92c12" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Searching and Sorting Algorithms in Javascript — Part 2 (Bubble Sort).]]></title>
            <link>https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-2-bubble-sort-2a969bfe5aa8?source=rss-e6e571c816f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/2a969bfe5aa8</guid>
            <category><![CDATA[sorting]]></category>
            <category><![CDATA[sorting-in-javascript]]></category>
            <category><![CDATA[bubble-sort]]></category>
            <dc:creator><![CDATA[Olawale Omosekeji]]></dc:creator>
            <pubDate>Tue, 06 Sep 2022 19:30:33 GMT</pubDate>
            <atom:updated>2022-09-24T20:13:31.145Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2aN6g4QiC4ajcRX7" /><figcaption>Photo by <a href="https://unsplash.com/@jamie452?utm_source=medium&amp;utm_medium=referral">Jamie Street</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Searching and Sorting Algorithms in Javascript — Part 2 (Bubble Sort).</h3><p>Bubble sort is one of the simplest algorithms out there. It is very easy to implement (although, not all that glitters is Gold). The aim of any sorting algorithm is to arrange the items of a given list in a particular order, based on the relationship between the items of that list. <br>The way Bubble sort does this is by looping through the array multiple times and in each passing, push the biggest item towards the end of the array. Enough talks! Let’s see how this works!!</p><h3>What does the algorithm look like?</h3><p>Given an array of numbers, say [10, 8, 11, 1, 3, 80, 2, 7, 9], we want a function that will accept this array of numbers and return it to us “sorted”. So, we’ll except something like [1,2,3,7,8,9,10,11,80] to be returned back to us from the function.</p><p>The algorithm to do that can be seen below — (Forgive me if my algorithm looks a little bit confusing, don’t worry, you’ll understand properly when we start writing the actual code)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56cc2e2306dce31c907b69b387e51e85/href">https://medium.com/media/56cc2e2306dce31c907b69b387e51e85/href</a></iframe><p>Let me try to explain further:</p><p>The way this algorithm works is by looping through your array over and over again (indefinitely, until all the items of your array have been sorted). On each loop, the function will do the following</p><ol><li>Take the element at the current index (i) and compare it to the element directly next to it (i + 1).</li><li>If the element is greater (i.e arr[i] &gt; arr[i +1], it swaps them and moves to the next number. If not, it moves to the next number without swapping.</li></ol><p>3. The condition for this infinite loop to end is if, after a loop, no swap occurred at all. This means that all elements are now in their right position. At this point, we break the infinite loop and return the sorted array.</p><p>The code implementation of this algorithm can be found below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/81f38ef9485e2ecb70ce30ecfc559bed/href">https://medium.com/media/81f38ef9485e2ecb70ce30ecfc559bed/href</a></iframe><p>I hope this explains how bubble sort works. If you still need more clarity on how it actually works, checkout this visual representation of the algorithm on visualgo — <a href="https://visualgo.net/en/sorting">https://visualgo.net/en/sorting</a></p><h3>Is that all?</h3><p>Yes. That’s all. That’s all there’s is to know about the implementation of the bubble sort algorithm. However, in a case where you have a very large list, you don’t want to use a bubble sort algorithm. Why? <br>Remember when we talked about the Big O Notation in part 1 and then mentioned a gentleman called time complexity? <br>As explained in part 1, time complexity measures how fast the algorithm does what it does. In the case of the bubble sort algorithm, the time complexity is O(n**2) (Read as “O of n squared).</p><p>What all these gibberish means is that, bubble sort algorithm takes a very very long time to sort very large arrays (It tends to be slow when the length of the array increases). For large arrays, you might want to use more refined algorithms like Merge sort and quick sort.</p><p>See you on the <a href="https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-3-selection-sort-fd96ae92c12">next one — Selection Sort</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2a969bfe5aa8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Searching and Sorting Algorithms in Javascript — Part 1 (Intro)]]></title>
            <link>https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-1-intro-87eb26b4503a?source=rss-e6e571c816f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/87eb26b4503a</guid>
            <category><![CDATA[searching-algorithm]]></category>
            <category><![CDATA[sorting-algorithms]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[interview-tips]]></category>
            <category><![CDATA[big-o-notation]]></category>
            <dc:creator><![CDATA[Olawale Omosekeji]]></dc:creator>
            <pubDate>Tue, 06 Sep 2022 16:54:13 GMT</pubDate>
            <atom:updated>2022-09-24T20:11:31.090Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MtsU3bdh_ccjlQK4" /><figcaption>Photo by <a href="https://unsplash.com/@markusspiske?utm_source=medium&amp;utm_medium=referral">Markus Spiske</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Searching and Sorting Algorithms in Javascript — Part 1 (Intro)</h3><p>For the most part, sorting and searching are some of the very frequent things you encounter in your daily tasks as a software engineer (or even in your day-to-day life). In your daily life activities, you have to sort your tasks based on priority, “search” for where you dropped your car keys, where you dropped that phone of yours, and so on…<br> In your work as a software engineer, you might encounter the task of sorting an array of numbers or searching through a list of inventory, and some other complex searches that you might imagine.</p><p>Now, it begs the question, what does an algorithm have to do with searching and sorting? To answer this question, let’s ask Wikipedia to tell us what an algorithm is (me: “opens a tab on my browser and starts searching”). According to Wikipedia:</p><blockquote>an algorithm is <strong>a self-contained step-by-step set of operations to be performed</strong>.</blockquote><p>In lame man terms, an algorithm is just a step-by-step instruction on how to perform a task or how to solve a particular problem. With that defined, we can say that a <strong>sorting algorithm</strong> is a <em>step-by-step instruction on how to arrange the items in a given list </em>while a <strong>searching algorithm</strong> on the other hand is the <em>step-by-step instructions on how to locate an item in a particular list.</em></p><p>There are several different sorting algorithms in computer science. To name a few, we have:</p><ol><li>Bubble Sort Algorithm</li><li>Selection Sort Algorithm</li><li>Insertion Sort Algorithm</li><li>Merge Sort Algorithm</li><li>Quick Sort Algorithm</li><li>Radix Sort Algorithm</li></ol><p>For searching algorithms, we have (just to name a few):</p><ol><li>Linear Search</li><li>Binary Search</li><li>Interpolation Search</li><li>Exponential Search</li></ol><p>… And so on. There are other more advanced, searching and sorting algorithms that were not mentioned here because of how complex they are. The ones mentioned here are just some of the basic ones, and I’ll do well to explain some of them as much as I can.</p><h4>Why do we need so many algorithms to perform the same task??!!😖</h4><p>To answer that question, we need to look at something called the <strong>Big O Notation. </strong>The Big O Notation is a way of measuring the efficiency of an algorithm.</p><p>There are 2 parts to measuring efficiency — We have the <strong><em>time complexity</em></strong> and the <strong><em>space complexity</em></strong> of an algorithm. Time complexity refers to HOW FAST an algorithm does what it does in the worst case scenario while Space Complexity refers to HOW MUCH SPACE (MEMORY) the algorithm uses while performing its operations in the worst case scenario.</p><p>These 2 measures (time and space complexity) are the major things that differentiate these algorithms from one another. Other things to consider are the ease of implementation of the algorithm, the size of the list you’re working with, and the relationship between the items in the list.</p><p>In the next series of posts, I’ll pick each of the algorithms listed here one after the other (starting from sorting algorithms) and explain them further, including how to implement them in Javascript. Although Javascript provides some helper functions to perform some of these operations (searching and sorting), it helps to know how they work and how to implement them (especially if you’re preparing for a technical interview).</p><p>See you on the <a href="https://narhzih.medium.com/searching-and-sorting-algorithms-in-javascript-part-2-bubble-sort-2a969bfe5aa8">next one</a>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=87eb26b4503a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>