Skip to content

Commit

Permalink
Test random input for bubble sort (TheAlgorithms#2492)
Browse files Browse the repository at this point in the history
  • Loading branch information
realDuYuanChao authored Oct 2, 2020
1 parent 2388bf4 commit 9b3f7c3
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions sorts/bubble_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@ def bubble_sort(collection):
Examples:
>>> bubble_sort([0, 5, 2, 3, 2])
[0, 2, 2, 3, 5]
>>> bubble_sort([])
[]
>>> bubble_sort([-2, -45, -5])
[-45, -5, -2]
>>> bubble_sort([-23, 0, 6, -4, 34])
[-23, -4, 0, 6, 34]
>>> bubble_sort([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2])
True
>>> bubble_sort([]) == sorted([])
True
>>> bubble_sort([-2, -45, -5]) == sorted([-2, -45, -5])
True
>>> bubble_sort([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34])
True
>>> bubble_sort(['d', 'a', 'b', 'e', 'c']) == sorted(['d', 'a', 'b', 'e', 'c'])
True
>>> import random
>>> collection = random.sample(range(-50, 50), 100)
>>> bubble_sort(collection) == sorted(collection)
True
>>> import string
>>> collection = random.choices(string.ascii_letters + string.digits, k=100)
>>> bubble_sort(collection) == sorted(collection)
True
"""
length = len(collection)
for i in range(length - 1):
Expand All @@ -34,8 +40,11 @@ def bubble_sort(collection):


if __name__ == "__main__":
import doctest
import time

doctest.testmod()

user_input = input("Enter numbers separated by a comma:").strip()
unsorted = [int(item) for item in user_input.split(",")]
start = time.process_time()
Expand Down

0 comments on commit 9b3f7c3

Please sign in to comment.