Class: Algorithmable::Sort::Shell

Inherits:
Object
  • Object
show all
Extended by:
Utils
Defined in:
lib/algorithmable/sort/shell.rb

Class Method Summary collapse

Methods included from Utils

exchange, partition, swap

Class Method Details

.sort(collection) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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