Navigate: Ricochet! / code / examples / ruby-bogosort
Bogosort : Ruby script: download as .txt
This script implements Bogosort in Ruby language.
I wrote it as a 'Hello World' in the language, and put up here as an example. It probably isn't nice / efficient (you'd smile at that term if you already know what BogoSort is) code in the language, but it works. :)
#! /usr/local/bin/ruby -w
# Simple implementation of BogoSort
# Let's have an array
# with some integers
a = [1,2,7,4,5]
# The array's length...
# Remember the array's index goes from 0 to .length-1
alen = a.length
iterations = 0
# Initialize the random number generator seed
srand()
loop {
sorted = true
for i in 0..alen-2
if (a[i]>a[i+1])
sorted = false
break
end
end
if (sorted)
break
end
for i in 0..alen-1
r = rand(alen)
a[r],a[i] = a[i],a[r]
end
iterations = iterations + 1
}
printf("Bogosorted in %d iterations!\n", iterations)
---
Links
Scripted By: Manish Malik
---

The content on this web site is licensed under a Creative Commons License.