-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuble_sort.py
More file actions
55 lines (41 loc) · 1.75 KB
/
buble_sort.py
File metadata and controls
55 lines (41 loc) · 1.75 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
import unittest
import random
def bubble_sort(arr: list[int]):
for i in range(len(arr)):
for j in range(1, len(arr) - i):
if arr[j] < arr[j - 1]:
arr[j], arr[j - 1] = arr[j - 1], arr[j]
return arr
arr = [5, 1, 4, 2, 3, 2, 5, 4, 54, 5443, 435, 34345, 34, 534, 5, 345, 435, 345, 3]
# test cases
class BubbleSortTest(unittest.TestCase):
def test_sort_1(self):
arr = random.sample(range(10, 30), random.randint(3, 10))
print(bubble_sort((arr)), sorted(arr))
self.assertEqual(bubble_sort((arr)), sorted(arr))
def test_sort_2(self):
arr = random.sample(range(10, 30), random.randint(3, 10))
print(bubble_sort((arr)), sorted(arr))
self.assertEqual(bubble_sort((arr)), sorted(arr))
def test_sort_3(self):
arr = random.sample(range(10, 30), random.randint(3, 10))
print(bubble_sort((arr)), sorted(arr))
self.assertEqual(bubble_sort((arr)), sorted(arr))
def test_sort_4(self):
arr = random.sample(range(10, 30), random.randint(3, 10))
print(bubble_sort((arr)), sorted(arr))
self.assertEqual(bubble_sort((arr)), sorted(arr))
def test_sort_5(self):
arr = random.sample(range(10, 30), random.randint(3, 10))
print(bubble_sort((arr)), sorted(arr))
self.assertEqual(bubble_sort((arr)), sorted(arr))
def test_sort_6(self):
arr = random.sample(range(10, 30), random.randint(3, 10))
print(bubble_sort((arr)), sorted(arr))
self.assertEqual(bubble_sort((arr)), sorted(arr))
def test_sort_7(self):
arr = [4, 2, 2, 8, 5, 3]
print(bubble_sort((arr)), sorted(arr))
self.assertEqual(bubble_sort((arr)), sorted(arr))
if __name__ == "__main__":
unittest.main()