Skip to content

Instantly share code, notes, and snippets.

@Nitesh-Mishra
Last active September 11, 2017 19:06
Show Gist options
  • Select an option

  • Save Nitesh-Mishra/e573cb281ea439da53c43eb662ef8ed8 to your computer and use it in GitHub Desktop.

Select an option

Save Nitesh-Mishra/e573cb281ea439da53c43eb662ef8ed8 to your computer and use it in GitHub Desktop.
Insertion Sort program in ruby
# insertion_sort.rb
#
# $ ruby insertion_sort.rb
# [1, 2, 3, 4, 5, 6, 7, 8]
def insertion_sort(array)
for i in 1..array.length - 1
j = i
temp = array[i]
while ((j>0) and (array[j-1] > temp))
array[j] = array[j-1]
j -= 1
end
array[j] = temp
end
return array
end
array = [6,5,3,1,8,7,2,4]
insertion_sort array
print array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment