Sorting algorithms/Bogosort: Difference between revisions

Content added Content deleted
(remove sections moved to Permutation Sort)
Line 6: Line 6:
=={{header|C++}}==
=={{header|C++}}==
The following algorithm actually works for all sequences of comparable types; restricting to lists of integers would not make the code simpler.
The following algorithm actually works for all sequences of comparable types; restricting to lists of integers would not make the code simpler.
<cpp>
<cpp>#include <iterator>
#include <iterator>
#include <algorithm>
#include <algorithm>


Line 18: Line 17:
while (std::adjacent_find(begin, end, std::greater<value_type>()) != end)
while (std::adjacent_find(begin, end, std::greater<value_type>()) != end)
std::random_shuffle(begin, end);
std::random_shuffle(begin, end);
}</cpp>
}
Using the is_sorted function, part of the SGI STL implementation:
</cpp>

{{works with|GCC}}
<cpp>#include <algorithm>
#include <ext/algorithm>

template<typename ForwardIterator>
void bogosort(ForwardIterator begin, ForwardIterator end)
{
while (!__gnu_cxx::is_sorted(begin, end))
std::random_shuffle(begin, end);
}</cpp>


=={{header|J}}==
=={{header|J}}==