-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.rb
58 lines (44 loc) · 1023 Bytes
/
sort.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
51
52
53
54
55
56
57
require 'test/unit'
# O(n^2) 3 items 3 - 1 loops which 3 if we remove the constant
def sort(array)
return array if array.size <= 1
move = true
index = 0
while move do
while index < array.size do
current = array[index]
next_item = array[index + 1]
break if next_item.nil?
if current > next_item
array[index] = next_item
array[index + 1] = current
move = true
else
move = false
end
index += 1
end
index = 0
end
array
end
class TestSort < Test::Unit::TestCase
def test_reverse_array_happy_path
array = [1, 2, 3]
expected = [1, 2, 3]
result = sort(array)
assert_equal expected, result
end
def test_reverse_array_non_happy_path
array = [1, 3, 2]
expected = [1, 2, 3]
result = sort(array)
assert_equal expected, result
end
def test_reverse_array_unhappy_path
array = [3, 2, 1]
expected = [1, 2, 3]
result = sort(array)
assert_equal expected, result
end
end