Sorting algorithms/Bogosort: Difference between revisions

Content added Content deleted
(→‎{{header|Ruby}}: ++ smalltalk)
Line 694: Line 694:
l
l
end</lang>
end</lang>

=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
This implementation uses closures rather than extending collections to provide a bogosort method.
<lang smalltalk>Smalltalk at: #isItSorted put: [ :c |
|isit|
isit := false.
(2 to: (c size)) detect: [ :i |
( (c at: ( i - 1 )) > (c at: i) )
] ifNone: [ isit := true ].
isit
].
Smalltalk at: #bogosort put: [ :c |
[ isItSorted value: c ] whileFalse: [
1 to: (c size) do: [ :i |
|r t|
r := (Random between: 1 and: (c size)).
t := (c at: i).
c at: i put: (c at: r).
c at: r put: t
]
]
].

|tobesorted|
tobesorted := { 2 . 7 . 5 . 3 . 4 . 8 . 6 . 1 }.
bogosort value: tobesorted.
tobesorted displayNl.</lang>