Skip to content

Commit 9b3f7c3

Browse files
Test random input for bubble sort (TheAlgorithms#2492)
1 parent 2388bf4 commit 9b3f7c3

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

Diff for: sorts/bubble_sort.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@ def bubble_sort(collection):
88
Examples:
99
>>> bubble_sort([0, 5, 2, 3, 2])
1010
[0, 2, 2, 3, 5]
11-
12-
>>> bubble_sort([])
13-
[]
14-
15-
>>> bubble_sort([-2, -45, -5])
16-
[-45, -5, -2]
17-
18-
>>> bubble_sort([-23, 0, 6, -4, 34])
19-
[-23, -4, 0, 6, 34]
20-
11+
>>> bubble_sort([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2])
12+
True
13+
>>> bubble_sort([]) == sorted([])
14+
True
15+
>>> bubble_sort([-2, -45, -5]) == sorted([-2, -45, -5])
16+
True
2117
>>> bubble_sort([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34])
2218
True
19+
>>> bubble_sort(['d', 'a', 'b', 'e', 'c']) == sorted(['d', 'a', 'b', 'e', 'c'])
20+
True
21+
>>> import random
22+
>>> collection = random.sample(range(-50, 50), 100)
23+
>>> bubble_sort(collection) == sorted(collection)
24+
True
25+
>>> import string
26+
>>> collection = random.choices(string.ascii_letters + string.digits, k=100)
27+
>>> bubble_sort(collection) == sorted(collection)
28+
True
2329
"""
2430
length = len(collection)
2531
for i in range(length - 1):
@@ -34,8 +40,11 @@ def bubble_sort(collection):
3440

3541

3642
if __name__ == "__main__":
43+
import doctest
3744
import time
3845

46+
doctest.testmod()
47+
3948
user_input = input("Enter numbers separated by a comma:").strip()
4049
unsorted = [int(item) for item in user_input.split(",")]
4150
start = time.process_time()

0 commit comments

Comments
 (0)