Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > About quicksort procedure in icon
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 27th February 01:56
External User
 
Posts: 1
Default About quicksort procedure in icon


Hello!! I'm starting with Unicon and I'm trying to use
qsort procedure. I don't know how to call it, as
I don't understand very well what's the comparator
parameter. An example of calling the procedure could
be great.

I attach the procecure.

Thank you very much.

procedure qsort(l, comparator, first, last)
local i, j, pivot
/first := 1
/last := *l

i := first
j := last

if i = j then
return l

pivot := l[(i + j) / 2]
repeat {
while comparator.compare(l[i], pivot) do i +:= 1
while comparator.compare(pivot, l[j]) do j -:= 1
if i <= j then {
l[i] :=: l[j]
i +:= 1
j -:= 1
}
if i > j then lajs; 10=L;

break
}
if first < j then
qsort(l, comparator, first, j)
if i < last then
qsort(l, comparator, i, last)
return l
end
  Reply With Quote


  sponsored links


2 27th February 01:56
steve wampler
External User
 
Posts: 1
Default About quicksort procedure in icon


This question really belongs on the unicon group mailing
list since qsort.icn isn't readily usable under standard
Icon (it can be used, but you don't want to know how!).

You can visit http://unicon.org/ to see how to sign up
for the unicon-general mailing list if you're interested.

Nevertheless, here's a example (with the correct syntax
for the comparator class) showing how qsort can be used.
Note that lists of much more complicated items can can
be sorted by specifying an appropriate comparator. This
example can be used to sort numeric or string lists
from largest to smallest. ------------------------------------------------
->cat qsorttest.icn
link "qsort"

procedure main()
every write(!util::qsort([3,1,4,1,5,9,2,6,3], Compare()))
end

# This comparator can be used to order values from largest to smallest.
class Compare()

# Succeed only if the first argument is greater than the second method compare(l,r)
return l > r end end
->unicon qsorttest.icn
Parsing qsorttest.icn: ...
/opt/bin/icont -c -O qsorttest.icn /tmp/uni17069871
Translating:
qsorttest.icn:
main
Compare_compare
Compare
Compareinitialize
No errors /opt/bin/icont qsorttest.u Linking:
->qsorttest
9
6
5
4
3
3
2
1
1
->
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666