<?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 Kalanithi Sethuraman on Medium]]></title>
        <description><![CDATA[Stories by Kalanithi Sethuraman on Medium]]></description>
        <link>https://medium.com/@kalanithi_sas?source=rss-4cc323105643------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*uPGK4Nvwa4yLmpQuHbFhGw.jpeg</url>
            <title>Stories by Kalanithi Sethuraman on Medium</title>
            <link>https://medium.com/@kalanithi_sas?source=rss-4cc323105643------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 22:47:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kalanithi_sas/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[JavaScript : Sorting Algorithms]]></title>
            <link>https://medium.com/@kalanithi_sas/javascript-sorting-algorithms-7ba8ce3fc45f?source=rss-4cc323105643------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ba8ce3fc45f</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[sorting-algorithms]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Kalanithi Sethuraman]]></dc:creator>
            <pubDate>Thu, 22 Aug 2019 08:11:46 GMT</pubDate>
            <atom:updated>2019-08-26T11:11:15.536Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4ENTv5SsXJ_vuUgjKbs0dg.png" /></figure><h3>What is sorting ?</h3><p>Sorting is the process of rearranging the items in a collection (e.g. an array) so that the items are in some kind of order.</p><h4>Example</h4><ul><li>Sorting numbers from smallest to largest</li><li>Sorting names alphabetically</li><li>Sorting movies based on release year</li><li>Sorting movies based on revenue</li></ul><h3>Why do we need to learn this?</h3><ul><li>Sorting is an incredibly common task, so it’s good to know how it works</li><li>There are many different ways to sort things, and different techniques have their own advantages and disadvantages</li><li>Sorting sometimes has quirks, so it’s good to understand how to navigate them</li></ul><h3>JavaScript has a sort method</h3><p>Yes it Does. But it doesn’t work as you expected.</p><pre>[ &quot;Nithi&quot;, &quot;Kala&quot;, &quot;Data Structures&quot;, &quot;Algorithms&quot; ].sort()</pre><pre>// [“Algorithms”, “Data Structures”, “Kala”, “Nithi”]</pre><pre>[ 3, 6, 14, 12 ].sort();</pre><pre>// [12, 14, 3, 6]</pre><h3>Telling JavaScript how to sort</h3><ul><li>The built-in sort method accepts an optional comparator function</li><li>You can use this comparator function to tell JavaScript how you want it to sort</li><li>The comparator looks at pairs of elements (a and b), determines their sort order based on the return value</li></ul><p>If it returns a negative number, a should come before b</p><p>If it returns a positive number, a should come after b,</p><p>If it returns 0, a and b are the same as far as the sort is concerned</p><h4>Example</h4><pre>[ 3, 6, 14, 12 ].sort(<strong><em>function</em></strong>(a,b){ return a — b});</pre><pre>// [3, 6, 12, 14]</pre><pre>[ &quot;Nithi&quot;, &quot;Kala&quot;, &quot;Data Structures&quot;, &quot;Algorithms&quot; ].sort(<strong><em>function</em></strong>(a,b){return a.length - b.length});</pre><pre>// [&quot;Kala&quot;, &quot;Nithi&quot;, &quot;Algorithms&quot;, &quot;Data Structures&quot;]</pre><h3>BubbleSort</h3><p>Bubble sort is based on the idea of repeatedly comparing pairs of adjacent elements and then swapping their positions if they exist in the wrong order.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K-O6r4X6e1ITK_IwfuOGPg.png" /></figure><h4>Example Solution :</h4><pre><em>function</em> <strong>bubbleSort</strong>(<em>arr</em>){</pre><pre>var noSwaps;</pre><pre>for(var i = arr.length; i &gt; 0; i--){</pre><pre>  noSwaps = true;</pre><pre>  for(var j = 0; j &lt; i - 1; j++){</pre><pre>    if(arr[j] &gt; arr[j+1]){</pre><pre>    var temp = arr[j];</pre><pre>    arr[j] = arr[j+1];</pre><pre>    arr[j+1] = temp;</pre><pre>    noSwaps = false;</pre><pre>  }</pre><pre>}</pre><pre>if(noSwaps) break;</pre><pre>}</pre><pre>return arr;</pre><pre>}</pre><pre><strong>bubbleSort</strong>([8,1,2,3,4,5,6,7]);</pre><h4>Time Complexity</h4><p>Worst Case — O(n²)</p><p>Best Case — O(n)</p><h3>Insertion Sort</h3><p><strong>Insertion sort</strong> is a simple sorting algorithm that builds the final sorted array (or list) one item at a time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/818/1*_NOe6jL9veyR4yAyf85dzA.png" /></figure><p><strong>Example Solution:</strong></p><pre><em>function</em> <strong>insertionSort</strong>(arr){ <br>  var currentVal;<br>  for(var i = 1; i &lt; arr.length; i++){        <br>    currentVal = arr[i];<br>    for(var j = i - 1; j &gt;= 0 &amp;&amp; arr[j] &gt; currentVal; j--) {           arr[j+1] = arr[j];<br>  }        <br>    arr[j+1] = currentVal;    <br>  }    <br> return arr;<br>} </pre><pre><strong>insertionSort</strong>([2,1,9,76,4]);</pre><h4>Time Complexity</h4><p>Worst Case — O(n²)</p><p>Best Case — O(n)</p><h3>Selection Sort</h3><p>Similar to bubble sort, but instead of first placing large values into sorted position, it places small values into sorted position</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/838/1*mheUvfO_0QZECXRhasVSrQ.png" /></figure><h4>Example <strong>Solution</strong>:</h4><pre><em>function</em> <strong>selectionSort</strong>(arr){    <br>  for(var i = 0; i &lt; arr.length; i++){        <br>   var lowest = i;<br>   for(var j = i+1; j &lt; arr.length; j++){<br>    if(arr[j] &lt; arr[lowest]){<br>     lowest = j;<br>    }<br>   }<br>  if(i !== lowest){<br>   //SWAP!<br>   var temp = arr[i];<br>   arr[i] = arr[lowest];<br>   arr[lowest] = temp;<br>  }<br> }<br>return arr;<br>}</pre><pre><strong>selectionSort</strong>([0,2,34,22,10,19,17]);</pre><h4>Time Complexity</h4><p>O(n²)</p><h3>Merge Sort</h3><ul><li>It’s a combination of two things — merging and sorting!</li><li>Exploits the fact that arrays of 0 or 1 element are always sorted</li><li>Works by decomposing an array into smaller arrays of 0 or 1 elements, then building up a newly sorted array</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DHropU8tA3fmbDo40X0fqw.png" /></figure><h4>Example Solution:</h4><pre><em>function</em> <strong>mergeArray</strong>(arr1, arr2) {<br>  var i = 0;<br>  var j = 0;<br>  var result = [];<br>   while (i &lt; arr1.length &amp;&amp; j &lt; arr2.length) {<br>     if (arr1[i] &gt; arr2[j]) {<br>      result.push(arr2[j]);<br>      j++;<br>     } else {<br>      result.push(arr1[i]);<br>      i++;<br>     }<br>   }<br>   while (i &lt; arr1.length) {<br>     result.push(arr1[i]);<br>     i++;<br>   }<br>   while (j &lt; arr2.length) {<br>     result.push(arr2[j]);<br>     j++;<br>   }<br> return result;<br>}</pre><pre><em>function</em> <strong>mergeSort</strong>(arr){<br>  if(arr.length &lt;= 1) return arr;<br>  var middle = Math.floor(arr.length/2);<br>  var left = <strong>mergeSort</strong>(arr.slice(0,middle));<br>  var right = <strong>mergeSort</strong>(arr.slice(middle));<br>  return <strong>mergeArray</strong>(left, right);<br>}</pre><pre>mergeSort([7,5,9,3,8,11,24,23,18,26,63,37,54]);</pre><h4>Time Complexity</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UyHLPtzbH5EdvDDfymWptg.png" /></figure><h3>Quick Sort</h3><p>Quick sort is an efficient sorting algorithm, serving as a systematic method for placing the elements of a random access file or an array in order. Developed by British computer scientist Tony Hoare in 1959 and published in 1961, it is still a commonly used algorithm for sorting.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vtxf3GlV_C0sHubAbM3S4g.png" /></figure><h4>Example Solution:</h4><pre><em>function</em> <strong>pivot</strong>(arr, start = 0, end = arr.length - 1) {<br> const swap = (arr, idx1, idx2) =&gt; {<br>   [arr[idx1], arr[idx2]] = [arr[idx2], arr[idx1]];<br> };<br> // We are assuming the pivot is always the first element<br> let pivot = arr[start];<br> let swapIdx = start;<br> for (let i = start + 1; i &lt;= end; i++) {<br>   if (pivot &gt; arr[i]) {<br>     swapIdx++;<br>     swap(arr, swapIdx, i);<br>   }<br> }<br> // Swap the pivot from the start the swap point<br> swap(arr, start, swapIdx);<br> return swapIdx;<br>}</pre><pre><em>function</em> <strong>quickSort</strong>(arr, left = 0, right = arr.length -1){<br>  if(left &lt; right){<br>    let pivotIndex = <strong>pivot</strong>(arr, left, right) //3<br>    //left<br>    <strong>quickSort</strong>(arr,left,pivotIndex-1);<br>    //right<br>    <strong>quickSort</strong>(arr,pivotIndex+1,right);<br>    }<br>  return arr;<br>}</pre><pre><strong>quickSort</strong>([100,-3,2,4,6,9,1,2,5,3,23]);</pre><h4>Time Complexity</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9Hh5TRCZRc1S-JECwVxTMg.png" /></figure><h3>Radix sort</h3><p>Radix sort is a special sorting algorithm that works on lists of numbers</p><p>It never makes comparisons between elements!</p><p>It exploits the fact that information about the size of a number is encoded in the number of digits.</p><p>More digits means a bigger number!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/654/1*qMGKyX37vvDHFRGmYpzhvA.png" /></figure><h4>Example Solution:</h4><pre><em>function</em> <strong>getDigit</strong>(<em>num</em>, <em>i</em>) {</pre><pre>  return Math.floor(Math.abs(num) / Math.pow(10, i)) % 10;</pre><pre>}</pre><pre><em>function</em> <strong>digitCount</strong>(<em>num</em>) {</pre><pre>  if (num === 0) return 1;</pre><pre>  return Math.floor(Math.log10(Math.abs(num))) + 1;</pre><pre>}</pre><pre><em>function</em> <strong>mostDigits</strong>(<em>nums</em>) {</pre><pre>  let maxDigits = 0;</pre><pre>  for (let i = 0; i &lt; nums.length; i++) {</pre><pre>    maxDigits = Math.max(maxDigits, <strong>digitCount</strong>(nums[i]));</pre><pre>  }</pre><pre> return maxDigits;</pre><pre>}</pre><pre><em>function</em> <strong>radixSort</strong>(<em>nums</em>){</pre><pre>  let maxDigitCount = <strong>mostDigits</strong>(nums);</pre><pre>  for(let k = 0; k &lt; maxDigitCount; k++){</pre><pre>    let digitBuckets = <em>Array</em>.from({length: 10}, () =&gt; []);</pre><pre>    for(let i = 0; i &lt; nums.length; i++){</pre><pre>      let digit = <strong>getDigit</strong>(nums[i],k);</pre><pre>      digitBuckets[digit].push(nums[i]);</pre><pre>    }</pre><pre>    nums = [].concat(...digitBuckets);</pre><pre> }</pre><pre> return nums;</pre><pre>}</pre><pre><strong>radixSort</strong>([23,345,5467,12,2345,9852]);</pre><h4>Time Complexity</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kXcjA-4ritF7-VaFqM8dfA.png" /></figure><p>Please click 👏 button below a few times to show your support! ⬇⬇</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ba8ce3fc45f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>