Skip to content

Commit 60ff83e

Browse files
add class SelectionSort.py and test_selection_sort.py
1 parent 8c33fc5 commit 60ff83e

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class SelectionSort:
2+
def sort(self, array):
3+
if array is None:
4+
raise TypeError('array cannot be None')
5+
return self._sort(array)
6+
7+
def _sort(self, array):
8+
if len(array) < 2:
9+
return array
10+
11+
for i in range(len(array)):
12+
minimum = i
13+
for j in range(i+1, len(array)):
14+
if array[j] < array[i]:
15+
array[i], array[j] = array[j], array[i]
16+
17+
return array
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import unittest
2+
from classical_algorithms.python.SelectionSort import SelectionSort
3+
4+
class TestSelectionSort(unittest.TestCase):
5+
def test_selection_sort(self):
6+
selection_sort = SelectionSort()
7+
8+
print('None Input')
9+
self.assertRaises(TypeError, selection_sort.sort, None)
10+
11+
print('Empty Input')
12+
self.assertEqual(selection_sort.sort([]), [])
13+
14+
print('One Element')
15+
self.assertEqual(selection_sort.sort([25]), [25])
16+
17+
print('Two or More Elements')
18+
array = [10, 1, 0, 100, 5, 15, 100, 7]
19+
self.assertEqual(selection_sort.sort(array), sorted(array))
20+
21+
print('Two or More with negative Elements')
22+
array = [10, -1, 0, 100, 5, 15, -100, -7]
23+
self.assertEqual(selection_sort.sort(array), sorted(array))
24+
25+
print('Success: selection_sort_sort\n')
26+
27+
if __name__ == '__main__':
28+
unittest.main()

0 commit comments

Comments
 (0)