Jump to content

Sort stability: Difference between revisions

m
added Category:Sorting
m (added Category:Sorting.)
m (added Category:Sorting)
Line 1:
{{task|Sorting Algorithms}}
{{Sorting Algorithm}}
[[Category:Sorting]]
 
When sorting records in a table by a particular column or field, a [[wp:Stable_sort#Stability|stable sort]] will always retain the relative order of records that have the same key.
 
 
For example, in this table of countries and cities, a stable sort on the '''second''' column, the cities, would keep the US Birmingham above the UK Birmingham. (Although an unstable sort ''might'', in this case, place the US Birmingham above the UK Birmingham, a stable sort routine would ''guarantee'' it).
;Example:
<pre>UK London
For example, inIn this table of countries and cities, a stable sort on the '''second''' column, the cities, would keep the &nbsp; '''US &nbsp;Birmingham''' &nbsp; above the UK Birmingham. (Although an unstable sort&nbsp; ''might'', in this case, place the US Birmingham above the UK &nbsp;Birmingham, a stable sort routine would ''guarantee''. it).
 
(Although an unstable sort ''might'', in this case, place the &nbsp; '''US&nbsp;Birmingham''' &nbsp; above the &nbsp; '''UK&nbsp;Birmingham''', &nbsp; a stable sort routine would ''guarantee'' it).
 
<pre>
<pre>UK London
US New York
US Birmingham
UK Birmingham</pre>
</pre>
Similarly, stable sorting on just the first column would generate “UK London” as the first item and “US Birmingham” as the last item (since the order of the elements having the same first word – “UK” or “US” – would be maintained).
 
Similarly, stable sorting on just the first column would generate “UK London”'''UK&nbsp;London''' as the first item and “US Birmingham”'''US&nbsp;Birmingham''' as the last item &nbsp; (since the order of the elements having the same first word – “UK”&nbsp; '''UK''' or “US”'''US''' &nbsp; – would be maintained).
 
 
;Task:
#Examine the documentation on any in-built sort routines supplied by a language.
:#Indicate if&nbsp; anExamine the documentation on any in-built routinesort isroutines supplied by a language.
:#If supplied,&nbsp; indicateIndicate whetherif or not thean in-built routine is stable.supplied
:# &nbsp; If supplied, indicate whether or not the in-built routine is stable.
 
<br>
Cookies help us deliver our services. By using our services, you agree to our use of cookies.