Sorting algorithms/Bogosort: Difference between revisions
Content added Content deleted
m (→{{header|AutoHotkey}}: Minor indentation and casing edit) |
m (formatting) |
||
Line 4: | Line 4: | ||
Pseudocode: |
Pseudocode: |
||
while not InOrder(list) do |
'''while not''' InOrder(list) '''do''' |
||
Shuffle(list) |
|||
'''done''' |
|||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
<lang actionscript> |
<lang actionscript>public function bogoSort(arr:Array):Array |
||
public function bogoSort(arr:Array):Array |
|||
{ |
{ |
||
while (!sorted(arr)) |
while (!sorted(arr)) |
||
Line 44: | Line 45: | ||
return true; |
return true; |
||
⚫ | |||
} |
|||
⚫ | |||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<lang ada> |
<lang ada>with Ada.Text_IO; use Ada.Text_IO; |
||
with Ada.Text_IO; use Ada.Text_IO; |
|||
with Ada.Numerics.Discrete_Random; |
with Ada.Numerics.Discrete_Random; |
||
Line 98: | Line 97: | ||
Put (Integer'Image (Sequence (I))); |
Put (Integer'Image (Sequence (I))); |
||
end loop; |
end loop; |
||
end Test_Bogosort; |
end Test_Bogosort;</lang> |
||
⚫ | |||
The solution is generic. The procedure Bogosort can be instantiated with any copyable comparable type. In the example given it is the standard Integer type. Sample output: |
The solution is generic. The procedure Bogosort can be instantiated with any copyable comparable type. In the example given it is the standard Integer type. Sample output: |
||
<pre> |
<pre> |
||
Line 149: | Line 147: | ||
[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))</lang> |
||
</lang> |
|||
Output: |
Output: |
||
+18 +46 +52 +61 +63 +94 |
+18 +46 +52 +61 +63 +94 |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
<lang AutoHotkey> |
<lang AutoHotkey>MsgBox % Bogosort("987654") |
||
MsgBox % Bogosort("987654") |
|||
MsgBox % Bogosort("319208") |
MsgBox % Bogosort("319208") |
||
MsgBox % Bogosort("fedcba") |
MsgBox % Bogosort("fedcba") |
||
Line 292: | Line 288: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{works with|C sharp|C#|3.0+}} |
{{works with|C sharp|C#|3.0+}} |
||
<lang csharp> |
<lang csharp>using System; |
||
using System; |
|||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
Line 340: | Line 335: | ||
} |
} |
||
⚫ | |||
} |
|||
</lang> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
Line 392: | Line 386: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
<lang factor> |
<lang factor>USING: grouping kernel math random sequences ; |
||
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 ;</lang> |
||
</lang> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Line 474: | Line 466: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
<lang haskell>import System.Random |
<lang haskell>import System.Random |
||
import Data.Array.IO |
import Data.Array.IO |
||
Line 536: | Line 527: | ||
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). |
||
<lang |
<lang java5>import java.util.Collections; |
||
import java.util.List; |
import java.util.List; |
||
import java.util.Iterator; |
import java.util.Iterator; |
||
Line 562: | Line 553: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
<lang javascript> |
<lang javascript>shuffle = function(v) { |
||
shuffle = function(v){ |
|||
for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x); |
for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x); |
||
return v; |
return v; |
||
Line 583: | Line 572: | ||
} |
} |
||
return v; |
return v; |
||
} |
} </lang> |
||
</lang> |
|||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
Line 623: | Line 610: | ||
) |
) |
||
arr |
arr |
||
⚫ | |||
) |
|||
⚫ | |||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
Line 730: | Line 716: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
⚫ | |||
<lang ocaml> |
|||
⚫ | |||
| 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 751: | Line 735: | ||
li |
li |
||
else |
else |
||
bogosort (shuffle li) |
bogosort (shuffle li)</lang> |
||
</lang> |
|||
Example: |
Example: |
||
<pre> |
<pre> |
||
Line 818: | Line 801: | ||
An alternative implementation for Python 2.5 or later: |
An alternative implementation for Python 2.5 or later: |
||
<lang python>import random |
<lang python>import random |
||
def bogosort(lst): |
def bogosort(lst): |