-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertion_sort_1.rb
138 lines (100 loc) · 2.72 KB
/
insertion_sort_1.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
def insertion_sort(ar)
(ar.length-1).downto(1) do |i|
break unless ar[i] < ar[i-1]
swap(ar, i, i-1)
end
ar
end
def insertion_sort_no_swap(ar)
val = ar.last
(ar.length-1).downto(1) do |i|
if (val < ar[i-1])
ar[i] = ar[i-1]
else
ar[i] = val
break
end
end
#when we don't swap, we need to check at the very end...
(ar[0] = val) if val < ar[0]
ar
end
def swap(ar, i, j)
ar[i], ar[j] = [ar[j], ar[i]]
end
# [3] => [3]
# [8, 3] => [3, 8]
# [1, 8, 3] => [1, 3, 8]
def assert(actual, expected)
raise "actual: #{actual}, expected: #{expected}" unless expected == actual
end
def it_handles_an_empty_array
ar = []
assert(insertion_sort(ar), [])
end
def it_handles_an_empty_array_no_swap
ar = []
assert(insertion_sort_no_swap(ar), [])
end
def it_handles_a_single_element
ar = [8]
assert(insertion_sort(ar), [8])
end
def it_handles_a_single_element_no_swap
ar = [8]
assert(insertion_sort_no_swap(ar), [8])
end
def it_swaps_elements_larger_than_the_last_element
ar = [8, 3]
assert(insertion_sort(ar), [3, 8])
end
def it_breaks_once_it_finds_a_smaller_element
ar = [1, 8, 3]
assert(insertion_sort(ar), [1, 3, 8])
end
def it_handles_a_large_array
ar = [2, 4, 6, 8, 3]
assert(insertion_sort(ar), [2, 3, 4, 6, 8])
end
def it_handles_a_large_array_no_swap
ar = [2, 4, 6, 8, 3]
assert(insertion_sort_no_swap(ar), [2, 3, 4, 6, 8])
end
def it_handles_duplicate_elements
ar = [2, 4, 4, 8, 3]
assert(insertion_sort(ar), [2, 3, 4, 4, 8])
end
def it_handles_duplicate_elements_no_swap
ar = [2, 4, 4, 8, 3]
assert(insertion_sort_no_swap(ar), [2, 3, 4, 4, 8])
end
def it_handles_elements_destined_for_the_first_location
ar = [2, 4, 6, 8, -10000]
assert(insertion_sort(ar), [-10000, 2, 4, 6, 8])
end
def it_handles_elements_destined_for_the_first_location_no_swap
ar = [2, 4, 6, 8, -10000]
assert(insertion_sort_no_swap(ar), [-10000, 2, 4, 6, 8])
end
def it_handles_annoying_example
ar = %w{1 3 5 9 13 22 27 35 46 51 55 83 87 23}
assert(insertion_sort(ar), %w{1 3 5 9 13 22 23 27 35 46 51 55 83 87})
end
def it_handles_annoying_example_no_swap
ar = %w{1 3 5 9 13 22 27 35 46 51 55 83 87 23}
assert(insertion_sort_no_swap(ar), %w{1 3 5 9 13 22 23 27 35 46 51 55 83 87})
end
# it_handles_an_empty_array()
# it_handles_a_single_element()
# it_handles_a_single_element_no_swap()
# it_swaps_elements_larger_than_the_last_element()
# it_breaks_once_it_finds_a_smaller_element()
# it_handles_a_large_array()
# it_handles_a_large_array_no_swap()
# it_handles_duplicate_elements()
# it_handles_duplicate_elements_no_swap()
# it_handles_elements_destined_for_the_first_location()
# it_handles_elements_destined_for_the_first_location_no_swap()
it_handles_annoying_example()
it_handles_annoying_example_no_swap()
puts "Passed"