Created
July 24, 2019 09:58
-
-
Save kelvin8773/5f91592c64015d534fb61b8e8e2b2dc6 to your computer and use it in GitHub Desktop.
Insertion Sort in Ruby
This file contains 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
def insertion_sort(array) | |
result = array.dup | |
def helper(array, index=0) | |
return array if array[index] > array[index-1] || index == 0 | |
if array[index] < array[index-1] | |
array[index], array[index-1] = array[index-1], array[index] | |
end | |
helper(array, index-1) | |
end | |
for index in 1..result.size-1 | |
result = helper(result, index) | |
end | |
result | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment