-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorter.rb
50 lines (41 loc) · 821 Bytes
/
sorter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true
module Strategy
class Sorter
attr_accessor :algo
def initialize
@algo = RubyDefault.new
end
def sort(data)
@algo.execute(data)
end
end
class RubyDefault
def execute(data)
data.sort
end
end
class Insertion
def execute(data)
(1...data.size).each do |i|
j = i
while j.positive? && (data[j - 1] > data[j])
data[j], data[j - 1] = data[j - 1], data[j]
j -= 1
end
end
data
end
end
class Selection
def execute(data)
(0...data.size).each do |j|
i_min = j
(j + 1...data.size).each do |i|
i_min = i if data[i] < data[i_min]
end
data[j], data[i_min] = data[i_min], data[j]
end
data
end
end
end