You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

18 lines
382 B
Ruby

# Insertion sort algorithm,
# uses N^2/4 compares and N^2/4 exchanges to sort a randomly ordered array,
# see test/insertion_sort_test.rb
module Insertion
def self.sort(array)
1.upto(array.size - 1) do |i|
j = i
while j > 0 && array[j] < array[j - 1]
array[j], array[j - 1] = array[j - 1], array[j]
j -= 1
end
end
array
end
end