Sorting algorithms/Bogosort: Difference between revisions

m
→‎true bogo sort: changed the output titles, ensured that the random numbers were unique (for swapping)..
m (→‎{{header|REXX}}: corrected a statement, found by Walter Pachl, reduced number of comparisons by ½.)
m (→‎true bogo sort: changed the output titles, ensured that the random numbers were unique (for swapping)..)
Line 1,769:
=={{header|REXX}}==
===true bogo sort===
<lang rexx>/*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 numernumber of numbers in list. */
do i=1 for words(list); a@.i=word(list,i); end /*create an array.*/
call tell 'un-bogoedbefore bogo sort'
 
do bogo=1
 
do j=1 for #-1; jp=j+1; x=a.j /*X [↓] is compare a number# inwith the A. array.next*/
if @.jp>=@.j then iterate y=a.jp /*Y so isfar, theso nextgood; number inkeep arraylooking.*/
if y>=x then iterate /*soget far,2 sounique good;random #s keepfor looking.swap*/
n do until a\==b; a=random(1, #); m b=random(1, #); /*get two random numbers for swap*/end
 
parse value @.a.n a@.mb with a@.mb @.a.n /*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 bogoedsort'
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────TELL subroutine─────────────────────*/
tell: say; say center(arg(1), 4050, '─')
do t=1 tofor #
say arg(1) 'element'right(t, length(size))'='right(a@.t, 18)
end /*t*/
say
return</lang>
{{out}} using the default input:
<pre>
─────────────────before bogo sort─────────────────
───────────────un-bogoed────────────────
un-bogoedbefore bogo sort element 1= -21
un-bogoedbefore bogo sort element 2= 333
un-bogoedbefore bogo sort element 3= 0
un-bogoedbefore bogo sort element 4= 444.4
 
number of bogo sorts performed = 96
 
───────────────── after bogo sort─────────────────
─────────────── bogoed────────────────
after bogo bogoedsort element 1= -21
after bogo bogoedsort element 2= 0
after bogo bogoedsort element 3= 333
after bogo bogoedsort element 4= 444.4
</pre>