Created
October 16, 2013 17:10
-
-
Save kevbuchanan/7011344 to your computer and use it in GitHub Desktop.
Insertion Sort - Ruby
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
| def insertion_sort(array) | |
| 0.upto(array.length - 1).each do |index| | |
| element = array[index] | |
| position = index | |
| while element < array[position - 1] && position > 0 | |
| array[position] = array[position - 1] | |
| array[position - 1] = element | |
| position -= 1 | |
| end | |
| end | |
| array | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment