Skip to content

Instantly share code, notes, and snippets.

@NullVoxPopuli
Last active November 24, 2015 18:45
Show Gist options
  • Save NullVoxPopuli/fb340585cf0cdd28e6c6 to your computer and use it in GitHub Desktop.
Save NullVoxPopuli/fb340585cf0cdd28e6c6 to your computer and use it in GitHub Desktop.
Random sorting algorithm benchmarks
num_sorted = 0
num_total = 1000000
num_elements = 10
max_random_integer = num_elements
sorted = []
num_total.times do |i|
array = Array.new(num_elements){ rand(max_random_integer) }
if array == array.sort
num_sorted += 1
sorted << array
else
# destroy the universe?
end
percent = (i / num_total.to_f) * 100.0
puts "#{percent.round(8)}% Complete"
end
ratio = num_sorted / num_total.to_f
puts "Finished!"
puts "The number of lists that were randomly sorted:"
puts "#{sorted.size} -- #{ratio * 100}% (out of #{num_total} iterations)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment