[Jprogramming] sorting using verb returing a boolean
Raul Miller
rauldmiller at gmail.com
Thu Dec 21 21:52:58 UTC 2023
It's probably worth noting here that (for example) the quicksort
algorithm relies on being able to tell whether items are equal. And a
0 1 result can't distinguish between ordering and equality. Many
sorting implementations rely on a _1, 0, 1 result, as a consequence.
That said, you could implement a sorting algorithm like bubble sort
using a 0/1 result, and that could be a verb. (Also... rosettacode.org
is currently down, due to a name server issue, but when it is up it
has a decent collection of sorting algorithms implemented in J, and I
think some of them could also be pressed into service here).
Meanwhile, for aoc2023 day 7, the part 1 hand ranker could have been
{{ (\:~ #/.~ y),'23456789TJQKA' i. y }}"1 (part 2 was slightly more
complicated and of course migrated the 'J' to the first element of
that character list.
I hope this helps,
--
Raul
On Thu, Dec 21, 2023 at 9:03 AM 'Viktor Grigorov' via Programming
<programming at jsoftware.com> wrote:
>
> Hey,
>
> Is there an easy way to sort an array with verb returning either 0 or 1, like the comparison primitives?
>
> The verb that I'd in mind relates to the 2023's advent of code's day 7: given two equal length hands, which one has the first high card. Which one can write as:
>
> 'K2345' ( 2 | 0 { [: I. [: , [: (<,.>)/ ,: & ('23456789TJQKA'i.]) ) 'KJ2KA'
> 0 NB. here meaning left is not greater right
>
> Cheers,
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
More information about the Programming
mailing list