<?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: Ola Adedoyin</title>
    <description>The latest articles on DEV Community by Ola Adedoyin (@olacodetech).</description>
    <link>https://dev.to/olacodetech</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%2F328042%2Fcfe3c71d-c875-4525-a2d6-521f0ee7cfd1.jpg</url>
      <title>DEV Community: Ola Adedoyin</title>
      <link>https://dev.to/olacodetech</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/olacodetech"/>
    <language>en</language>
    <item>
      <title>Sorting Algorithms - Merge Sort</title>
      <dc:creator>Ola Adedoyin</dc:creator>
      <pubDate>Sat, 01 Feb 2020 20:58:19 +0000</pubDate>
      <link>https://dev.to/olacodetech/sorting-algorithms-merge-sort-13n5</link>
      <guid>https://dev.to/olacodetech/sorting-algorithms-merge-sort-13n5</guid>
      <description>&lt;p&gt;&lt;em&gt;Note: This is part of &lt;a href="https://dev.to/olacodetech/sorting-and-sorting-algorithms-480"&gt;this series&lt;/a&gt; of articles on sorting Algorithm&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;Merge Sort as an efficient sorting algorithm that uses the divide-and-conquer approach (The divide-and-conquer approach entails breaking a problem into sub-problems, solving the sub-problems and then combining the solutions back together to solve the initial problem) to perform sorting on an array. Merge Sort is guaranteed to run in O(nlogn) time (we’ll see how later) which is relatively faster than the average and worst case running times of several other sorting algorithms. There are two steps to mergesort - merging and sorting. We can implement the mergesort algorithm iteratively or recursively (you need to have some knowledge of recursion to understand this approach). &lt;/p&gt;

&lt;p&gt;Let’s go over the recursive approach using the divide and conquer technique. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt; Given an array of integers [6, 8, -8, 4, -5, 2, 9], using mergesort, sort and return the array. The sorted array should be in ascending order from left to right.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach:&lt;/strong&gt;  We shall take the following steps in sorting this array;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Declare a base case - if the array has only on element, we want to return the array.&lt;/li&gt;
&lt;li&gt;Divide - We want to split the array into two halves possibly equal in length.&lt;/li&gt;
&lt;li&gt;Conquer - we then use recursion to sort both arrays.&lt;/li&gt;
&lt;li&gt;Combine - after which, we merge the two sorted arrays and return result.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s see what that looks like in code. Note the approach below mutates the original array when doing the merge instead of creating a new result array. &lt;/p&gt;

&lt;p&gt;First we create the mergeSort function: The comments in the code explains what each line of code does.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mergeSort&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// declare the base case&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// find the midpoint of the array&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// split the array into two halves&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;leftArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rightArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// recursively split the array passing the left and right arrays to mergeSort&lt;/span&gt;
  &lt;span class="c1"&gt;// until we only have one element left in which case we return&lt;/span&gt;
  &lt;span class="nx"&gt;mergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;leftArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;mergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rightArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// call the merge function on the array, left array and right array&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;leftArray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;rightArray&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;We called the merge function above but we haven’t created that yet. But we know that after splitting the array we need to merge them in sorted order. So my approach is to create a merge function that takes in three arrays, the leftArray, rightArray and the array from which they were split and perform the merge by comparing the element in the firstArray to the secondArray, one after the other while mutating the original array with lesser element. This maintains sorting stability with no need to create another array to push the sorted elements to.&lt;/p&gt;

&lt;p&gt;Let’s see what that looks like in code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;merge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;leftArray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;rightArray&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// declare pointers to keep track of where we are in each array&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// leftArray pointer&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// rightArray Pointer&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// parent array pointer&lt;/span&gt;
  &lt;span class="c1"&gt;// while we have elements in the leftArray and rightArray&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;leftArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;rightArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// we compare element in the leftArray to the rightArray&lt;/span&gt;
    &lt;span class="c1"&gt;// we mutate the parent array with the element that is lesser&lt;/span&gt;
    &lt;span class="c1"&gt;// we move our pointers accordingly&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;leftArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;rightArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;leftArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
      &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&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="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;rightArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
      &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;k&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// exiting the while loop there might be elements left in leftArray or rightArray&lt;/span&gt;
  &lt;span class="c1"&gt;// that we haven't compared, we mutate the parent array with these elements.&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;leftArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;leftArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;k&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;rightArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;rightArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;k&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// we return the sorted array&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;array&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;h3&gt;
  
  
  Complexity of Merge Sort
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Dividing the array into two parts takes O(1)&lt;/li&gt;
&lt;li&gt;It takes T(n/2) time to solve each half, therefore solving both halves takes 2T(n/2)&lt;/li&gt;
&lt;li&gt;The merge step takes O(n) time.&lt;/li&gt;
&lt;li&gt;Combining the time according to the &lt;a href="https://en.wikipedia.org/wiki/Master_theorem_(analysis_of_algorithms)"&gt;Master Theorem&lt;/a&gt; gives O(nlogn)&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;time complexity&lt;/strong&gt; of mergesort is the same, &lt;strong&gt;O(nlogn)&lt;/strong&gt;,  in best, average and worst case scenario regardless of the number of input. Merge Sort operates on a &lt;strong&gt;space complexity of O(n)&lt;/strong&gt; in worst case.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>computerscience</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>Sorting and Sorting Algorithms</title>
      <dc:creator>Ola Adedoyin</dc:creator>
      <pubDate>Sat, 01 Feb 2020 20:41:24 +0000</pubDate>
      <link>https://dev.to/olacodetech/sorting-and-sorting-algorithms-480</link>
      <guid>https://dev.to/olacodetech/sorting-and-sorting-algorithms-480</guid>
      <description>&lt;p&gt;&lt;em&gt;Note: This will be a series of articles on sorting Algorithms&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is sorting?
&lt;/h3&gt;

&lt;p&gt;Sorting can be defined as the arranging of the elements in a collection or list in increasing or decreasing order of some defined property. There are reasons why we may want to sort data or arrange things in a certain order, one reason could be for better readability of the data and another reason could be just to search or extract information from the data or collection. For example, if we visit the Today’s Deals page on Amazon, there is an option in the form of a dropdown to sort the products on that page by different criteria. The sorting criteria includes; featured, Price-Low to High, Price-High to Low, Discount - Low to High, DIscount High to Low. Selecting any of these options will re-arrange the products by the selected option. In this case, we have used sorting to re-arrange how we want to view the products on the page. Another example of where we might like to keep our data sorted is a Language Dictionary, where we want to keep the words sorted so that searching for a word in the dictionary is easy. Complex computer programs such as searching files on a computer, compressing data and finding the shortest route to a destination are founded upon some form of sorting algorithm.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is sorting algorithm?
&lt;/h3&gt;

&lt;p&gt;Now that we understand what sorting is and ways that it can be implemented, we can now dive into the algorithms that perform the sorting. Sorting a list or collection require that the list should be homologous, that is all the elements in the list should all be of the same type. Most times we use a list of integers and typically would sort the list of integers in increasing or decreasing order of value. For example given the following list of integers: 2, 3, 9, 4, 6, we could sort this list based on some property:&lt;/p&gt;

&lt;p&gt;Input: 2, 3, 9, 4, 6&lt;/p&gt;

&lt;p&gt;A. Output: 2, 3, 4, 6, 9 =&amp;gt; sorted based on increasing order of value&lt;br&gt;
    B. Output: 9, 6, 4, 3, 2 =&amp;gt; sorted based on decreasing order of value&lt;br&gt;
    C. Output: 2, 3, 9, 4, 6 =&amp;gt; sorted based on increasing order of number of factors&lt;/p&gt;

&lt;p&gt;In the first case above, we sorted the list based on the increasing order of the items in the list.&lt;br&gt;
In the second case, we sorted the list based on the decreasing order of the items in the list.&lt;br&gt;
In the third case, we sorted the list based on the increasing order of the number of factors that each integer has. We can also sort a list of strings or words in lexicographical order and may also need to sort very complex data types. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A sorting algorithm is an algorithm made-up of a series of instructions that takes an array as input, performs specified operations on the array, sometimes called a list, and outputs a sorted array.  - brilliant.org&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Why do we need to worry about sorting data and why has years of research and studies been invested on finding the fastest and efficient sorting algorithms? The teaching and understanding of sorting algorithms help introduce other key computer science concepts like Big-O notation, divide-and-conquer methods, and data structures such as binary trees, and heaps. Sorted data is very helpful for computational power of machines.&lt;/p&gt;

&lt;p&gt;If we store a list in computer memory as unsorted and we want to search for an item in that list, we would have to do a &lt;strong&gt;linear search&lt;/strong&gt;, starting from the first item in the list and keep scanning until we find the item we are looking for. In the case we didn’t find the item in the list (our worst case scenario) we would have searched through the whole list comparing each item to the item we are looking for. So, if there are n items in the list, in the worst case scenario, we will make n comparisons. Imagine the amount of data handled by the modern day computer, so if we take &lt;em&gt;n = 2⁶⁴&lt;/em&gt;, and assuming it takes 1 millisecond to compute one comparison, the computational time for the comparison in a worse case scenario will equal &lt;em&gt;2⁶⁴&lt;/em&gt; milliseconds.  I’m not going to try and convert that to hours, days or even years, but we can all agree that it will take some years for that computation to be complete. Nobody wants to sit around for that long waiting for some computation to be over, as we all know, time is valuable. However, if we store our list as a sorted list in computer memory, then we can use a &lt;strong&gt;binary search&lt;/strong&gt; to find our item. In this case, for our n size list it will take &lt;em&gt;log₂n&lt;/em&gt; milliseconds for the computation to complete.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;If we take n = 2⁶⁴; &lt;br&gt;
log₂n =&amp;gt;  log₂2⁶⁴&lt;br&gt;
=&amp;gt; 64log₂2&lt;br&gt;
=&amp;gt; 64 * 1 =&amp;gt; 64&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;(Note: log₂2 is equal to 1).&lt;/p&gt;

&lt;p&gt;Therefore, it will take 64 milliseconds to complete the comparison computation. Wow! What a difference, right?! With a sorted list we have been able to save huge amount of time and can move on to other important things.&lt;/p&gt;

&lt;p&gt;The importance of sorting as a problem has led to the design of different sorting algorithms we have today. Some of these sorting algorithms include; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bubble sort&lt;/li&gt;
&lt;li&gt;Selection sort&lt;/li&gt;
&lt;li&gt;Insertion sort&lt;/li&gt;
&lt;li&gt;Merge sort&lt;/li&gt;
&lt;li&gt;Heap sort&lt;/li&gt;
&lt;li&gt;Counting sort&lt;/li&gt;
&lt;li&gt;Radix sort.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Classification of sorting algorithms
&lt;/h3&gt;

&lt;p&gt;Sorting algorithms are often classified based on different parameters, some of which are listed below;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity:&lt;/strong&gt; This is the rate of growth of time taken by an algorithm with respect to input size. Some algorithms are relatively faster than others. The running time of an algorithm describes the number of operations an algorithm must perform before it completes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity or Memory Usage:&lt;/strong&gt; This parameter further classifies sorting algorithm into In-Place sorting and Not-In-Place sorting.
-- &lt;em&gt;In-Place Sorting Algorithm:&lt;/em&gt; This sorting algorithm uses constant amount of extra memory to rearrange items in a list. It sorts the array by modifying the element order directly within the array. No extra data structure is used. Example of sorting algorithms with In-Place sorting are Bubble Sort, Selection Sort, Insertion Sort, Heap Sort.
-- &lt;em&gt;Not-In-Place Sorting Algorithm:&lt;/em&gt; This sorting algorithm uses extra data structure to sort the array therefore uses extra space or memory. Examples include Merge Sort which require an additional O(n) space to perform the merge operation, and Quick sort.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stability:&lt;/strong&gt; A stable sorting algorithm in the case of equality of key, or property used to define the sorting, preserves the relative order of elements. Therefore,  If there are identical items in the list to be sorted, this sorting algorithm ensures that these identical items are sorted in the same order they appear in the input. This is guaranteed by a stable sorting algorithm. Examples are Bubble Sort, Insertion Sort, Merge Sort, Counting Sort. In an unstable sort, the order of identical elements can not be guaranteed to stay the same in the output as they appear in the input. Examples include Quick Sort, Heap Sort, Selection Sort. Generally, an unstable sorting algorithm is faster than a stable sorting algorithm.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Internal and External:&lt;/strong&gt; When all the data to be sorted are in the main memory or RAM, then such a sort is internal sort. Mostly use for small dataset. An external sort is when the dataset are on an auxiliary storage like hard disk often because it’s not possible to have them all in memory at once. Usually used to handle huge amount of data at a time.
Recursive or Non-Recursive: Some sorting algorithms are recursive, e.g. Quick Sort and Merge Sort (They both use the divide-and-conquer approach to sort an array) while some are non recursive like Insertion Sort and Selection Sort.&lt;/li&gt;
&lt;/ul&gt;

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