The current Icon/Unicon bubble sort solution:
http://rosettacode.org/wiki/Sorting_algorithms/Bubble_sort#Icon
only sorts lists of strings or integers. I'd like to change the
code to:
invocable all
procedure bubblesort(X,op) #: simple bubble sort
local i,sorted
op := case op of { # select how to sort
"string": "<<"
"numeric": "<"
&null: if type(X[1]) == "string" then "<<" else "<"
default: op
}
op := proc(op, 2) | runerr(123, image(op))
while /sorted := 1 do # the sort
every i := 2 to *X do
if op(X[i],X[i-1]) then {
X[i-1] :=: X[i]
sorted := &null
}
return X
end
to allow sorting *anything*, provided the correct comparison
operation is passed in. For example, if we have:
procedure cmp(a,b)
return a < b # Imagine some complex test here!
end
then:
displaysort(copy(l1),"cmp")
and
displaysort(copy(l1),cmp)
would both work instead of generating a runerr 123.
Are people OK with this change?
--
Steve Wampler -- [email protected]
The gods that smiled on your birth are now laughing out loud.
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Unicon-group mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/unicon-group