Sorting algorithms/Selection sort: Difference between revisions

Content added Content deleted
(added Ursala)
Line 421: Line 421:
=={{header|Ursala}}==
=={{header|Ursala}}==
The selection_sort function is parameterized by a relational predicate p.
The selection_sort function is parameterized by a relational predicate p.
In this example, the relational predicate on natural number is used.
There are no arrays in Ursala so it uses a list, and the selected item
There are no arrays in Ursala so it uses a list, and the selected item
is deleted from the list on each iteration rather than swapped.
is deleted from the list and inserted into another on each iteration
rather than swapped with a preceding item of the same list.
<lang Ursala>
<lang Ursala>
#import std
#import std
#import nat

selection_sort "p" = @iNX ~&l->rx ^jrX/~&l ^|C/"p"$- ~&


selection_sort "p" = @iNX ~&l->rx ^(gldif ==,~&r)^/~&l ^|C/"p"$- ~&
</lang>
This is already a bad way to code a sorting algorithm in this
language, but with only a bit more work, we can get a bigger and
slower version that more closely simulates the operations of
repeatedly reordering an array.
<lang Ursala>
selection_sort "p" = ~&itB^?a\~&a ^|JahPfatPRC/~& ~=-~BrhPltPClhPrtPCTlrTQrS^D/"p"$- ~&
</lang>
Here is a test program sorting by the partial order relation on natural
numbers.
<lang Ursala>
#import nat
#cast %nL
#cast %nL