[REBOL] Re: sort/compare
From: tomc:darkwing:uoregon at: 8-Dec-2003 20:22
Hi Anton,
> Just wondering if there is a way to find
> the indices of the pair of values passed
> to the compare function (directly, without using find).
>
> Here's a simple use of sort/compare:
>
> sort/compare a: [1 4 9 7 3 2] func [v1 v2][v2 < v1]
>
> Now here you can see how many comparisons
> occurred and at what positions they were at (hopefully):
>
> n: 0
> sort/compare a: [1 4 9 7 3 2] func [v1 v2][
> print [index? find a v1 index? find a v2 ":" v1 v2]
> n: n + 1
> v1 < v2
> ]
> ?? n
>
> (At the end, n = 16).
> (Should be able to figure what type of sort is used..)
>
> But, consider that my values might not be unique, so using
>
> index? find ...
>
> is unreliable.
>
> Anton.
well we know it can be a stable sort (if the compare returns -1 0 1)
which rules out several ... and the sort may change with the length of the
series being sorted ...
but here you go have fun
random/seed now
val: make object![v:0 i: 0]
a: copy []
repeat n 10[
insert tail a make val[v: random 10 i: n] prin [a/:n/v " "]
]
print ""
n: 0
sort/compare a func[v1 v2][
print [n: n + 1 tab v1/i v2/i ":" v1/v v2/v] v1/v < v2/v
]