Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In reading about various sorting algorithms I've seen it mentioned that some are "stable" and some are not. What does that mean, and what tradeoffs are involved on that basis when selecting an algorithm?

share|improve this question
15  
This question would be easily answered within a minute with wikipedia. en.wikipedia.org/wiki/Sorting_algorithm –  Mare Infinitus 17 hours ago
2  
@MareInfinitus More precisely : en.wikipedia.org/wiki/Sorting_algorithm#Stability –  BЈовић 5 hours ago
    
here is a good answer https://class.coursera.org/algs4partI-005/lecture/34 –  zangw 4 hours ago
    
This is a question lacking own research. Answered with a wikipedia picture. And it gets really good feedback, which somehow makes me sad. IMHO it should be closed, and not get upvotes. –  Mare Infinitus 6 mins ago
add comment

2 Answers

A stable sort is one which preserves the original order of the input set, where the comparison algorithm does not distinguish between two or more items.

Consider a sorting algorithm that sorts cards by rank, but not by suit. The stable sort will guarantee that the original order of cards having the same suit is preserved; the unstable sort will not.

enter image description here

share|improve this answer
13  
Correction: The unstable algorithm exhibits undefined behaviour when two elements are equal, it is perfectly possible that the order is sometimes preserved. –  eBusiness 17 hours ago
    
@eBusiness: FTFY. –  Robert Harvey 17 hours ago
3  
Nice picture, very similar to wikipedia en.wikipedia.org/wiki/Sorting_algorithm –  Mare Infinitus 17 hours ago
4  
@MareInfinitus: It's in the public domain. Check the attribution on the original image. –  Robert Harvey 17 hours ago
1  
A good picture explains faster and deeper than a lot of words on the average case. Legal issues were not what I wanted to talk about. –  Mare Infinitus 17 hours ago
add comment

Stable algorithms preserve the relative order of elements.

So a stable sorting algorithm will retain the relative order of values which compare as equal.

Consider a sorting algorithm where we sort a collection of 2d points based on their X dimension.

Collection to be sorted: {(6, 3), (5, 5), (6, 1), (1, 3)}

Stable Sorted: {(1, 3), (5, 5), (6, 3), (6, 1)}

Regular Sorted: Either {(1, 3), (5, 5), (6, 3), (6, 1)}, or {(1, 3), (5, 5), (6, 1), (6, 3)}


As for the tradeoff... well, stable sorting is less efficient, but sometimes you need it.

For example when a user clicks the a column header to sort values in a UI, it's reasonable to expect his previous sorting order to be used in the case of equal values.

share|improve this answer
    
Is it less efficient though? It seems "obvious", but some of the best sorting algorithms are stable by nature (e.g. anything based on merge sort, such as Tim sort), they don't need to do any explicit extra work to be stable. –  delnan 17 hours ago
5  
Stable has nothing to do with performance in general. Mergesort runs in O(n*log n) and is stable. Heapsort has similar performance, but is not stable. –  Mare Infinitus 17 hours ago
1  
"Stable" can also apply to data-structures, eg. a "stable heap" is a heap which dequeues items that have the same priority in the same order they were queued. This is very important for efficient path-finding algorithms. –  BlueRaja - Danny Pflughoeft 15 hours ago
    
There are no stable sorts which are O(n ln n) comparisons and also O(1) on memory. Speed is not the only measure of efficiency. The fact that you can't stable sort in-place matters. –  QuestionC 1 hour ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.