<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Franco Chalissery</title>
    <subtitle>Software architect writing about Linux, Kubernetes, distributed systems, algorithms, and other technical things.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://tofchaliss.com/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://tofchaliss.com"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-01-26T00:00:00+00:00</updated>
    <id>https://tofchaliss.com/atom.xml</id>
    <entry xml:lang="en">
        <title>Sorting in capsule</title>
        <published>2026-01-26T00:00:00+00:00</published>
        <updated>2026-01-26T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Franco Chalissery
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://tofchaliss.com/blog/sorting/"/>
        <id>https://tofchaliss.com/blog/sorting/</id>
        
        <content type="html" xml:base="https://tofchaliss.com/blog/sorting/">&lt;p&gt;This blog is a snapshot of all well known sorting algorithms and its attributes.&lt;&#x2F;p&gt;
&lt;div style=&quot;overflow-x: auto&quot;&gt;
&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th rowspan=&quot;2&quot;&gt;Algorithm&lt;&#x2F;th&gt;
      &lt;th colspan=&quot;3&quot;&gt;Time complexity&lt;&#x2F;th&gt;
      &lt;th rowspan=&quot;2&quot;&gt;Space complexity&lt;&#x2F;th&gt;
      &lt;th rowspan=&quot;2&quot;&gt;Attributes&lt;&#x2F;th&gt;
    &lt;&#x2F;tr&gt;
    &lt;tr&gt;
      &lt;th&gt;Best case&lt;&#x2F;th&gt;
      &lt;th&gt;Average case&lt;&#x2F;th&gt;
      &lt;th&gt;Worst case&lt;&#x2F;th&gt;
    &lt;&#x2F;tr&gt;
  &lt;&#x2F;thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;tofchaliss&#x2F;dsa-algo&#x2F;blob&#x2F;main&#x2F;sorting&#x2F;insertionsort.py&quot;&gt;Insertion Sort&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;
      &lt;td&gt;O(n)&lt;&#x2F;td&gt;
      &lt;td&gt;O(n²)&lt;&#x2F;td&gt;
      &lt;td&gt;O(n²)&lt;&#x2F;td&gt;
      &lt;td&gt;O(1)&lt;&#x2F;td&gt;
      &lt;td&gt;Stable, In-Place, Comparison based, used as base case for hybrid sort (timsort)&lt;&#x2F;td&gt;
    &lt;&#x2F;tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;tofchaliss&#x2F;dsa-algo&#x2F;blob&#x2F;main&#x2F;sorting&#x2F;selectionsort.py&quot;&gt;Selection Sort&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;
      &lt;td&gt;O(n²)&lt;&#x2F;td&gt;
      &lt;td&gt;O(n²)&lt;&#x2F;td&gt;
      &lt;td&gt;O(n²)&lt;&#x2F;td&gt;
      &lt;td&gt;O(1)&lt;&#x2F;td&gt;
      &lt;td&gt;Not Stable, In-Place, Comparison based, used in heavy memory write use cases , used in small dataset&lt;&#x2F;td&gt;
    &lt;&#x2F;tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;tofchaliss&#x2F;dsa-algo&#x2F;blob&#x2F;main&#x2F;sorting&#x2F;bubblesort.py&quot;&gt;Bubble Sort&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;
      &lt;td&gt;O(n)&lt;&#x2F;td&gt;
      &lt;td&gt;O(n²)&lt;&#x2F;td&gt;
      &lt;td&gt;O(n²)&lt;&#x2F;td&gt;
      &lt;td&gt;O(1)&lt;&#x2F;td&gt;
      &lt;td&gt;Stable, In-Place, Comparison based&lt;&#x2F;td&gt;
    &lt;&#x2F;tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;tofchaliss&#x2F;dsa-algo&#x2F;blob&#x2F;main&#x2F;sorting&#x2F;mergesort.py&quot;&gt;Merge Sort&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;
      &lt;td&gt;O(n log n)&lt;&#x2F;td&gt;
      &lt;td&gt;O(n log n)&lt;&#x2F;td&gt;
      &lt;td&gt;O(n log n)&lt;&#x2F;td&gt;
      &lt;td&gt;O(n)&lt;&#x2F;td&gt;
      &lt;td&gt;Stable, Not In-Place, Comparison based, large dataset, external sorting (files, directories)&lt;&#x2F;td&gt;
    &lt;&#x2F;tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;tofchaliss&#x2F;dsa-algo&#x2F;blob&#x2F;main&#x2F;sorting&#x2F;quicksort.py&quot;&gt;Quick Sort&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;
      &lt;td&gt;O(n log n)&lt;&#x2F;td&gt;
      &lt;td&gt;O(n log n)&lt;&#x2F;td&gt;
      &lt;td&gt;O(n²)&lt;&#x2F;td&gt;
      &lt;td&gt;O(log n) (average)&lt;br&gt;O(n) (worst)&lt;&#x2F;td&gt;
      &lt;td&gt;Not Stable, In-Place, Comparison based, general purpose&lt;&#x2F;td&gt;
    &lt;&#x2F;tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;tofchaliss&#x2F;dsa-algo&#x2F;blob&#x2F;main&#x2F;sorting&#x2F;heapsort.py&quot;&gt;Heap Sort&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;
      &lt;td&gt;O(n log n)&lt;&#x2F;td&gt;
      &lt;td&gt;O(n log n)&lt;&#x2F;td&gt;
      &lt;td&gt;O(n log n)&lt;&#x2F;td&gt;
      &lt;td&gt;O(1)&lt;&#x2F;td&gt;
      &lt;td&gt;Not Stable, In-Place, Comparison based, when guaranteed to be O(n log n) and constant space requirement&lt;&#x2F;td&gt;
    &lt;&#x2F;tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;tofchaliss&#x2F;dsa-algo&#x2F;blob&#x2F;main&#x2F;sorting&#x2F;countingsort.py&quot;&gt;Counting Sort&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;
      &lt;td&gt;O(n + k)&lt;&#x2F;td&gt;
      &lt;td&gt;O(n + k)&lt;&#x2F;td&gt;
      &lt;td&gt;O(n + k)&lt;&#x2F;td&gt;
      &lt;td&gt;O(n + k)&lt;&#x2F;td&gt;
      &lt;td&gt;Stable, Not In-Place, Not Comparison based, Integer with small know range (score, age)&lt;&#x2F;td&gt;
    &lt;&#x2F;tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;tofchaliss&#x2F;dsa-algo&#x2F;blob&#x2F;main&#x2F;sorting&#x2F;bucketsort.py&quot;&gt;Bucket Sort&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;
      &lt;td&gt;O(n + k)&lt;&#x2F;td&gt;
      &lt;td&gt;O(n + k)&lt;&#x2F;td&gt;
      &lt;td&gt;O(n²)&lt;&#x2F;td&gt;
      &lt;td&gt;O(n + k)&lt;&#x2F;td&gt;
      &lt;td&gt;Depends on hash function(Stable or not), Not In-Place, Not Comparison based, Uniformly distributed floating point numbers&lt;&#x2F;td&gt;
    &lt;&#x2F;tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;tofchaliss&#x2F;dsa-algo&#x2F;blob&#x2F;main&#x2F;sorting&#x2F;radixsort.py&quot;&gt;Radix Sort&lt;&#x2F;a&gt;&lt;&#x2F;td&gt;
      &lt;td&gt;O(d · (n + k))&lt;&#x2F;td&gt;
      &lt;td&gt;O(d · (n + k))&lt;&#x2F;td&gt;
      &lt;td&gt;O(d · (n + k))&lt;&#x2F;td&gt;
      &lt;td&gt;O(n + k)&lt;&#x2F;td&gt;
      &lt;td&gt;Stable, Not In-Place, Not Comparison based, Integer with small know range (id, phone number)&lt;&#x2F;td&gt;
    &lt;&#x2F;tr&gt;
  &lt;&#x2F;tbody&gt;
