Module: BrutishSorts

Extended by:
BrutishSorts
Included in:
BrutishSorts
Defined in:
lib/brutish_sorts.rb

Instance Method Summary collapse

Instance Method Details

#insertion_sort(array) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/brutish_sorts.rb', line 17

def insertion_sort(array)
  dup = array.dup
  tmp = 0
  0.upto(dup.length - 1) do |a|
    a.downto(0) do |b|
      if b < (dup.length - 1)
        if dup[b + 1] < dup[b]
          tmp = dup[b]
          dup[b] = dup[b + 1]
          dup[b + 1] = tmp
        end
      end
    end
  end
  dup
end

#selection_sort(array) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/brutish_sorts.rb', line 7

def selection_sort(array)
  dup = array.dup
  pivot = 0
  for i in 0..dup.length - 1
    pivot = SelectionSort.min_index(dup,i)
    dup = SelectionSort.swap(dup,pivot,i)
  end
  dup
end