Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,7 +1,9 @@
{{Sorting Algorithm}}
[[wp:Bogosort|Bogosort]] a list of numbers. Bogosort simply shuffles a collection randomly until it is sorted.
[[wp:Bogosort|Bogosort]] a list of numbers.
Bogosort simply shuffles a collection randomly until it is sorted.
"Bogosort" is a perversely inefficient algorithm only used as an in-joke. Its average run-time is O(n!) because the chance that any given shuffle of a set will end up in sorted order is about one in ''n'' factorial, and the worst case is infinite since there's no guarantee that a random shuffling will ever produce a sorted sequence. Its best case is O(n) since a single pass through the elements may suffice to order them.
"Bogosort" is a perversely inefficient algorithm only used as an in-joke.
Its average run-time is O(n!) because the chance that any given shuffle of a set will end up in sorted order is about one in ''n'' factorial, and the worst case is infinite since there's no guarantee that a random shuffling will ever produce a sorted sequence. Its best case is O(n) since a single pass through the elements may suffice to order them.
Pseudocode:
'''while not''' InOrder(list) '''do'''

View file

@ -0,0 +1,29 @@
#include <algorithm>
#include <iostream>
#include <iterator>
#include <random>
template <typename RandomAccessIterator, typename Predicate>
void bogo_sort(RandomAccessIterator begin, RandomAccessIterator end,
Predicate p) {
std::random_device rd;
std::mt19937 generator(rd());
while (!std::is_sorted(begin, end, p)) {
std::shuffle(begin, end, generator);
}
}
template <typename RandomAccessIterator>
void bogo_sort(RandomAccessIterator begin, RandomAccessIterator end) {
bogo_sort(
begin, end,
std::less<
typename std::iterator_traits<RandomAccessIterator>::value_type>());
}
int main() {
int a[] = {100, 2, 56, 200, -52, 3, 99, 33, 177, -199};
bogo_sort(std::begin(a), std::end(a));
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
}

View file

@ -1,17 +1,9 @@
(ns bogosort
(:use [clojure.contrib.seq-utils :only (shuffle)]))
(defn in-order? [less xs]
(defn in-order? [order xs]
(or (empty? xs)
(empty? (next xs))
(and (less (first xs) (second xs))
(recur less (next xs)))))
(apply order xs)))
(defn bogosort
([xs]
(bogosort < xs))
([less xs]
(if (in-order? less xs) xs
(recur less (shuffle xs)))))
(defn bogosort [order xs]
(if (in-order? order xs) 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]))

View file

@ -0,0 +1,58 @@
class
BOGO_SORT
feature
bogo_sort(ar: ARRAY[INTEGER]): ARRAY[INTEGER]
do
from
until
is_sorted (ar) = TRUE
loop
Result:= shuffel(ar)
end
end
feature{NONE}
is_sorted (ar:ARRAY[INTEGER]): BOOLEAN
require
not_void: ar /= Void
local
i: INTEGER
do
Result := True
from
i := 1+ 1
invariant
i >= 1 + 1 and i <= ar.count + 1
until
i > ar.count
loop
Result := Result and ar [i - 1] <= ar [i]
i := i + 1
variant
ar.count + 1 - i
end
end
shuffel(ar:ARRAY[INTEGER]): ARRAY[INTEGER]
require
not_void: ar/= Void
local
i,j:INTEGER
ith: INTEGER
random: V_RANDOM
do
create random
from
i:=ar.count
until
i<2
loop
j:=random.bounded_item (1, i)
ith:= ar[i]
ar[i]:= ar[j]
ar[j]:= ith
random.forth
i:=i-1
end
Result:= ar
end
end

View file

@ -0,0 +1,24 @@
class
APPLICATION
inherit
ARGUMENTS
create
make
feature {NONE} -- Initialization
make
do
test:= <<3,2,5,7,1>>
io.put_string ("Unsorted: ")
across test as t loop io.put_string (t.item.out + " ") end
create sorter
test:= sorter.bogo_sort (test)
io.put_string ("%NSorted: ")
across test as t loop io.put_string (t.item.out + " ") end
end
test: ARRAY[INTEGER]
sorter: BOGO_SORT
end

View file

@ -0,0 +1,54 @@
*process source xref;
bogosort: Proc Options(main);
Dcl SYSPRINT Print;
Dcl (HBOUND,RANDOM,TIME) Builtin;
Dcl tim Pic'(9)9';
Dcl timms Pic'(3)9' def tim pos(7);
tim=time();
x=random(timms);
Dcl a(5) Dec Fixed(5,1) Init(-21,333,0,444.4,1);
Dcl (x,y,temp) Dec Fixed(5,1);
Dcl (n,bogo,j,u,v) Bin Fixed(31);
n=hbound(a);
Call tell('un-bogoed');
loop:
Do bogo=1 By 1;
Do j=1 To n-1;
jp=j+1;
x=a(j);
y=a(jp);
if y>=x Then
Iterate;
u=rand(1,n);
Do Until v^=u
v=rand(1,n);
End;
Temp=a(u);
a(u)=a(v);
a(v)=temp;
Iterate loop;
End;
Leave;
End;
Put Edit('number of bogo sorts performed =',bogo)(Skip,a,f(4));
call tell(' bogoed');
Return;
tell: Proc(txt);
Dcl txt Char(*);
Dcl t Bin Fixed(31);
Put Edit(txt)(skip,a);
Do t=1 to n;
Put Edit(a(t))(Skip,f(6,1));
End;
End;
rand: Proc(lo,hi) Returns(Bin Fixed(31));
Dcl (lo,hi,res) Bin Fixed(31);
Dcl r Bin Float(31);
r=random();
res=r*(hi-lo+1)+lo;
Return(res);
End;
End;

View file

@ -0,0 +1,12 @@
import operator
import random
from itertools import dropwhile, imap, islice, izip, repeat, starmap
def shuffled(x):
x = x[:]
random.shuffle(x)
return x
bogosort = lambda l: next(dropwhile(
lambda l: not all(starmap(operator.le, izip(l, islice(l, 1, None)))),
imap(shuffled, repeat(l))))

View file

@ -0,0 +1,31 @@
/*REXX program performs a type of bogo sort on numbers in an array. */
parse arg list /*obtain optional list from C.L. */
if list='' then list=-21 333 0 444.4 /*Not defined? Then use default.*/
#=words(list) /*the number of numbers in list. */
do i=1 for words(list); @.i=word(list,i); end /*create an array.*/
call tell 'before bogo sort'
do bogo=1
do j=1 for #-1; jp=j+1 /* [↓] compare a # with the next*/
if @.jp>=@.j then iterate /*so far, so good; keep looking.*/
/*get 2 unique random #s for swap*/
do until a\==b; a=random(1, #); b=random(1, #); end
parse value @.a @.b with @.b @.a /*swap 2 random numbers in array.*/
iterate bogo /*go and try another bogo sort. */
end /*j*/
leave /*we're finished with bogo sort. */
end /*bogo*/ /* [↓] show the # of bogo sorts.*/
say 'number of bogo sorts performed =' bogo
call tell ' after bogo sort'
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────TELL subroutine─────────────────────*/
tell: say; say center(arg(1), 50, '')
do t=1 for #
say arg(1) 'element'right(t, length(#))'='right(@.t, 18)
end /*t*/
say
return

View file

@ -0,0 +1,42 @@
/*REXX program performs a type of bogo sort on numbers in an array. */
@.1 = 0 ; @.11= -64 ; @.21= 4096 ; @.31= 6291456
@.2 = 0 ; @.12= 64 ; @.22= 40960 ; @.32= 5242880
@.3 = 1 ; @.13= 256 ; @.23= 16384 ; @.33= -15728640
@.4 = 2 ; @.14= 0 ; @.24= -114688 ; @.34= -27262976
@.5 = 0 ; @.15= -768 ; @.25= -131072 ; @.35= 29360128
@.6 = -4 ; @.16= -512 ; @.26= 262144 ; @.36= 104857600
@.7 = 0 ; @.17= 2048 ; @.27= 589824 ; @.37= -16777216
@.8 = 16 ; @.18= 3072 ; @.28= -393216 ; @.38= -335544320
@.9 = 16 ; @.19= -4096 ; @.29= -2097152 ; @.39= -184549376
@.10= -32 ; @.20= -12288 ; @.30= -262144 ; @.40= 905969664
/* [↑] @.1 is really the 0th Berstel number*/
#=40 /*we have a list of two score Berstel numbers.*/
call tell 'before bogo sort'
do bogo=1
do j=1 for #; ?=@.j /*? is the next number in array.*/
do k=j+1 to #
if @.k>=? then iterate /*is this # in order? Get next. */
/*get 2 unique random #s for swap*/
do until a\==b; a=random(j, k); b=random(j, k); end
parse value @.a @.b with @.b @.a /*swap 2 random #s in array.*/
iterate bogo /*go and try another bogo sort. */
end /*k*/
end /*j*/
leave /*we're finished with bogo sort. */
end /*bogo*/ /* [↓] show the # of bogo sorts.*/
say 'number of bogo sorts performed =' bogo
call tell ' after bogo sort'
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────TELL subroutine─────────────────────*/
tell: say; say center(arg(1), 50, '')
do t=1 for #
say arg(1) 'element'right(t, length(#))'='right(@.t, 18)
end /*t*/
say
return

View file

@ -1,46 +0,0 @@
/*REXX program;to perform a type of bogo sort on an array. */
/*a.1 is really the 0th Berstel number... */
a.1 = 0 ; a.11= -64 ; a.21= 4096 ; a.31= 6291456
a.2 = 0 ; a.12= 64 ; a.22= 40960 ; a.32= 5242880
a.3 = 1 ; a.13= 256 ; a.23= 16384 ; a.33= -15728640
a.4 = 2 ; a.14= 0 ; a.24= -114688 ; a.34= -27262976
a.5 = 0 ; a.15= -768 ; a.25= -131072 ; a.35= 29360128
a.6 = -4 ; a.16= -512 ; a.26= 262144 ; a.36= 104857600
a.7 = 0 ; a.17= 2048 ; a.27= 589824 ; a.37= -16777216
a.8 = 16 ; a.18= 3072 ; a.28= -393216 ; a.38= -335544320
a.9 = 16 ; a.19= -4096 ; a.29= -2097152 ; a.39= -184549376
a.10= -32 ; a.20= -12288 ; a.30= -262144 ; a.40= 905969664
size=40 /*we have a list of two score Berstel numbers.*/
call tell 'un-bogoed'
do bogo=1
do j=1 for size
_=a.j
do k=j+1 to size
if a.k>=_ then iterate
n=random(j,k) /*we have an num out of order.get rand*/
do forever; m=random(j,k) /*get another random number.*/
if m\==n then leave /*ensure we're not swapping the same #*/
end /*forever*/
parse value a.n a.m with a.m a.n /*swap 2 random nums*/
iterate bogo
end /*k*/
end /*j*/
leave
end /*bogo*/
say 'number of bogo sorts performed =' bogo-1
call tell ' bogoed'
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────TELL subroutine─────────────────────*/
tell: say; say center(arg(1),40,'')
do j=1 to size
say arg(1) 'element'right(j,length(size))'='right(a.j,20)
end /*j*/
say
return