Sorting algorithms/Bogosort: Difference between revisions
Content added Content deleted
m (moved Bogosort to Sorting algorithms/Bogosort) |
(VBScript entry) |
||
Line 1,167: | Line 1,167: | ||
output: |
output: |
||
<pre><0,8,12,47,50,51></pre> |
<pre><0,8,12,47,50,51></pre> |
||
=={{header|VBScript}}== |
|||
=====Implementation===== |
|||
<lang vb> |
|||
sub swap( byref a, byref b ) |
|||
dim tmp |
|||
tmp = a |
|||
a = b |
|||
b = tmp |
|||
end sub |
|||
'knuth shuffle (I think) |
|||
function shuffle( a ) |
|||
dim i |
|||
dim r |
|||
randomize timer |
|||
for i = lbound( a ) to ubound( a ) |
|||
r = int( rnd * ( ubound( a ) + 1 ) ) |
|||
if r <> i then |
|||
swap a(i), a(r) |
|||
end if |
|||
next |
|||
shuffle = a |
|||
end function |
|||
function inOrder( a ) |
|||
dim res |
|||
dim i |
|||
for i = 0 to ubound( a ) - 1 |
|||
res = ( a(i) <= a(i+1) ) |
|||
if res = false then exit for |
|||
next |
|||
inOrder = res |
|||
end function |
|||
</lang> |
|||
=====Invocation===== |
|||
<lang vb> |
|||
dim a |
|||
a = array(11, 1, 2, 3, 4, 4, 6, 7, 8) |
|||
dim t |
|||
t = timer |
|||
while not inorder( a ) |
|||
shuffle a |
|||
wend |
|||
wscript.echo timer-t, "seconds" |
|||
wscript.echo join( a, ", " ) |
|||
</lang> |
|||
=====A few outputs (timed)===== |
|||
<pre> |
|||
10.34766 seconds |
|||
1, 2, 3, 4, 4, 6, 7, 8, 11 |
|||
0.5039063 seconds |
|||
1, 2, 3, 4, 4, 6, 7, 8, 11 |
|||
1.980469 seconds |
|||
1, 2, 3, 4, 4, 6, 7, 8, 11 |
|||
</pre> |