Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A sorting algorithm is said to be <strong>stable</strong> if two objects with equal keys appear in the same order in sorted output as they appear in the input array to be sorted. Some sorting algorithms are stable by nature like Insertion sort, Merge Sort, Bubble Sort, etc. And some sorting algorithms are not, like Heap Sort, Quick Sort, etc.</p> <p><em>Background</em>: a "stable" sorting algorithm keeps the items with the same sorting key in order. Suppose we have a list of 5-letter words:</p> <pre><code>peach straw apple spork </code></pre> <p>If we sort the list by just the first letter of each word then a stable-sort would produce:</p> <pre><code>apple peach straw spork </code></pre> <p>In an <strong>unstable</strong> sort algorithm, <code>straw</code> or <code>spork</code> may be interchanged, but in a stable one, they stay in the same relative positions (that is, since <code>straw</code> appears before <code>spork</code> in the input, it also appears before <code>spork</code> in the output).</p> <p>We could sort the list of words using this algorithm: stable sorting by column 5, then 4, then 3, then 2, then 1. In the end, it will be correctly sorted. Convince yourself of that. (by the way, that algorithm is called radix sort)</p> <p>Now to answer your question, suppose we have a list of first and last names. We are asked to sort "by last name, then by first". We could first sort (stable or unstable) by the first name, then stable sort by the last name. After these sorts, the list is primarily sorted by the last name. However, where last names are the same, the first names are sorted.</p> <p>You can't stack unstable sorts in the same fashion.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload