Sorting algorithms/Bogosort: Difference between revisions

Added Easylang
(add BQN)
(Added Easylang)
 
(8 intermediate revisions by 6 users not shown)
Line 798:
}
}</syntaxhighlight>
 
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">global array
dim array = {10, 1, 2, -6, 3}
lb = array[?,]-1 : ub = array[?]-1
 
print "unsort ";
for i = lb to ub
print rjust(array[i], 4);
next i
 
call Bogosort(array) # ordenar el array
 
print chr(10); " sort ";
for i = lb to ub
print rjust(array[i], 4);
next i
end
 
subroutine shuffle(array)
n = array[?] : m = array[?]*2
 
for k = 1 to m
i = int(Rand*n)
j = int(Rand*n)
tmp = array[i] #swap lb(i), lb(j)
array[i] = array[j]
array[j] = tmp
next k
end subroutine
 
function inorder(array)
n = array[?]
for i = 0 to n-2
if array[i] > array[i+1] then return false
next i
return true
end function
 
subroutine Bogosort(array)
while not inorder(array)
call shuffle(array)
end while
end subroutine</syntaxhighlight>
 
=={{header|BBC BASIC}}==
Line 1,140 ⟶ 1,185:
}
}</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
proc shuffle . l[] .
for i = len l[] downto 2
r = randint i
swap l[i] l[r]
.
.
proc issorted . l[] r .
for i = 2 to len l[]
if l[i] < l[i - 1]
r = 0
return
.
.
r = 1
.
proc bogosort . l[] .
repeat
issorted l[] r
until r = 1
shuffle l[]
.
.
list[] = [ 2 7 41 11 3 1 6 5 8 ]
bogosort list[]
print list[]
</syntaxhighlight>
 
{{out}}
<pre>
[ 1 2 3 5 6 7 8 11 41 ]
</pre>
 
=={{header|Eiffel}}==
Line 1,641 ⟶ 1,720:
}
];</syntaxhighlight>
 
=={{header|Insitux}}==
 
{{Trans|Clojure}}
 
<syntaxhighlight lang="insitux">(function bogo-sort order list
(return-unless (1 list) [])
(if (... order list)
list
(recur order (shuffle list))))
(bogo-sort < [7 5 12 1 4 2 23 18])</syntaxhighlight>
 
Even with this small list the web REPL sometimes exceeds its default recur budget (1e4 - 10000):
 
<pre>4:6 (recur order (shuffle list))))
Budget Error: recurred too many times.</pre>
 
=={{header|Io}}==
Line 2,999 ⟶ 3,095:
[0, 1, 2, 2, 4, 31, 65, 83, 99, 782]
</pre>
 
=={{header|RPL}}==
<code>KNUTH</code> is defined at [[Knuth shuffle#RPL|Knuth shuffle]]
{{works with|HP|48G}}
≪ '''WHILE''' DUP ΔLIST ≪ MIN ≫ STREAM 0 < '''REPEAT'''
<span style="color:blue">KNUTH</span>
'''END'''
≫ '<span style="color:blue">BOGOSORT</span>' STO
 
=={{header|Ruby}}==
Line 3,075 ⟶ 3,179:
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func in_order(a) {
return true if (a.len <= 1);
var first = a[0];
a.ftlast(-1).all { |elem| first <= elem  ? do { first = elem; true }  : false }
}
 
func bogosort(a) {
a.shuffle! while  !in_order(a);
return a;
}
 
var arr = 5.of { 100.rand.intirand };
say "Before: #{arr}";
say "After: #{bogosort(arr)}";</syntaxhighlight>
{{out}}
<pre>
Line 3,434 ⟶ 3,538:
</pre>
 
=={{header|V (Vlang)}}==
Updated for V (Vlang) version 0.2.2
<syntaxhighlight lang="go">import rand
 
Line 3,479 ⟶ 3,583:
=={{header|Wren}}==
{{libheader|Wren-sort}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./sort" for Sort
 
var bogoSort = Fn.new { |a|
Line 3,528 ⟶ 3,632:
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">dim a(5)
a (0) = 10: a (1) = 1: a (2) = 2: a (3) = -6: a (4) = 3
 
Bogosort(a())
 
for i = 0 to arraysize(a(),1) - 1
print a(i), " ";
next i
end
 
sub shuffle(a())
n = arraysize(a(),1)
Line 3,546 ⟶ 3,659:
for i = 0 to n-2
if a(i) > a(i+1) then return false : fi
next i
return true
Line 3,555 ⟶ 3,668:
shuffle(a())
wend
end sub</syntaxhighlight>
 
dim a(5)
a (0) = 10: a (1) = 1: a (2) = 2: a (3) = -6: a (4) = 3
 
Bogosort(a())
 
for i = 0 to arraysize(a(),1) - 1
print a(i), " ";
next i
end
</syntaxhighlight>
 
 
=={{header|zkl}}==
1,983

edits