Skip to content

Instantly share code, notes, and snippets.

@theHamdiz
Created June 12, 2017 05:57
Show Gist options
  • Select an option

  • Save theHamdiz/c108154367dbad81aa0ae50ec1613b0c to your computer and use it in GitHub Desktop.

Select an option

Save theHamdiz/c108154367dbad81aa0ae50ec1613b0c to your computer and use it in GitHub Desktop.
insertion sorting in ruby at its finest, using traditional ruby methods and performing on a 256 digit array.
require_relative 'random_password'
include RandomPassword
class Array
def insertion_sort
start_time = Time.now
arr = self
for i in (1...(arr.size))
if arr[i-1] > arr[i]
i.downto(1) do |el|
if arr[el] < arr[el-1]
arr[el-1], arr[el] = arr[el], arr[el-1]
end
end
end
end
puts "Insertion sorting took #{Time.now - start_time} ms for a 256 membered array"
arr
end
end
# just generates a random 256 digit password then split the string into an array of random characters to be sorted.
unsorted = generate.split('')
puts unsorted.inspect
sorted = unsorted.insertion_sort
puts sorted.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment