Created
January 3, 2013 08:12
-
-
Save ruggeri/4441742 to your computer and use it in GitHub Desktop.
Insertion sort implementation
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) | |
i = 0 | |
while i < array.length | |
j = 0 | |
while array[j] < array[i] | |
j += 1 | |
end | |
# array[j] is the first element >= array[i] | |
# delete array[i] and move it to just before position j | |
val = array.delete_at(i) | |
array.insert(j, val) | |
i += 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think one could start incrementing
i
from 1 instead of 0 since that first array element is never moved. Line 2 would readi = 1
. Is that correct?