<?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: David Inoa</title>
    <description>The latest articles on DEV Community by David Inoa (@davidinoa).</description>
    <link>https://dev.to/davidinoa</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%2F37595%2F7c4ee600-2634-4902-8988-0abdf87e172a.jpeg</url>
      <title>DEV Community: David Inoa</title>
      <link>https://dev.to/davidinoa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/davidinoa"/>
    <language>en</language>
    <item>
      <title>Implementing Counting Sort with JavaScript</title>
      <dc:creator>David Inoa</dc:creator>
      <pubDate>Tue, 13 Aug 2019 02:05:23 +0000</pubDate>
      <link>https://dev.to/davidinoa/implementing-counting-sort-with-javascript-2lo8</link>
      <guid>https://dev.to/davidinoa/implementing-counting-sort-with-javascript-2lo8</guid>
      <description>&lt;p&gt;Counting sort is an algorithm that, in certain cases, allows you to sort an array in linear time. To use counting sort your array must only contain integers, and it is assumed you know the minimum and maximum values.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fdt5kywjt727u7uh77e6w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fdt5kywjt727u7uh77e6w.png" alt="counting sort algorithm"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Breakdown
&lt;/h2&gt;

&lt;p&gt;(1) Create a new array &lt;code&gt;counts&lt;/code&gt; in which you will keep track of how many times an integer is present in your input array. The length of &lt;code&gt;counts&lt;/code&gt; will be determined by the range of the values in your input array. Each index in &lt;code&gt;counts&lt;/code&gt; will correspond to an integer, and each value at this particular index will tell us how many times the integer is present in our input.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fnapcn0riwemqz1lhvlpl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fnapcn0riwemqz1lhvlpl.png" alt="code fragment"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(2) If all our integers are positive, we could just keep track of the count for each one at the index in &lt;code&gt;counts&lt;/code&gt; corresponding to its value (i.e. &lt;code&gt;counts[3]&lt;/code&gt; is where we save our count for an integer &lt;code&gt;3&lt;/code&gt;). But for this algorithm to work with negatives, we must determine an &lt;code&gt;offset&lt;/code&gt; value to helps us map both negative and positive integers to an index in &lt;code&gt;counts&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fs9y65xmav5imgkjmq6oh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fs9y65xmav5imgkjmq6oh.png" alt="code fragment"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(3) Populate &lt;code&gt;counts&lt;/code&gt; by iterating through your original array. Every time we encounter an integer, we increase its count by one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fdjbmli2ph0prt63o3bl6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fdjbmli2ph0prt63o3bl6.png" alt="code fragment"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(4) Initialize an array to hold the integers in ascending order.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fw2ap0mlcwow2m2k6e1xd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fw2ap0mlcwow2m2k6e1xd.png" alt="code fragment"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(5) Loop through all the integers between your &lt;code&gt;min&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt;. At each iteration, determine the &lt;code&gt;count&lt;/code&gt; for the current &lt;code&gt;integer&lt;/code&gt;. If the &lt;code&gt;count&lt;/code&gt; is &lt;code&gt;0&lt;/code&gt;, we continue to the next one. But if the &lt;code&gt;count&lt;/code&gt; is greater than &lt;code&gt;0&lt;/code&gt;, we push that integer to &lt;code&gt;sortedIntegers&lt;/code&gt; as many times as indicated by &lt;code&gt;count&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F6it7at1q4sm0zhozehb3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F6it7at1q4sm0zhozehb3.png" alt="code fragment"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(6) Return your sorted array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fddx9bd06t258l4rx240u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fddx9bd06t258l4rx240u.png" alt="code fragment"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Complexity
&lt;/h2&gt;

&lt;p&gt;In the worst-case scenario, the complexity of this algorithm is as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Time complexity: &lt;code&gt;O(n + k)&lt;/code&gt;, where &lt;code&gt;n&lt;/code&gt; is the numbers of integers in your original array and &lt;code&gt;k&lt;/code&gt; is the range of your &lt;code&gt;min&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt; values.&lt;/li&gt;
&lt;li&gt;Space complexity: &lt;code&gt;O(n + k)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When &lt;code&gt;k&lt;/code&gt; is less or equal to &lt;code&gt;n&lt;/code&gt;, both complexities reduce to &lt;code&gt;O(n)&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>sort</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
