class Algorithmable::Sort::Shell

Public Class Methods

sort(collection) click to toggle source
# File lib/algorithmable/sort/shell.rb, line 5
def self.sort(collection)
  return collection if 2 > collection.length
  length = collection.length
  h = 1

  h = 3 * h + 1 while h < length / 3

  while h >= 1
    h.upto(length - 1).each do |i|
      j = i
      while j >= h && collection[j] < collection[j - h]
        exchange(j, j - h, collection)
        j -= h
      end
    end
    h /= 3
  end

  collection
end