Implement Bogosort
Perl 6: 23 chars
@s.=pick(*)until[<=] @s
APL(Dyalog), 20
{⍵≡⍵[⍋⍵]:⍵⋄∇⍵[?⍨⍴⍵]}
Explanation
⍵
is the (right) argument
⍵≡⍵[⍋⍵]
: Checks if ⍵
sorted equals ⍵
itself
:⍵
: If yes, then return ⍵
∇⍵[?⍨⍴⍵]
: Else, generate an array of 1 to ⍴⍵
(length of ⍵
) in random order, reorder ⍵
according to that (⍵[...]
), and apply the function to it (∇
)
Suddenly revisiting this problem and...
APL(Dyalog), 19
{∧/2≤/⍵:⍵⋄∇⍵[?⍨⍴⍵]}
Was just thinking about sorting an array in the check makes it kind of pointless (not saying that Bogosort is meaningful), a more accurate implementation would be ∧/2≤/⍵
, and that happens to lower the char count.
Ruby - 33 characters
g=->l{l.shuffle!!=l.sort ?redo:l}