Sorting algorithms/Bogosort: Difference between revisions
Content added Content deleted
m (→{{header|J}}: Add lang tags) |
m (Fixed lang tags.) |
||
Line 117: | Line 117: | ||
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}} |
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}} |
||
<lang |
<lang algol68>MODE TYPE = INT; |
||
PROC random shuffle = (REF[]TYPE l)VOID: ( |
PROC random shuffle = (REF[]TYPE l)VOID: ( |
||
Line 347: | Line 347: | ||
We use seq-utils' shuffle, which initializes a Java ArrayList with the input sequence, shuffle it, and then return a sequence of the result. |
We use seq-utils' shuffle, which initializes a Java ArrayList with the input sequence, shuffle it, and then return a sequence of the result. |
||
⚫ | |||
<lang clojure> |
|||
⚫ | |||
(defn in-order? [cmp xs] |
(defn in-order? [cmp xs] |
||
Line 358: | Line 357: | ||
(defn bogosort [cmp xs] |
(defn bogosort [cmp xs] |
||
(if (in-order? cmp xs) xs |
(if (in-order? cmp xs) xs |
||
(recur cmp (shuffle xs)))) |
(recur cmp (shuffle xs))))</lang> |
||
⚫ | |||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 436: | Line 434: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
<lang fortran> |
<lang fortran>MODULE BOGO |
||
IMPLICIT NONE |
|||
CONTAINS |
|||
FUNCTION Sorted(a) |
|||
LOGICAL :: Sorted |
|||
INTEGER, INTENT(IN) :: a(:) |
|||
INTEGER :: i |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
DO i = SIZE(a), 1, -1 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
a(i) = temp |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
s = Sorted(array) |
|||
IF (s) EXIT |
|||
⚫ | |||
⚫ | |||
iter = iter + 1 |
|||
END DO |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
s = Sorted(array) |
|||
⚫ | |||
⚫ | |||
iter = iter + 1 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Line 543: | Line 541: | ||
=={{header|Icon}}== |
=={{header|Icon}}== |
||
<lang icon>procedure shuffle(l) |
|||
repeat { |
|||
!l :=: ?l |
|||
suspend l |
|||
} |
|||
end |
|||
procedure sorted(l) |
|||
local i |
|||
if (i := 2 to *l & l[i] >= l[i-1]) then return &fail else return 1 |
|||
end |
|||
procedure main() |
|||
local l |
|||
l := [6,3,4,5,1] |
|||
|( shuffle(l) & sorted(l)) \1 & every writes(" ",!l) |
|||
end</lang> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
<lang j>bogo=: monad define |
<lang j>bogo=: monad define |
||
whilst. -. *./ 2 </\ Ry do. Ry=. (A.~ ?@!@#) y end. Ry |
whilst. -. *./ 2 </\ Ry do. Ry=. (A.~ ?@!@#) y end. Ry |
||
⚫ | |||
) |
|||
</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 616: | Line 613: | ||
} |
} |
||
return v; |
return v; |
||
} |
}</lang> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Line 647: | Line 644: | ||
=={{header|M4}}== |
=={{header|M4}}== |
||
<lang M4> |
<lang M4>divert(-1) |
||
divert(-1) |
|||
define(`randSeed',141592653) |
define(`randSeed',141592653) |
||
define(`setRand', |
define(`setRand', |
||
Line 687: | Line 683: | ||
show(`b') |
show(`b') |
||
bogosort(`b') |
bogosort(`b') |
||
show(`b') |
show(`b')</lang> |
||
</lang> |
|||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
< |
<lang maxscript>fn notSorted arr = |
||
( |
( |
||
if arr.count > 0 then |
if arr.count > 0 then |
||
Line 726: | Line 721: | ||
) |
) |
||
arr |
arr |
||
)</ |
)</lang> |
||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
Line 957: | Line 952: | ||
=={{header|R}}== |
=={{header|R}}== |
||
<lang R> |
<lang R>bogosort <- function(x) |
||
bogosort <- function(x) |
|||
{ |
{ |
||
is.sorted <- function(x) all(diff(x) >= 0) |
is.sorted <- function(x) all(diff(x) >= 0) |
||
Line 1,056: | Line 1,050: | ||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
<lang Ursala> |
<lang Ursala>#import std |
||
#import std |
|||
#import nat |
#import nat |
||