Skip to content

Instantly share code, notes, and snippets.

@ruggeri
Created January 3, 2013 08:12
Show Gist options
  • Save ruggeri/4441742 to your computer and use it in GitHub Desktop.
Save ruggeri/4441742 to your computer and use it in GitHub Desktop.
Insertion sort implementation
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
@kdavh
Copy link

kdavh commented Jan 14, 2013

I think one could start incrementing i from 1 instead of 0 since that first array element is never moved. Line 2 would read i = 1. Is that correct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment