Created
June 12, 2017 05:57
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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