September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,12 +0,0 @@
|
|||
#include <iterator>
|
||||
#include <algorithm>
|
||||
|
||||
template<typename ForwardIterator>
|
||||
void bogosort(ForwardIterator begin, ForwardIterator end)
|
||||
{
|
||||
typedef std::iterator_traits<ForwardIterator>::value_type value_type;
|
||||
|
||||
// if we find two adjacent values where the first is greater than the second, the sequence isn't sorted.
|
||||
while (std::adjacent_find(begin, end, std::greater<value_type>()) != end)
|
||||
std::random_shuffle(begin, end);
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
#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);
|
||||
}
|
||||
|
|
@ -2,9 +2,10 @@ import System.Random
|
|||
import Data.Array.IO
|
||||
import Control.Monad
|
||||
|
||||
isSorted :: (Ord a) => [a] -> Bool
|
||||
isSorted (e1:e2:r) = e1 <= e2 && isSorted (e2:r)
|
||||
isSorted _ = True
|
||||
isSortedBy :: (a -> a -> Bool) -> [a] -> Bool
|
||||
isSortedBy _ [] = True
|
||||
isSortedBy f xs = all (uncurry f) . (zip <*> tail) $ xs
|
||||
|
||||
|
||||
-- from http://www.haskell.org/haskellwiki/Random_shuffle
|
||||
shuffle :: [a] -> IO [a]
|
||||
|
|
@ -21,6 +22,9 @@ shuffle xs = do
|
|||
newArray :: Int -> [a] -> IO (IOArray Int a)
|
||||
newArray n xs = newListArray (1,n) xs
|
||||
|
||||
bogosort :: (Ord a) => [a] -> IO [a]
|
||||
bogosort li | isSorted li = return li
|
||||
| otherwise = shuffle li >>= bogosort
|
||||
bogosortBy :: (a -> a -> Bool) -> [a] -> IO [a]
|
||||
bogosortBy f xs | isSortedBy f xs = return xs
|
||||
| otherwise = shuffle xs >>= bogosortBy f
|
||||
|
||||
bogosort :: Ord a => [a] -> IO [a]
|
||||
bogosort = bogosortBy (<)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
// version 1.1.2
|
||||
|
||||
const val RAND_MAX = 32768 // big enough for this
|
||||
|
||||
val rand = java.util.Random()
|
||||
|
||||
fun isSorted(a: IntArray): Boolean {
|
||||
val n = a.size
|
||||
if (n < 2) return true
|
||||
for (i in 1 until n) {
|
||||
if (a[i] < a[i - 1]) return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
fun shuffle(a: IntArray) {
|
||||
val n = a.size
|
||||
if (n < 2) return
|
||||
for (i in 0 until n) {
|
||||
val t = a[i]
|
||||
val r = rand.nextInt(RAND_MAX) % n
|
||||
a[i] = a[r]
|
||||
a[r] = t
|
||||
}
|
||||
}
|
||||
|
||||
fun bogosort(a: IntArray) {
|
||||
while (!isSorted(a)) shuffle(a)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val a = intArrayOf(1, 10, 9, 7, 3, 0)
|
||||
println("Before sorting : ${a.contentToString()}")
|
||||
bogosort(a)
|
||||
println("After sorting : ${a.contentToString()}")
|
||||
}
|
||||
|
|
@ -1,24 +1,27 @@
|
|||
extern crate rand;
|
||||
use rand::Rng;
|
||||
|
||||
use rand::{thread_rng, Rng};
|
||||
use std::vec::Vec;
|
||||
|
||||
fn sorted(curlist: &Vec<u64>) -> bool {
|
||||
let mut sortedlist = curlist.clone();
|
||||
sortedlist.sort();
|
||||
curlist.iter().eq(sortedlist.iter())
|
||||
}
|
||||
|
||||
fn sort(curlist: &Vec<u64>) -> Vec<u64> {
|
||||
let mut result = curlist.clone();
|
||||
while !sorted(&result) {
|
||||
let mut rng = thread_rng();
|
||||
rng.shuffle(&mut result);
|
||||
fn bogosort_by<T,F>(order: F, coll: &mut [T])
|
||||
where F: Fn(&T, &T) -> bool
|
||||
{
|
||||
let mut rng = rand::thread_rng();
|
||||
while !is_sorted(&order, coll) {
|
||||
rng.shuffle(coll);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_sorted_by<T,F>(order: F, coll: &[T]) -> bool
|
||||
where F: Fn(&T,&T) -> bool,
|
||||
{
|
||||
coll[..].iter().zip(&coll[1..]).all(|(x,y)| order(x,y))
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let mut testlist = vec![1,55,88,24,990876,312,67,0,854,13,4,7];
|
||||
println!("{:?}", sort(&mut testlist))
|
||||
let mut testlist = [1,55,88,24,990876,312,67,0,854,13,4,7];
|
||||
bogosort_by(|x,y| x < y, &mut testlist);
|
||||
println!("{:?}", testlist)
|
||||
bogosort_by(|x,y| x > y, &mut testlist);
|
||||
println!("{:?}", testlist);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,8 @@
|
|||
fcn increasing(list){
|
||||
list.len()<2 or
|
||||
list.reduce(fcn(a,b){ if(b<a) return(Void.Stop,False); b }).toBool()
|
||||
}
|
||||
|
||||
ns:=L(5,23,1,6,123,7,23);
|
||||
while(not increasing(ns)){ ns=ns.shuffle() }
|
||||
ns.println();
|
||||
Loading…
Add table
Add a link
Reference in a new issue