-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_sort.py
More file actions
243 lines (157 loc) · 5.48 KB
/
Copy pathlinear_sort.py
File metadata and controls
243 lines (157 loc) · 5.48 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
"""
Implementation of Insertion Sort as found in 'Introduction to Algorithms'
chapter 2
to run tests execute:
python3 -m unittest linear_sort
"""
import unittest
def insertion_sort(sequence, debug=False):
""" Sorts sequence using insertion sort algorithm
I: sequence of numbers <a1, ... , an>
O: permutation <a1', ... , an'> such that a1' <= .... <= an'
sorting is in palce think two subarrays:
A[1 .. j -1] is sorted
A[j + 1 .. n] is unsorted
if debug is true state of sequence after each run is printed
"""
# a[0:i] is the ordered subarray
# a[i:len(a)] is unordered subarray
# The while loop iterates down the ordered subarray
# 'pushing up' values to create space for
# right location for the key
# j is index of current element
# insert A[j] into sorted sequence A[1..j-1]
for j, el in enumerate(sequence):
if j == 0: continue
if debug: print(str(sequence))
key = sequence[j]
i = j - 1
# Descend ordered sequence and whilst
# cursor element is superior to key
# shift element at index i to i + 1
while i >= 0 and sequence[i] > key:
sequence[i + 1] = sequence[i]
i = i - 1
sequence[i + 1] = key # insert key
return sequence
def selection_sort(sequence, debug=False):
for i, el in enumerate(sequence):
if debug: print(str(sequence))
minimum = i
for j, el in enumerate(sequence[i:]):
if el < sequence[minimum]:
minimum = i + j # new minimum
# swap values:
sequence[i], sequence[minimum] = sequence[minimum], sequence[i]
return sequence
def merge(first, second):
"""
takes two sorted lists and returns
them merged into new sorted list
"""
merged = list()
i = 0
y = 0
while len(first[i:]) and len(second[y:]):
first_head = first[i]
second_head = second[y]
if first_head <= second_head:
to_add = first_head
i += 1
else:
to_add = second_head
y += 1
merged.append(to_add)
if len(first[i:]):
merged.extend(first[i:])
elif len(second[y:]):
merged.extend(second[y:])
return merged
def merge_sort(sequence):
to_merge = [[item] for item in sequence]
while len(to_merge) > 1:
first = to_merge[0]
second = to_merge[1]
merged = merge(first, second)
to_merge = to_merge[2:]
to_merge.append(merged)
result = to_merge[0]
return result
class TestInsertionSort(unittest.TestCase):
# TODO generalise these tests to all sorting algorithms
# sorts = [insertion_sort, selection_sort]
def test_can_sort_empty_list(self):
input_ = []
output = insertion_sort(input_)
expected = []
self.assertSequenceEqual(output, expected)
def test_can_sort_list_of_length_one(self):
input_ = [1]
output = insertion_sort(input_)
expected = [1]
self.assertSequenceEqual(output, expected)
def test_can_sort_ordered_list_of_length_two(self):
input_ = [1, 2]
output = insertion_sort(input_)
expected = [1, 2]
self.assertSequenceEqual(output, expected)
def test_can_sort_unordered_list_of_length_two(self):
input_ = [2, 1]
output = insertion_sort(input_)
expected = [1, 2]
self.assertSequenceEqual(output, expected)
def test_can_sort_unordered_list_of_length_three(self):
input_ = [4, 2, 1]
output = insertion_sort(input_)
expected = [1, 2, 4]
self.assertSequenceEqual(output, expected)
def test_can_sort_unordered_list_of_length_four(self):
input_ = [4, 2, 1, 3]
output = insertion_sort(input_)
expected = [1, 2, 3, 4]
self.assertSequenceEqual(output, expected)
def test_can_sort_unordered_list(self):
input_ = [11, 4, 2, 1, 3, 9]
output = insertion_sort(input_)
expected = [1, 2, 3, 4, 9, 11]
self.assertSequenceEqual(output, expected)
class TestSelectionSort(unittest.TestCase):
def test_sequence_three_ints(self):
seq = [1, 3, 2]
got = selection_sort(seq)
expected = [1, 2, 3]
self.assertSequenceEqual(got, expected)
def test_sequence_four_ints(self):
seq = [1, 5, 3, 2]
got = selection_sort(seq)
expected = [1, 2, 3, 5]
self.assertSequenceEqual(got, expected)
def test_sequence_five_ints(self):
seq = [6, 5, 3, 2, -4]
got = selection_sort(seq)
expected = [-4, 2, 3, 5, 6]
self.assertSequenceEqual(got, expected)
class TestMergeSort(unittest.TestCase):
def test_merge_three_items_each(self):
seq_a = [2, 4, 6]
seq_b = [1, 3, 5]
got = merge(seq_a, seq_b)
expected = [1, 2, 3, 4, 5, 6]
self.assertSequenceEqual(got, expected)
def test_merge_one_items_each(self):
seq_a = [2]
seq_b = [1]
got = merge(seq_a, seq_b)
expected = [1, 2]
self.assertSequenceEqual(got, expected)
def test_merge_one_items_each_values_swapped(self):
seq_a = [1]
seq_b = [2]
got = merge(seq_a, seq_b)
expected = [1, 2]
self.assertSequenceEqual(got, expected)
def test_merge_sort(self):
seq = [2, 4, 6, 1, 3, 5]
got = merge_sort(seq)
expected = [1, 2, 3, 4, 5, 6]
self.assertSequenceEqual(got, expected)