Sorting algorithms/Bubble sort: Difference between revisions

→‎{{header|Icon}}: note on mixed type sorting
m (→‎{{header|Icon}}: fix sortop)
(→‎{{header|Icon}}: note on mixed type sorting)
Line 943:
 
The following code supports this and other sorting demonstrations.
* Sorting illustrates a difference in the way Icon and Unicon handles data types. Built-in operators for comparing data types make a syntactic distinction between numeric and string types, and sorting structured and user-defined types require custom code. TheAn 'sortop'added procedurecomplication allowsarises variousbecause methodsmixed oftypes comparisonare be selectedallowed. Two Theapproaches exampleare couldpossible behere: made(1) morethat generaltaken toby dealthe withbuilt-in coercionsort which sorts first by type and then value The sort order of types likeis: cset&null, tointeger, real, string, cset, procedure, list, set, table, and record; and (admittedly2) anCoercion uninterestingof exampletypes aswhich csetsis areused alreadyhere sorted(and implemented in 'sortop') to decide on using string or numeric sorting. These sorts will not handle more exotic type mixes.
* The 'sortop' procedure allows various methods of comparison be selected including customized ones. The example could be made more general to deal with coercion of types like cset to string (admittedly an uninteresting example as csets are already sorted). Custom comparators are shown by and example procedure 'cmp'.
* Custom comparators can be easily provided as shown by 'cmp'.
* 'demosort' can apply different sorting procedures and operators to lists and strings to show how this works. The routines 'displaysort' and 'writex' are helpers.
 
Anonymous user