Posted by: prankster September 19, 2010
Any Math CompScience guru ?? Need help..
Login in to Rate this Post:     0       ?        

did you get a chance to look here?


http://en.wikipedia.org/wiki/Sorting_algorithm#Stability


Prolly this is helpful


Stability


Stable sorting algorithms maintain the relative order of records with equal keys. If all keys are different then this distinction is not necessary. But if there are equal keys, then a sorting algorithm is stable if whenever there are two records (let's say R and S) with the same key, and R appears before S in the original list, then R will always appear before S in the sorted list. When equal elements are indistinguishable, such as with integers, or more generally, any data where the entire element is the key, stability is not an issue. However, assume that the following pairs of numbers are to be sorted by their first component:

(4, 2) (3, 7) (3, 1) (5, 6)

In this case, two different results are possible, one which maintains the relative order of records with equal keys, and one which does not:

(3, 7) (3, 1) (4, 2) (5, 6) (order maintained)
(3, 1) (3, 7) (4, 2) (5, 6) (order changed)

Unstable sorting algorithms may change the relative order of records with equal keys, but stable sorting algorithms never do so. Unstable sorting algorithms can be specially implemented to be stable. One way of doing this is to artificially extend the key comparison, so that comparisons between two objects with otherwise equal keys are decided using the order of the entries in the original data order as a tie-breaker. Remembering this order, however, often involves an additional computational cost.


Sorting based on a primary, secondary, tertiary, etc. sort key can be done by any sorting method, taking all sort keys into account in comparisons (in other words, using a single composite sort key). If a sorting method is stable, it is also possible to sort multiple times, each time with one sort key. In that case the keys need to be applied in order of increasing priority.


Example: sorting pairs of numbers as above by first, then second component:

(4, 2) (3, 7) (3, 1) (5, 6) (original)
(3, 1) (4, 2) (5, 6) (3, 7) (after sorting by second component)
(3, 1) (3, 7) (4, 2) (5, 6) (after sorting by first component)

On the other hand:

(3, 7) (3, 1) (4, 2) (5, 6) (after sorting by first component)
(3, 1) (4, 2) (5, 6) (3, 7) (after sorting by second component,
order by first component is disrupted).

Read Full Discussion Thread for this article