&lt;&#x2F;table&gt;
&lt;&#x2F;div&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Sorting Algorithm Stability - What it is and When it Matters</title>
        <published>2020-12-14T00:00:00+00:00</published>
        <updated>2020-12-14T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Franco Chalissery
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://tofchaliss.com/blog/sorting-stability/"/>
        <id>https://tofchaliss.com/blog/sorting-stability/</id>
        
        <content type="html" xml:base="https://tofchaliss.com/blog/sorting-stability/">&lt;p&gt;There are many sorting algorithms out there that are widely accepted: Insertion sort, quick sort, heap sort, merge sort, bucket sort… and so on and so forth. There are &lt;em&gt;a lot&lt;&#x2F;em&gt; of qualities of each sorting algorithm that allow you to choose what algorithm fits your requirements, but one of the sorting algorithm qualities I want to talk about in this post is &lt;strong&gt;stability&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;sorting-stability&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#sorting-stability&quot; aria-label=&quot;Anchor link for: sorting-stability&quot;&gt;&lt;i class=&quot;icon&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt;
Sorting stability&lt;&#x2F;h2&gt;
&lt;p&gt;Stability of a sorting algorithmn is the label that indicates whether or not it will preserve the order of two or more elements in the array &lt;strong&gt;if they have equal sorting keys&lt;&#x2F;strong&gt;. A sorting algorithm is &lt;strong&gt;stable&lt;&#x2F;strong&gt; if it preserves the order of multiple elements with the same keys. Conversely, a sorting algorithm is &lt;strong&gt;unstable&lt;&#x2F;strong&gt; if it cannot guarantee order preservation of multiple elements with equal keys.&lt;&#x2F;p&gt;
&lt;p&gt;An example in real life: There is a line of people that need to be ordered by age. If there are two people with the same age standing in line, the sorter would be using a stable approach if there is a guarantee that their order in line relative to each remains the same.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;when-does-stability-matter&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#when-does-stability-matter&quot; aria-label=&quot;Anchor link for: when-does-stability-matter&quot;&gt;&lt;i class=&quot;icon&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt;
When does stability matter?&lt;&#x2F;h2&gt;
&lt;p&gt;Sorting stability is interesting by itself, but when do we care about it? We care in a situation when we &lt;em&gt;require&lt;&#x2F;em&gt; the output to have the guarantee of preserving order for duplicate keys.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;example-data&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#example-data&quot; aria-label=&quot;Anchor link for: example-data&quot;&gt;&lt;i class=&quot;icon&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt;
Example data&lt;&#x2F;h3&gt;
&lt;p&gt;Let’s see another example and dissect it. Let’s say we have an array of data, where each element has two fields: &lt;code&gt;Name&lt;&#x2F;code&gt; and &lt;code&gt;State&lt;&#x2F;code&gt;. Here is a visual respresentation of the data:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;sort_stability_01.png&quot; alt=&quot;Dataset&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;requirement&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#requirement&quot; aria-label=&quot;Anchor link for: requirement&quot;&gt;&lt;i class=&quot;icon&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt;
Requirement&lt;&#x2F;h3&gt;
&lt;p&gt;Sort the data by state and then by name, so that the final data is ordered by state first and then by name.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;approach&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#approach&quot; aria-label=&quot;Anchor link for: approach&quot;&gt;&lt;i class=&quot;icon&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt;
Approach&lt;&#x2F;h3&gt;
&lt;p&gt;This data will be sorted twice so that the requirements can be met:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Sort first by &lt;code&gt;Name&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Sort second by &lt;code&gt;State&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;The first sort by &lt;code&gt;Name&lt;&#x2F;code&gt; doesn’t require a stable sorting algorithm, as the order for duplicate sorting keys (names) doesn’t matter. So in this case, we could do a heap sort (a naturally unstable sorting algorithmn) on this array by name.&lt;&#x2F;p&gt;
&lt;p&gt;The output from sorting by &lt;code&gt;Name&lt;&#x2F;code&gt; is:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;sort_stability_03.png&quot; alt=&quot;Dataset sorted by Name&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The second sort by &lt;code&gt;State&lt;&#x2F;code&gt; is where it can get interesting. If we were to try a heap sort by &lt;code&gt;State&lt;&#x2F;code&gt;, this is how we would setup our complete binary tree:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;sort_stability_02.png&quot; alt=&quot;Heap sort start by State&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;If we were to continue the heap sort by &lt;code&gt;State&lt;&#x2F;code&gt;, we would end up with an array sorted like:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;sort_stability_04.png&quot; alt=&quot;Output of heap sort by state&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Yes, the array is sorted correctly by &lt;code&gt;State&lt;&#x2F;code&gt;, but let’s see how the elements themselves transitioned:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;sort_stability_05.png&quot; alt=&quot;Element transition&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The fact that elements with the same sorting key (&lt;code&gt;State&lt;&#x2F;code&gt;) have overlapping sections to the destination array elements indicates that &lt;strong&gt;their order was not preserved&lt;&#x2F;strong&gt;, showing that this is a &lt;strong&gt;unstable&lt;&#x2F;strong&gt; sorting algorithmn.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-solution&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-solution&quot; aria-label=&quot;Anchor link for: the-solution&quot;&gt;&lt;i class=&quot;icon&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt;
The solution&lt;&#x2F;h3&gt;
&lt;p&gt;If we were to look back at the requirements, the heap sort by &lt;code&gt;State&lt;&#x2F;code&gt; does not work: The output is sorted by &lt;code&gt;State&lt;&#x2F;code&gt; but it is no longer sorted also by &lt;code&gt;Name&lt;&#x2F;code&gt; within each &lt;code&gt;State&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Let’s try doing a second sort (by &lt;code&gt;State&lt;&#x2F;code&gt;) with a naturally stable sorting algorithm: Merge sort. The output of the second sort if we were to use a merge sort (instead of a heap sort) would be:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;sort_stability_06.png&quot; alt=&quot;Merge sort output&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Of course, &lt;code&gt;State&lt;&#x2F;code&gt; is correctly sorted but now let’s see how the elments transitioned from being sorted by &lt;code&gt;Name&lt;&#x2F;code&gt; to a merge sort by &lt;code&gt;State&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;sort_stability_07.png&quot; alt=&quot;Element transition merge sort&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Now because we used a stable sorting algorithmn (merge sort) on &lt;code&gt;State&lt;&#x2F;code&gt;, we preserved the order of elements with the same &lt;code&gt;State&lt;&#x2F;code&gt; key value, giving us the desired output of a dataset that is sorted by state and then by name.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;summary&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#summary&quot; aria-label=&quot;Anchor link for: summary&quot;&gt;&lt;i class=&quot;icon&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt;
Summary&lt;&#x2F;h2&gt;
&lt;p&gt;Sorting algorithmns are a large part of computing, and understanding the behaviors of different algorithms will help in determining the right one for your requirements.&lt;&#x2F;p&gt;
</content>
        
    </entry>
</feed>
