Bogosort in ruby

Yesterday, I stumbled across the Bogosort sorting algorithm. The example implementations looked horrible so I tried to implement it in a short and readable way, in Ruby of course.

Here is my first and only attempt of a Ruby Bogosort.

# Use: ruby bogosort.rb a b c

def sorted
  for i in (0..$array.length-2)
    return false if $array[i] > $array[i+1]
  return true

$array = ARGV
$array = $array.sort_by {rand} while not sorted
puts ">>>>>>>>>>>>>>>> #{$array}"

You can find the Java implementation on the wikipedia page:Bogosort

Here are other C, Java and Python implementations:

I wonder if the C and Java examples really have to be that long.

Leave a Reply

Your email address will not be published. Required fields are marked *