Sorting algorithms/Bogosort: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Eiffel}}: fix irregular markup) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 25: | Line 25: | ||
<br><br> |
<br><br> |
||
=={{header|11l}}== |
=={{header|11l}}== |
||
< |
<syntaxhighlight lang="11l">F is_sorted(data) |
||
R all((0 .< data.len - 1).map(i -> @data[i] <= @data[i + 1])) |
R all((0 .< data.len - 1).map(i -> @data[i] <= @data[i + 1])) |
||
Line 34: | Line 34: | ||
V arr = [2, 1, 3] |
V arr = [2, 1, 3] |
||
bogosort(&arr) |
bogosort(&arr) |
||
print(arr)</ |
print(arr)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 43: | Line 43: | ||
=={{header|AArch64 Assembly}}== |
=={{header|AArch64 Assembly}}== |
||
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
||
<syntaxhighlight lang="aarch64 assembly"> |
|||
<lang AArch64 Assembly> |
|||
/* ARM assembly AARCH64 Raspberry PI 3B */ |
/* ARM assembly AARCH64 Raspberry PI 3B */ |
||
/* program bogosort64.s */ |
/* program bogosort64.s */ |
||
Line 217: | Line 217: | ||
/* for this file see task include a file in language AArch64 assembly */ |
/* for this file see task include a file in language AArch64 assembly */ |
||
.include "../includeARM64.inc" |
.include "../includeARM64.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">PROC PrintArray(INT ARRAY a INT size) |
||
INT i |
INT i |
||
Line 289: | Line 289: | ||
Test(c,8) |
Test(c,8) |
||
Test(d,12) |
Test(d,12) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Bogosort.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Bogosort.png Screenshot from Atari 8-bit computer] |
||
Line 315: | Line 315: | ||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
< |
<syntaxhighlight lang="actionscript">public function bogoSort(arr:Array):Array |
||
{ |
{ |
||
while (!sorted(arr)) |
while (!sorted(arr)) |
||
Line 351: | Line 351: | ||
return true; |
return true; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
||
with Ada.Numerics.Discrete_Random; |
with Ada.Numerics.Discrete_Random; |
||
Line 403: | Line 403: | ||
Put (Integer'Image (Sequence (I))); |
Put (Integer'Image (Sequence (I))); |
||
end loop; |
end loop; |
||
end Test_Bogosort;</ |
end Test_Bogosort;</syntaxhighlight> |
||
The solution is generic. |
The solution is generic. |
||
The procedure Bogosort can be instantiated |
The procedure Bogosort can be instantiated |
||
Line 422: | Line 422: | ||
{{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}} |
||
< |
<syntaxhighlight lang="algol68">MODE TYPE = INT; |
||
PROC random shuffle = (REF[]TYPE l)VOID: ( |
PROC random shuffle = (REF[]TYPE l)VOID: ( |
||
Line 458: | Line 458: | ||
[6]TYPE sample := (61, 52, 63, 94, 46, 18); |
[6]TYPE sample := (61, 52, 63, 94, 46, 18); |
||
print((bogo sort(sample), new line))</ |
print((bogo sort(sample), new line))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
+18 +46 +52 +61 +63 +94 |
+18 +46 +52 +61 +63 +94 |
||
Line 464: | Line 464: | ||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi}} |
{{works with|as|Raspberry Pi}} |
||
<syntaxhighlight lang="arm assembly"> |
|||
<lang ARM Assembly> |
|||
/* ARM assembly Raspberry PI */ |
/* ARM assembly Raspberry PI */ |
||
Line 714: | Line 714: | ||
bx lr |
bx lr |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">bogoSort: function [items][ |
||
a: new items |
a: new items |
||
while [not? sorted? a]-> shuffle 'a |
while [not? sorted? a]-> shuffle 'a |
||
Line 724: | Line 724: | ||
] |
] |
||
print bogoSort [3 1 2 8 5 7 9 4 6]</ |
print bogoSort [3 1 2 8 5 7 9 4 6]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 731: | Line 731: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">MsgBox % Bogosort("987654") |
||
MsgBox % Bogosort("319208") |
MsgBox % Bogosort("319208") |
||
MsgBox % Bogosort("fedcba") |
MsgBox % Bogosort("fedcba") |
||
Line 764: | Line 764: | ||
} |
} |
||
Return Found |
Return Found |
||
}</ |
}</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
Sort standard input and output to the standard output |
Sort standard input and output to the standard output |
||
< |
<syntaxhighlight lang="awk">function randint(n) |
||
{ |
{ |
||
return int(n * rand()) |
return int(n * rand()) |
||
Line 797: | Line 797: | ||
print line[i] |
print line[i] |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
< |
<syntaxhighlight lang="bbcbasic"> DIM test(9) |
||
test() = 4, 65, 2, 31, 0, 99, 2, 83, 782, 1 |
test() = 4, 65, 2, 31, 0, 99, 2, 83, 782, 1 |
||
Line 823: | Line 823: | ||
IF d(I%) < d(I%-1) THEN = FALSE |
IF d(I%) < d(I%-1) THEN = FALSE |
||
NEXT |
NEXT |
||
= TRUE</ |
= TRUE</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 830: | Line 830: | ||
=={{header|Brat}}== |
=={{header|Brat}}== |
||
< |
<syntaxhighlight lang="brat">bogosort = { list | |
||
sorted = list.sort #Kinda cheating here |
sorted = list.sort #Kinda cheating here |
||
while { list != sorted } { list.shuffle! } |
while { list != sorted } { list.shuffle! } |
||
Line 836: | Line 836: | ||
} |
} |
||
p bogosort [15 6 2 9 1 3 41 19]</ |
p bogosort [15 6 2 9 1 3 41 19]</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <stdbool.h> |
#include <stdbool.h> |
||
Line 875: | Line 875: | ||
for (i=0; i < 6; i++) printf("%d ", numbers[i]); |
for (i=0; i < 6; i++) printf("%d ", numbers[i]); |
||
printf("\n"); |
printf("\n"); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{works with|C sharp|C#|3.0+}} |
{{works with|C sharp|C#|3.0+}} |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
Line 926: | Line 926: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
Uses C++11. Compile with |
Uses C++11. Compile with |
||
g++ -std=c++11 bogo.cpp |
g++ -std=c++11 bogo.cpp |
||
< |
<syntaxhighlight lang="cpp">#include <algorithm> |
||
#include <iostream> |
#include <iostream> |
||
#include <iterator> |
#include <iterator> |
||
Line 959: | Line 959: | ||
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " ")); |
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " ")); |
||
std::cout << "\n"; |
std::cout << "\n"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 967: | Line 967: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(defn in-order? [order xs] |
||
(or (empty? xs) |
(or (empty? xs) |
||
(apply order xs))) |
(apply order xs))) |
||
Line 975: | Line 975: | ||
(recur order (shuffle xs)))) |
(recur order (shuffle xs)))) |
||
(println (bogosort < [7 5 12 1 4 2 23 18]))</ |
(println (bogosort < [7 5 12 1 4 2 23 18]))</syntaxhighlight> |
||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
This program generates an array of ten pseudo-random numbers in the range 0 to 999 and then sorts them into ascending order. Eventually. |
This program generates an array of ten pseudo-random numbers in the range 0 to 999 and then sorts them into ascending order. Eventually. |
||
< |
<syntaxhighlight lang="cobol">identification division. |
||
program-id. bogo-sort-program. |
program-id. bogo-sort-program. |
||
data division. |
data division. |
||
Line 1,043: | Line 1,043: | ||
add 1 to array-index giving adjusted-index. |
add 1 to array-index giving adjusted-index. |
||
if item(array-index) is greater than item(adjusted-index) |
if item(array-index) is greater than item(adjusted-index) |
||
then move zero to sorted.</ |
then move zero to sorted.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>BEFORE SORT: 141 503 930 105 78 518 180 907 791 361 |
<pre>BEFORE SORT: 141 503 930 105 78 518 180 907 791 361 |
||
Line 1,055: | Line 1,055: | ||
<code>nshuffle</code> is the same code as in [[Knuth shuffle#Common Lisp|Knuth shuffle]]. |
<code>nshuffle</code> is the same code as in [[Knuth shuffle#Common Lisp|Knuth shuffle]]. |
||
< |
<syntaxhighlight lang="lisp">(defun nshuffle (sequence) |
||
(loop for i from (length sequence) downto 2 |
(loop for i from (length sequence) downto 2 |
||
do (rotatef (elt sequence (random i)) |
do (rotatef (elt sequence (random i)) |
||
Line 1,066: | Line 1,066: | ||
(defun bogosort (list predicate) |
(defun bogosort (list predicate) |
||
(do ((list list (nshuffle list))) |
(do ((list list (nshuffle list))) |
||
((sortedp list predicate) list)))</ |
((sortedp list predicate) list)))</syntaxhighlight> |
||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
< |
<syntaxhighlight lang="crystal">def knuthShuffle(items : Array) |
||
i = items.size-1 |
i = items.size-1 |
||
while i > 1 |
while i > 1 |
||
Line 1,094: | Line 1,094: | ||
knuthShuffle(items) |
knuthShuffle(items) |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.random; |
||
void bogoSort(T)(T[] data) { |
void bogoSort(T)(T[] data) { |
||
Line 1,108: | Line 1,108: | ||
bogoSort(array); |
bogoSort(array); |
||
writeln(array); |
writeln(array); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1, 2, 3, 5, 6, 7, 8, 11, 41]</pre> |
<pre>[1, 2, 3, 5, 6, 7, 8, 11, 41]</pre> |
||
Line 1,118: | Line 1,118: | ||
Using the shuffle from [[Knuth shuffle#E]]. |
Using the shuffle from [[Knuth shuffle#E]]. |
||
< |
<syntaxhighlight lang="e">def isSorted(list) { |
||
if (list.size() == 0) { return true } |
if (list.size() == 0) { return true } |
||
var a := list[0] |
var a := list[0] |
||
Line 1,133: | Line 1,133: | ||
shuffle(list, random) |
shuffle(list, random) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
<syntaxhighlight lang="eiffel"> |
|||
<lang Eiffel> |
|||
class |
class |
||
BOGO_SORT |
BOGO_SORT |
||
Line 1,203: | Line 1,203: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
TEST: |
TEST: |
||
<syntaxhighlight lang="eiffel"> |
|||
<lang Eiffel> |
|||
class |
class |
||
APPLICATION |
APPLICATION |
||
Line 1,238: | Line 1,238: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,247: | Line 1,247: | ||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 5.0 : |
ELENA 5.0 : |
||
< |
<syntaxhighlight lang="elena">import extensions; |
||
import system'routines; |
import system'routines; |
||
Line 1,271: | Line 1,271: | ||
console.printLine("before:", list.asEnumerable()); |
console.printLine("before:", list.asEnumerable()); |
||
console.printLine("after :", list.bogoSorter().asEnumerable()) |
console.printLine("after :", list.bogoSorter().asEnumerable()) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,279: | Line 1,279: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">defmodule Sort do |
||
def bogo_sort(list) do |
def bogo_sort(list) do |
||
if sorted?(list) do |
if sorted?(list) do |
||
Line 1,291: | Line 1,291: | ||
defp sorted?([x, y | _]) when x>y, do: false |
defp sorted?([x, y | _]) when x>y, do: false |
||
defp sorted?([_, y | rest]), do: sorted?([y | rest]) |
defp sorted?([_, y | rest]), do: sorted?([y | rest]) |
||
end</ |
end</syntaxhighlight> |
||
Example: |
Example: |
||
Line 1,300: | Line 1,300: | ||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
< |
<syntaxhighlight lang="euphoria">function shuffle(sequence s) |
||
object temp |
object temp |
||
integer j |
integer j |
||
Line 1,331: | Line 1,331: | ||
end function |
end function |
||
? bogosort(shuffle({1,2,3,4,5,6}))</ |
? bogosort(shuffle({1,2,3,4,5,6}))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,344: | Line 1,344: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: grouping kernel math random sequences ; |
||
: sorted? ( seq -- ? ) 2 <clumps> [ first2 <= ] all? ; |
: sorted? ( seq -- ? ) 2 <clumps> [ first2 <= ] all? ; |
||
: bogosort ( seq -- newseq ) [ dup sorted? ] [ randomize ] until ;</ |
: bogosort ( seq -- newseq ) [ dup sorted? ] [ randomize ] until ;</syntaxhighlight> |
||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
< |
<syntaxhighlight lang="fantom"> |
||
class Main |
class Main |
||
{ |
{ |
||
Line 1,377: | Line 1,377: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
< |
<syntaxhighlight lang="fortran">MODULE BOGO |
||
IMPLICIT NONE |
IMPLICIT NONE |
||
CONTAINS |
CONTAINS |
||
Line 1,429: | Line 1,429: | ||
WRITE (*,*) "Array required", iter, " shuffles to sort" |
WRITE (*,*) "Array required", iter, " shuffles to sort" |
||
END PROGRAM BOGOSORT</ |
END PROGRAM BOGOSORT</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">sub shuffle( a() as long ) |
||
dim as ulong n = ubound(a), i, j, k, m = ubound(a)*2 |
dim as ulong n = ubound(a), i, j, k, m = ubound(a)*2 |
||
dim as ulong tmp |
dim as ulong tmp |
||
Line 1,469: | Line 1,469: | ||
for i=0 to ubound(a) - 1 |
for i=0 to ubound(a) - 1 |
||
print a(i) |
print a(i) |
||
next i</ |
next i</syntaxhighlight> |
||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
'''[https://gambas-playground.proko.eu/?gist=b2b766f379d809cbf054c2d32d76c453 Click this link to run this code]''' |
'''[https://gambas-playground.proko.eu/?gist=b2b766f379d809cbf054c2d32d76c453 Click this link to run this code]''' |
||
< |
<syntaxhighlight lang="gambas">Public Sub Main() |
||
Dim sSorted As String = "123456789" 'The desired outcome |
Dim sSorted As String = "123456789" 'The desired outcome |
||
Dim sTest, sChr As String 'Various strings |
Dim sTest, sChr As String 'Various strings |
||
Line 1,491: | Line 1,491: | ||
Print "Solved in " & Str(iCounter) & " loops" 'Print the result |
Print "Solved in " & Str(iCounter) & " loops" 'Print the result |
||
End</ |
End</syntaxhighlight> |
||
Output: (This example was completed in under 2 seconds) |
Output: (This example was completed in under 2 seconds) |
||
<pre> |
<pre> |
||
Line 1,504: | Line 1,504: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,525: | Line 1,525: | ||
} |
} |
||
fmt.Println("sorted! ", temp) |
fmt.Println("sorted! ", temp) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} (sometimes takes a few seconds) |
{{out}} (sometimes takes a few seconds) |
||
<pre> |
<pre> |
||
Line 1,534: | Line 1,534: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Solution (also implicitly tracks the number of shuffles required): |
Solution (also implicitly tracks the number of shuffles required): |
||
< |
<syntaxhighlight lang="groovy">def bogosort = { list -> |
||
def n = list.size() |
def n = list.size() |
||
while (n > 1 && (1..<n).any{ list[it-1] > list[it] }) { |
while (n > 1 && (1..<n).any{ list[it-1] > list[it] }) { |
||
Line 1,541: | Line 1,541: | ||
} |
} |
||
list |
list |
||
}</ |
}</syntaxhighlight> |
||
Test Program: |
Test Program: |
||
< |
<syntaxhighlight lang="groovy">println (bogosort([3,1,2]))</syntaxhighlight> |
||
{{out}} trial 1: |
{{out}} trial 1: |
||
Line 1,553: | Line 1,553: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import System.Random |
||
import Data.Array.IO |
import Data.Array.IO |
||
import Control.Monad |
import Control.Monad |
||
Line 1,582: | Line 1,582: | ||
bogosort :: Ord a => [a] -> IO [a] |
bogosort :: Ord a => [a] -> IO [a] |
||
bogosort = bogosortBy (<)</ |
bogosort = bogosortBy (<)</syntaxhighlight> |
||
Example: |
Example: |
||
<pre> |
<pre> |
||
Line 1,590: | Line 1,590: | ||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang="icon">procedure shuffle(l) |
||
repeat { |
repeat { |
||
!l :=: ?l |
!l :=: ?l |
||
Line 1,606: | Line 1,606: | ||
l := [6,3,4,5,1] |
l := [6,3,4,5,1] |
||
|( shuffle(l) & sorted(l)) \1 & every writes(" ",!l) |
|( shuffle(l) & sorted(l)) \1 & every writes(" ",!l) |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Inform 6}}== |
=={{header|Inform 6}}== |
||
< |
<syntaxhighlight lang="inform 6">[ shuffle a n i j tmp; |
||
for(i = n - 1: i > 0: i--) |
for(i = n - 1: i > 0: i--) |
||
{ |
{ |
||
Line 1,634: | Line 1,634: | ||
shuffle(a, n); |
shuffle(a, n); |
||
} |
} |
||
];</ |
];</syntaxhighlight> |
||
=={{header|Io}}== |
=={{header|Io}}== |
||
< |
<syntaxhighlight lang="io">List do( |
||
isSorted := method( |
isSorted := method( |
||
slice(1) foreach(i, x, |
slice(1) foreach(i, x, |
||
Line 1,653: | Line 1,653: | ||
lst := list(2, 1, 4, 3) |
lst := list(2, 1, 4, 3) |
||
lst bogoSortInPlace println # ==> list(1, 2, 3, 4), hopefully :)</ |
lst bogoSortInPlace println # ==> list(1, 2, 3, 4), hopefully :)</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
{{eff note|J|/:~}} |
{{eff note|J|/:~}} |
||
< |
<syntaxhighlight lang="j">bogo=: monad define |
||
whilst. +./ 2 >/\ Ry do. Ry=. (A.~ ?@!@#) y end. Ry |
whilst. +./ 2 >/\ Ry do. Ry=. (A.~ ?@!@#) y end. Ry |
||
)</ |
)</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
Without Collections, Lists or Iterators. With a counter. |
Without Collections, Lists or Iterators. With a counter. |
||
< |
<syntaxhighlight lang="java"> |
||
public class BogoSort |
public class BogoSort |
||
Line 1,719: | Line 1,719: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,729: | Line 1,729: | ||
{{works with|Java|1.5+}} |
{{works with|Java|1.5+}} |
||
This implementation works for all comparable types (types with <tt>compareTo</tt> defined). |
This implementation works for all comparable types (types with <tt>compareTo</tt> defined). |
||
< |
<syntaxhighlight lang="java5">import java.util.Collections; |
||
import java.util.List; |
import java.util.List; |
||
import java.util.Iterator; |
import java.util.Iterator; |
||
Line 1,752: | Line 1,752: | ||
Collections.shuffle(list); |
Collections.shuffle(list); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript">shuffle = function(v) { |
||
for(var j, x, i = v.length; i; j = Math.floor(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x); |
for(var j, x, i = v.length; i; j = Math.floor(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x); |
||
return v; |
return v; |
||
Line 1,774: | Line 1,774: | ||
} |
} |
||
return v; |
return v; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{works with|Julia|0.6}} |
{{works with|Julia|0.6}} |
||
< |
<syntaxhighlight lang="julia">function bogosort!(arr::AbstractVector) |
||
while !issorted(arr) |
while !issorted(arr) |
||
shuffle!(arr) |
shuffle!(arr) |
||
Line 1,787: | Line 1,787: | ||
v = rand(-10:10, 10) |
v = rand(-10:10, 10) |
||
println("# unordered: $v\n -> ordered: ", bogosort!(v))</ |
println("# unordered: $v\n -> ordered: ", bogosort!(v))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,795: | Line 1,795: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
const val RAND_MAX = 32768 // big enough for this |
const val RAND_MAX = 32768 // big enough for this |
||
Line 1,830: | Line 1,830: | ||
bogosort(a) |
bogosort(a) |
||
println("After sorting : ${a.contentToString()}") |
println("After sorting : ${a.contentToString()}") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,839: | Line 1,839: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">function bogosort (list) |
||
if type (list) ~= 'table' then return list end |
if type (list) ~= 'table' then return list end |
||
Line 1,864: | Line 1,864: | ||
return list |
return list |
||
end</ |
end</syntaxhighlight> |
||
=={{header|M4}}== |
=={{header|M4}}== |
||
< |
<syntaxhighlight lang="m4">divert(-1) |
||
define(`randSeed',141592653) |
define(`randSeed',141592653) |
||
define(`setRand', |
define(`setRand', |
||
Line 1,906: | Line 1,906: | ||
show(`b') |
show(`b') |
||
bogosort(`b') |
bogosort(`b') |
||
show(`b')</ |
show(`b')</syntaxhighlight> |
||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">arr := Array([2,3,1]): |
||
len := numelems(arr): |
len := numelems(arr): |
||
#Translation of C, random swapping |
#Translation of C, random swapping |
||
Line 1,924: | Line 1,924: | ||
shuffle_arr(arr, len): |
shuffle_arr(arr, len): |
||
end do: |
end do: |
||
arr;</ |
arr;</syntaxhighlight> |
||
{{Out|Output}} |
{{Out|Output}} |
||
<pre>[1 2 3]</pre> |
<pre>[1 2 3]</pre> |
||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">Bogosort[x_List] := Block[{t=x},While[!OrderedQ[t],t=RandomSample[x]]; t] |
||
Bogosort[{1, 2, 6, 4, 0, -1, Pi, 3, 5}] |
Bogosort[{1, 2, 6, 4, 0, -1, Pi, 3, 5}] |
||
=> {-1, 0, 1, 2, 3, Pi, 4, 5, 6}</ |
=> {-1, 0, 1, 2, 3, Pi, 4, 5, 6}</syntaxhighlight> |
||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang="matlab">function list = bogoSort(list) |
||
while( ~issorted(list) ) %Check to see if it is sorted |
while( ~issorted(list) ) %Check to see if it is sorted |
||
list = list( randperm(numel(list)) ); %Randomly sort the list |
list = list( randperm(numel(list)) ); %Randomly sort the list |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="matlab">bogoSort([5 3 8 4 9 7 6 2 1]) |
||
ans = |
ans = |
||
1 2 3 4 5 6 7 8 9 |
1 2 3 4 5 6 7 8 9 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
< |
<syntaxhighlight lang="maxscript">fn notSorted arr = |
||
( |
( |
||
if arr.count > 0 then |
if arr.count > 0 then |
||
Line 1,985: | Line 1,985: | ||
) |
) |
||
arr |
arr |
||
)</ |
)</syntaxhighlight> |
||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
< |
<syntaxhighlight lang="modula3">MODULE Bogo EXPORTS Main; |
||
IMPORT IO, Fmt, Random; |
IMPORT IO, Fmt, Random; |
||
Line 2,034: | Line 2,034: | ||
END; |
END; |
||
IO.Put("\nRequired " & Fmt.Int(count) & " shuffles\n"); |
IO.Put("\nRequired " & Fmt.Int(count) & " shuffles\n"); |
||
END Bogo.</ |
END Bogo.</syntaxhighlight> |
||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |
||
< |
<syntaxhighlight lang="nanoquery">def sorted(list) |
||
if len(list) = 0 |
if len(list) = 0 |
||
return true |
return true |
||
Line 2,057: | Line 2,057: | ||
return list |
return list |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Nemerle}}== |
=={{header|Nemerle}}== |
||
< |
<syntaxhighlight lang="nemerle">using System; |
||
using System.Console; |
using System.Console; |
||
using Nemerle.Imperative; |
using Nemerle.Imperative; |
||
Line 2,098: | Line 2,098: | ||
foreach (i in sortme) Write($"$i "); |
foreach (i in sortme) Write($"$i "); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref savelog symbols nobinary |
options replace format comments java crossref savelog symbols nobinary |
||
Line 2,148: | Line 2,148: | ||
method isFalse public static returns boolean |
method isFalse public static returns boolean |
||
return \isTrue |
return \isTrue |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,156: | Line 2,156: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import random |
||
randomize() |
randomize() |
||
Line 2,173: | Line 2,173: | ||
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782] |
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782] |
||
bogoSort a |
bogoSort a |
||
echo a</ |
echo a</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>@[-31, 0, 2, 2, 4, 65, 83, 99, 782]</pre> |
<pre>@[-31, 0, 2, 2, 4, 65, 83, 99, 782]</pre> |
||
Line 2,179: | Line 2,179: | ||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
{{Works with|Oxford Oberon-2 Compiler}} |
{{Works with|Oxford Oberon-2 Compiler}} |
||
< |
<syntaxhighlight lang="oberon2">MODULE Bogo; |
||
IMPORT Out, Random; |
IMPORT Out, Random; |
||
Line 2,229: | Line 2,229: | ||
END; |
END; |
||
Out.Ln; |
Out.Ln; |
||
END Bogo.</ |
END Bogo.</syntaxhighlight> |
||
Init initializes the array as 1 thru 10, then it is shuffled, and then the while loop continually shuffles until Sorted returns true. |
Init initializes the array as 1 thru 10, then it is shuffled, and then the while loop continually shuffles until Sorted returns true. |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">let rec is_sorted comp = function |
||
| e1 :: e2 :: r -> comp e1 e2 <= 0 && is_sorted comp (e2 :: r) |
| e1 :: e2 :: r -> comp e1 e2 <= 0 && is_sorted comp (e2 :: r) |
||
| _ -> true |
| _ -> true |
||
Line 2,253: | Line 2,253: | ||
li |
li |
||
else |
else |
||
bogosort (shuffle li)</ |
bogosort (shuffle li)</syntaxhighlight> |
||
Example: |
Example: |
||
<pre> |
<pre> |
||
Line 2,263: | Line 2,263: | ||
We use an array because that made most sense for the Knuth Shuffle task. Usually you would use lists for stuff like this in Oz. |
We use an array because that made most sense for the Knuth Shuffle task. Usually you would use lists for stuff like this in Oz. |
||
< |
<syntaxhighlight lang="oz">declare |
||
proc {BogoSort Arr} |
proc {BogoSort Arr} |
||
for while:{Not {InOrder Arr}} do |
for while:{Not {InOrder Arr}} do |
||
Line 2,294: | Line 2,294: | ||
in |
in |
||
{BogoSort X} |
{BogoSort X} |
||
{Show {Array.toRecord unit X}}</ |
{Show {Array.toRecord unit X}}</syntaxhighlight> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
This implementation sorts 9 distinct elements in only 600 milliseconds. |
This implementation sorts 9 distinct elements in only 600 milliseconds. |
||
< |
<syntaxhighlight lang="parigp">bogosort(v)={ |
||
while(1, |
while(1, |
||
my(u=vecextract(v,numtoperm(#v,random((#v)!)))); |
my(u=vecextract(v,numtoperm(#v,random((#v)!)))); |
||
Line 2,304: | Line 2,304: | ||
return(u) |
return(u) |
||
); |
); |
||
};</ |
};</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
< |
<syntaxhighlight lang="pascal">program bogosort; |
||
const |
const |
||
Line 2,372: | Line 2,372: | ||
a[i] := (max + 1) - i; |
a[i] := (max + 1) - i; |
||
bogo(a); |
bogo(a); |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,382: | Line 2,382: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">use List::Util qw(shuffle); |
||
sub bogosort |
sub bogosort |
||
Line 2,394: | Line 2,394: | ||
{$_ >= $last or return 0; |
{$_ >= $last or return 0; |
||
$last = $_;} |
$last = $_;} |
||
return 1;}</ |
return 1;}</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
Line 2,413: | Line 2,413: | ||
<span style="color: #0000FF;">?</span> <span style="color: #000000;">bogosort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">}))</span> |
<span style="color: #0000FF;">?</span> <span style="color: #000000;">bogosort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">}))</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,424: | Line 2,424: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php">function bogosort($l) { |
||
while (!in_order($l)) |
while (!in_order($l)) |
||
shuffle($l); |
shuffle($l); |
||
Line 2,435: | Line 2,435: | ||
return FALSE; |
return FALSE; |
||
return TRUE; |
return TRUE; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de bogosort (Lst) |
||
(loop |
(loop |
||
(map |
(map |
||
'((L) (rot L (rand 1 (length L)))) |
'((L) (rot L (rand 1 (length L)))) |
||
Lst ) |
Lst ) |
||
(T (apply <= Lst) Lst) ) )</ |
(T (apply <= Lst) Lst) ) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>: (bogosort (make (do 9 (link (rand 1 999))))) |
<pre>: (bogosort (make (do 9 (link (rand 1 999))))) |
||
Line 2,456: | Line 2,456: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
{{trans|REXX}} |
{{trans|REXX}} |
||
< |
<syntaxhighlight lang="pli">*process source xref; |
||
bogosort: Proc Options(main); |
bogosort: Proc Options(main); |
||
Dcl SYSPRINT Print; |
Dcl SYSPRINT Print; |
||
Line 2,509: | Line 2,509: | ||
Return(res); |
Return(res); |
||
End; |
End; |
||
End;</ |
End;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>un-bogoed |
<pre>un-bogoed |
||
Line 2,527: | Line 2,527: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
Shuffle taken from [[Knuth Shuffle]] |
Shuffle taken from [[Knuth Shuffle]] |
||
< |
<syntaxhighlight lang="powershell">function shuffle ($a) { |
||
$c = $a.Clone() # make copy to avoid clobbering $a |
$c = $a.Clone() # make copy to avoid clobbering $a |
||
1..($c.Length - 1) | ForEach-Object { |
1..($c.Length - 1) | ForEach-Object { |
||
Line 2,555: | Line 2,555: | ||
} |
} |
||
$l = 7; BogoSort ( 1..$l | ForEach-Object { $Rand = New-Object Random }{ $Rand.Next( 0, $l - 1 ) } )</ |
$l = 7; BogoSort ( 1..$l | ForEach-Object { $Rand = New-Object Random }{ $Rand.Next( 0, $l - 1 ) } )</syntaxhighlight> |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
< |
<syntaxhighlight lang="prolog">bogo_sort(L,Rl) :- |
||
min_list(L,Min), |
min_list(L,Min), |
||
repeat, |
repeat, |
||
Line 2,568: | Line 2,568: | ||
is_sorted([N|T],P) :- |
is_sorted([N|T],P) :- |
||
N >= P, |
N >= P, |
||
is_sorted(T,N).</ |
is_sorted(T,N).</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,576: | Line 2,576: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">Procedure KnuthShuffle (Array a(1)) |
||
Protected i, Size = ArraySize(a()) |
Protected i, Size = ArraySize(a()) |
||
For i = 0 To Size |
For i = 0 To Size |
||
Line 2,608: | Line 2,608: | ||
Next |
Next |
||
BogoSort(b())</ |
BogoSort(b())</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Array of 10 integers required 2766901 shuffles To SORT.</pre> |
<pre>Array of 10 integers required 2766901 shuffles To SORT.</pre> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">import random |
||
def bogosort(l): |
def bogosort(l): |
||
Line 2,628: | Line 2,628: | ||
return False |
return False |
||
last = x |
last = x |
||
return True</ |
return True</syntaxhighlight> |
||
Alternative definition for ''in_order'' (Python 2.5) |
Alternative definition for ''in_order'' (Python 2.5) |
||
< |
<syntaxhighlight lang="python">def in_order(l): |
||
return all( l[i] <= l[i+1] for i in xrange(0,len(l)-1))</ |
return all( l[i] <= l[i+1] for i in xrange(0,len(l)-1))</syntaxhighlight> |
||
An alternative implementation for Python 2.5 or later: |
An alternative implementation for Python 2.5 or later: |
||
< |
<syntaxhighlight lang="python">import random |
||
def bogosort(lst): |
def bogosort(lst): |
||
random.shuffle(lst) # must shuffle it first or it's a bug if lst was pre-sorted! :) |
random.shuffle(lst) # must shuffle it first or it's a bug if lst was pre-sorted! :) |
||
while lst != sorted(lst): |
while lst != sorted(lst): |
||
random.shuffle(lst) |
random.shuffle(lst) |
||
return lst</ |
return lst</syntaxhighlight> |
||
Another alternative implementation, using iterators for maximum efficiency: |
Another alternative implementation, using iterators for maximum efficiency: |
||
< |
<syntaxhighlight lang="python">import operator |
||
import random |
import random |
||
from itertools import dropwhile, imap, islice, izip, repeat, starmap |
from itertools import dropwhile, imap, islice, izip, repeat, starmap |
||
Line 2,655: | Line 2,655: | ||
bogosort = lambda l: next(dropwhile( |
bogosort = lambda l: next(dropwhile( |
||
lambda l: not all(starmap(operator.le, izip(l, islice(l, 1, None)))), |
lambda l: not all(starmap(operator.le, izip(l, islice(l, 1, None)))), |
||
imap(shuffled, repeat(l))))</ |
imap(shuffled, repeat(l))))</syntaxhighlight> |
||
=={{header|Qi}}== |
=={{header|Qi}}== |
||
<syntaxhighlight lang="qi"> |
|||
<lang Qi> |
|||
(define remove-element |
(define remove-element |
||
0 [_ | R] -> R |
0 [_ | R] -> R |
||
Line 2,682: | Line 2,682: | ||
Suggestion -> Suggestion where (in-order? Suggestion) |
Suggestion -> Suggestion where (in-order? Suggestion) |
||
Suggestion -> (bogosort (shuffle Suggestion))) |
Suggestion -> (bogosort (shuffle Suggestion))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery">[ true swap |
||
dup [] != if |
dup [] != if |
||
[ behead swap witheach |
[ behead swap witheach |
||
Line 2,693: | Line 2,693: | ||
drop ] is inorder ( [ --> b ) |
drop ] is inorder ( [ --> b ) |
||
[ dup inorder not while shuffle again ] is bogosort ( [ --> [ )</ |
[ dup inorder not while shuffle again ] is bogosort ( [ --> [ )</syntaxhighlight> |
||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="r">bogosort <- function(x) { |
||
while(is.unsorted(x)) x <- sample(x) |
while(is.unsorted(x)) x <- sample(x) |
||
x |
x |
||
Line 2,702: | Line 2,702: | ||
n <- c(1, 10, 9, 7, 3, 0) |
n <- c(1, 10, 9, 7, 3, 0) |
||
bogosort(n)</ |
bogosort(n)</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
Line 2,709: | Line 2,709: | ||
is unit tests and an example. |
is unit tests and an example. |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
(define (bogo-sort l) (if (apply <= l) l (bogo-sort (shuffle l)))) |
(define (bogo-sort l) (if (apply <= l) l (bogo-sort (shuffle l)))) |
||
Line 2,720: | Line 2,720: | ||
(displayln unsorted) |
(displayln unsorted) |
||
(displayln (bogo-sort unsorted))) |
(displayln (bogo-sort unsorted))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} (chances are you won't get quite this!): |
{{out}} (chances are you won't get quite this!): |
||
Line 2,730: | Line 2,730: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
<lang |
<syntaxhighlight lang="raku" line>sub bogosort (@list is copy) { |
||
@list .= pick(*) until [<=] @list; |
@list .= pick(*) until [<=] @list; |
||
return @list; |
return @list; |
||
Line 2,737: | Line 2,737: | ||
my @nums = (^5).map: { rand }; |
my @nums = (^5).map: { rand }; |
||
say @nums.sort.Str eq @nums.&bogosort.Str ?? 'ok' !! 'not ok'; |
say @nums.sort.Str eq @nums.&bogosort.Str ?? 'ok' !! 'not ok'; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===true bogo sort=== |
===true bogo sort=== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program performs a type of bogo sort on numbers in an array. */ |
||
parse arg list /*obtain optional list from C.L. */ |
parse arg list /*obtain optional list from C.L. */ |
||
if list='' then list=-21 333 0 444.4 /*Not defined? Then use default.*/ |
if list='' then list=-21 333 0 444.4 /*Not defined? Then use default.*/ |
||
Line 2,771: | Line 2,771: | ||
end /*t*/ |
end /*t*/ |
||
say |
say |
||
return</ |
return</syntaxhighlight> |
||
{{out}} using the default input: |
{{out}} using the default input: |
||
<pre> |
<pre> |
||
Line 2,794: | Line 2,794: | ||
<br>has already been sorted and including the number out-of-order. The search then starts over. |
<br>has already been sorted and including the number out-of-order. The search then starts over. |
||
<br>This is repeated as often as it takes to finally get the array in order. |
<br>This is repeated as often as it takes to finally get the array in order. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program performs a type of bogo sort on numbers in an array. */ |
||
@.1 = 0 ; @.11= -64 ; @.21= 4096 ; @.31= 6291456 |
@.1 = 0 ; @.11= -64 ; @.21= 4096 ; @.31= 6291456 |
||
@.2 = 0 ; @.12= 64 ; @.22= 40960 ; @.32= 5242880 |
@.2 = 0 ; @.12= 64 ; @.22= 40960 ; @.32= 5242880 |
||
Line 2,835: | Line 2,835: | ||
end /*t*/ |
end /*t*/ |
||
say |
say |
||
return</ |
return</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:30ex"> |
<pre style="height:30ex"> |
||
Line 2,946: | Line 2,946: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
# Project : Sorting algorithms/Bogosort |
# Project : Sorting algorithms/Bogosort |
||
Line 2,987: | Line 2,987: | ||
see svect |
see svect |
||
see "]" + nl |
see "]" + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 2,995: | Line 2,995: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">def shuffle(l) |
||
l.sort_by { rand } |
l.sort_by { rand } |
||
end |
end |
||
Line 3,006: | Line 3,006: | ||
def in_order(l) |
def in_order(l) |
||
(0..l.length-2).all? {|i| l[i] <= l[i+1] } |
(0..l.length-2).all? {|i| l[i] <= l[i+1] } |
||
end</ |
end</syntaxhighlight> |
||
An alternative implementation: |
An alternative implementation: |
||
< |
<syntaxhighlight lang="ruby">def shuffle(l) |
||
l.sort_by { rand } |
l.sort_by { rand } |
||
end |
end |
||
Line 3,017: | Line 3,017: | ||
l = shuffle(l) until l == l.sort |
l = shuffle(l) until l == l.sort |
||
l |
l |
||
end</ |
end</syntaxhighlight> |
||
{{works with|Ruby|1.8.7+}} |
{{works with|Ruby|1.8.7+}} |
||
< |
<syntaxhighlight lang="ruby">def in_order(l) |
||
(0..l.length-2).all? {|i| l[i] <= l[i+1] } |
(0..l.length-2).all? {|i| l[i] <= l[i+1] } |
||
end |
end |
||
Line 3,028: | Line 3,028: | ||
l.shuffle! until in_order(l) |
l.shuffle! until in_order(l) |
||
l |
l |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Works with Rust 1.11+, requires rand module |
Works with Rust 1.11+, requires rand module |
||
{{libheader|rand}} |
{{libheader|rand}} |
||
< |
<syntaxhighlight lang="rust">extern crate rand; |
||
use rand::Rng; |
use rand::Rng; |
||
Line 3,060: | Line 3,060: | ||
println!("{:?}", testlist); |
println!("{:?}", testlist); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{works with|Scala|2.8}} |
{{works with|Scala|2.8}} |
||
< |
<syntaxhighlight lang="scala">def isSorted(l: List[Int]) = l.iterator sliding 2 forall (s => s.head <= s.last) |
||
def bogosort(l: List[Int]): List[Int] = if (isSorted(l)) l else bogosort(scala.util.Random.shuffle(l))</ |
def bogosort(l: List[Int]): List[Int] = if (isSorted(l)) l else bogosort(scala.util.Random.shuffle(l))</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">func in_order(a) { |
||
return true if (a.len <= 1); |
return true if (a.len <= 1); |
||
var first = a[0]; |
var first = a[0]; |
||
Line 3,081: | Line 3,081: | ||
var arr = 5.of{ 100.rand.int }; |
var arr = 5.of{ 100.rand.int }; |
||
say "Before: #{arr}"; |
say "Before: #{arr}"; |
||
say "After: #{bogosort(arr)}";</ |
say "After: #{bogosort(arr)}";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,090: | Line 3,090: | ||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
Uses [[Knuth shuffle]] to shuffle the list. |
Uses [[Knuth shuffle]] to shuffle the list. |
||
< |
<syntaxhighlight lang="scheme">(import (rnrs base (6)) |
||
(srfi :27 random-bits)) |
(srfi :27 random-bits)) |
||
Line 3,123: | Line 3,123: | ||
(display "Output: ") |
(display "Output: ") |
||
(display (bogosort input)) |
(display (bogosort input)) |
||
(newline))</ |
(newline))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Input: (5 4 3 2 1) |
<pre>Input: (5 4 3 2 1) |
||
Line 3,132: | Line 3,132: | ||
This implementation uses closures rather than extending collections to provide a bogosort method. |
This implementation uses closures rather than extending collections to provide a bogosort method. |
||
< |
<syntaxhighlight lang="smalltalk">Smalltalk at: #isItSorted put: [ :c | |
||
|isit| |
|isit| |
||
isit := false. |
isit := false. |
||
Line 3,155: | Line 3,155: | ||
tobesorted := { 2 . 7 . 5 . 3 . 4 . 8 . 6 . 1 }. |
tobesorted := { 2 . 7 . 5 . 3 . 4 . 8 . 6 . 1 }. |
||
bogosort value: tobesorted. |
bogosort value: tobesorted. |
||
tobesorted displayNl.</ |
tobesorted displayNl.</syntaxhighlight> |
||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
< |
<syntaxhighlight lang="snobol4">* Library for random() |
||
-include 'Random.sno' |
-include 'Random.sno' |
||
Line 3,199: | Line 3,199: | ||
* # Test and display |
* # Test and display |
||
bogo(s2a('5 4 3 2 1',5)) |
bogo(s2a('5 4 3 2 1',5)) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,209: | Line 3,209: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">import Darwin |
||
func shuffle<T>(inout array: [T]) { |
func shuffle<T>(inout array: [T]) { |
||
Line 3,231: | Line 3,231: | ||
shuffle(&ary) |
shuffle(&ary) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.5 |
||
proc shuffleInPlace {listName} { |
proc shuffleInPlace {listName} { |
||
Line 3,263: | Line 3,263: | ||
} |
} |
||
return $list |
return $list |
||
}</ |
}</syntaxhighlight> |
||
=={{header|TI-83 BASIC}}== |
=={{header|TI-83 BASIC}}== |
||
Line 3,314: | Line 3,314: | ||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
< |
<syntaxhighlight lang="ursala">#import std |
||
#import nat |
#import nat |
||
Line 3,323: | Line 3,323: | ||
#cast %nL |
#cast %nL |
||
example = bogosort <8,50,0,12,47,51></ |
example = bogosort <8,50,0,12,47,51></syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre><0,8,12,47,50,51></pre> |
<pre><0,8,12,47,50,51></pre> |
||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
{{trans|Phix}}< |
{{trans|Phix}}<syntaxhighlight lang="vb">Private Function Knuth(a As Variant) As Variant |
||
Dim t As Variant, i As Integer |
Dim t As Variant, i As Integer |
||
If Not IsMissing(a) Then |
If Not IsMissing(a) Then |
||
Line 3,363: | Line 3,363: | ||
Public Sub main() |
Public Sub main() |
||
Debug.Print Join(bogosort(Knuth([{1,2,3,4,5,6}])), ", ") |
Debug.Print Join(bogosort(Knuth([{1,2,3,4,5,6}])), ", ") |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
<pre>... |
<pre>... |
||
1, 3, 2, 5, 6, 4 |
1, 3, 2, 5, 6, 4 |
||
Line 3,373: | Line 3,373: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
=====Implementation===== |
=====Implementation===== |
||
< |
<syntaxhighlight lang="vb">sub swap( byref a, byref b ) |
||
dim tmp |
dim tmp |
||
tmp = a |
tmp = a |
||
Line 3,402: | Line 3,402: | ||
next |
next |
||
inOrder = res |
inOrder = res |
||
end function</ |
end function</syntaxhighlight> |
||
=====Invocation===== |
=====Invocation===== |
||
< |
<syntaxhighlight lang="vb">dim a |
||
a = array(11, 1, 2, 3, 4, 4, 6, 7, 8) |
a = array(11, 1, 2, 3, 4, 4, 6, 7, 8) |
||
Line 3,414: | Line 3,414: | ||
wend |
wend |
||
wscript.echo timer-t, "seconds" |
wscript.echo timer-t, "seconds" |
||
wscript.echo join( a, ", " )</ |
wscript.echo join( a, ", " )</syntaxhighlight> |
||
=====A few outputs (timed)===== |
=====A few outputs (timed)===== |
||
Line 3,430: | Line 3,430: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
Updated for Vlang version 0.2.2 |
Updated for Vlang version 0.2.2 |
||
< |
<syntaxhighlight lang="go">import rand |
||
fn shuffle_array(mut arr []int) { |
fn shuffle_array(mut arr []int) { |
||
Line 3,460: | Line 3,460: | ||
sort_array(mut array) |
sort_array(mut array) |
||
println('Output: $array') |
println('Output: $array') |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Input: [6, 9, 1, 4] |
<pre>Input: [6, 9, 1, 4] |
||
Line 3,473: | Line 3,473: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-sort}} |
{{libheader|Wren-sort}} |
||
< |
<syntaxhighlight lang="ecmascript">import "random" for Random |
||
import "/sort" for Sort |
import "/sort" for Sort |
||
Line 3,484: | Line 3,484: | ||
System.print("Before: %(a)") |
System.print("Before: %(a)") |
||
bogoSort.call(a) |
bogoSort.call(a) |
||
System.print("After : %(a)")</ |
System.print("After : %(a)")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,493: | Line 3,493: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">code Ran=1, ChOut=8, IntOut=11; |
||
proc BogoSort(A, L); \Sort array A of length L |
proc BogoSort(A, L); \Sort array A of length L |
||
Line 3,512: | Line 3,512: | ||
BogoSort(A, 10); |
BogoSort(A, 10); |
||
for I:= 0 to 10-1 do [IntOut(0, A(I)); ChOut(0, ^ )]; |
for I:= 0 to 10-1 do [IntOut(0, A(I)); ChOut(0, ^ )]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,522: | Line 3,522: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="yabasic"> |
||
sub shuffle(a()) |
sub shuffle(a()) |
||
n = arraysize(a(),1) |
n = arraysize(a(),1) |
||
Line 3,560: | Line 3,560: | ||
next i |
next i |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn increasing(list){ |
||
list.len()<2 or |
list.len()<2 or |
||
list.reduce(fcn(a,b){ if(b<a) return(Void.Stop,False); b }).toBool() |
list.reduce(fcn(a,b){ if(b<a) return(Void.Stop,False); b }).toBool() |
||
Line 3,571: | Line 3,571: | ||
ns:=L(5,23,1,6,123,7,23); |
ns:=L(5,23,1,6,123,7,23); |
||
while(not increasing(ns)){ ns=ns.shuffle() } |
while(not increasing(ns)){ ns=ns.shuffle() } |
||
ns.println();</ |
ns.println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>L(1,5,6,7,23,23,123)</pre> |
<pre>L(1,5,6,7,23,23,123)</pre> |