Sorting algorithms/Bogosort: Difference between revisions

no edit summary
No edit summary
Line 7:
 
=={{header|ActionScript}}==
<lang actionscript>
public function bogoSort(arr:Array):Array
{
Line 45:
return true;
}
</lang>
</actionscript>
 
=={{header|Ada}}==
<lang ada>
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Discrete_Random;
Line 99:
end loop;
end Test_Bogosort;
</adalang>
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>
Line 106:
=={{header|C++}}==
The following algorithm actually works for all sequences of comparable types; restricting to lists of integers would not make the code simpler.
<lang cpp>#include <iterator>
#include <algorithm>
 
Line 117:
while (std::adjacent_find(begin, end, std::greater<value_type>()) != end)
std::random_shuffle(begin, end);
}</cpplang>
Using the is_sorted function, part of the SGI STL implementation:
 
{{works with|GCC}}
<lang cpp>#include <algorithm>
#include <ext/algorithm>
 
Line 129:
while (!__gnu_cxx::is_sorted(begin, end))
std::random_shuffle(begin, end);
}</cpplang>
 
=={{header|C sharp|C#}}==
{{works with|C sharp|C#|3.0+}}
<lang csharp>
using System;
using System.Collections.Generic;
Line 182:
}
}
</csharplang>
 
=={{header|D}}==
<lang d>module bogosort ;
import std.stdio, std.random ;
 
Line 209:
writefln("%s", b) ; // sort is in place
}</dlang>
 
=={{header|Fortran}}==
Line 327:
{{works with|Java|1.5+}}
This implementation works for all comparable types (types with <tt>compareTo</tt> defined).
<lang java>import java.util.Collections;
import java.util.List;
import java.util.Iterator;
Line 350:
Collections.shuffle(list);
}
}</javalang>
 
=={{header|Modula-3}}==
 
<codelang modula3>MODULE Bogo EXPORTS Main;
 
IMPORT IO, Random;
Line 397:
END;
IO.PutChar('\n');
END Bogo.</codelang>
 
=={{header|Oberon-2}}==
Line 457:
=={{header|OCaml}}==
 
<lang ocaml>
let rec is_sorted comp = function
| e1 :: e2 :: r -> comp e1 e2 <= 0 && is_sorted comp (e2 :: r)
Line 478:
else
bogosort (shuffle li)
</ocamllang>
Example:
<pre>
Line 486:
 
=={{header|Perl}}==
<lang perl>sub bogosort
{my @l = @_;
shuffle(\@l) until in_order(@l);
Line 505:
for (my $n = $#l ; $n ; --$n)
{my $k = int rand($n + 1);
@l[$k, $n] = @l[$n, $k] if $k != $n;}}</perllang>
 
=={{header|PHP}}==
<lang php>function bogosort($l) {
while (!in_order($l))
shuffle($l);
Line 519:
return FALSE;
return TRUE;
}</phplang>
 
=={{header|Python}}==
<lang python>import random
 
def bogosort(l):
Line 537:
return False
last = x
return True</pythonlang>
 
Alternative definition for ''in_order'' (Python 2.5)
<lang python>def in_order(l):
return all( l[i] <= l[i+1] for i in xrange(0,len(l)-1))</pythonlang>
 
An alternative implementation for Python 2.5 or later:
 
<lang python>import random
def bogosort(lst):
random.shuffle(lst) # must shuffle it first or it's a bug if lst was pre-sorted! :)
while lst != sorted(lst):
random.shuffle(lst)
return lst</pythonlang>
 
=={{header|Ruby}}==
<lang ruby>def shuffle(l)
l.sort_by { rand }
end
Line 564:
def in_order(l)
(0..l.length-2).all? {|i| l[i] <= l[i+1] }
end</rubylang>
 
An alternative implementation:
 
<lang ruby>def shuffle(l)
l.sort_by { rand }
end
Line 575:
l = shuffle(l) until l == l.sort
l
end</rubylang>