Skip to content

Commit 38d7e70

Browse files
SinghSujitkumarcclauss
authored andcommitted
The time complexity of every algorithms make its value (#1401)
* added timer in bubble sort * Updated time of execution * import time in main only * Update bubble_sort.py * start = time.process_time()
1 parent cd10c94 commit 38d7e70

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

sorts/bubble_sort.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ def bubble_sort(collection):
66
:return: the same collection ordered by ascending
77
88
Examples:
9-
>>> bubble_sort([0, 5, 3, 2, 2])
9+
>>> bubble_sort([0, 5, 2, 3, 2])
1010
[0, 2, 2, 3, 5]
1111
1212
>>> bubble_sort([])
1313
[]
1414
15-
>>> bubble_sort([-2, -5, -45])
15+
>>> bubble_sort([-2, -45, -5])
1616
[-45, -5, -2]
1717
1818
>>> bubble_sort([-23, 0, 6, -4, 34])
@@ -29,11 +29,14 @@ def bubble_sort(collection):
2929
swapped = True
3030
collection[j], collection[j + 1] = collection[j + 1], collection[j]
3131
if not swapped:
32-
break # Stop iteration if the collection is sorted.
32+
break # Stop iteration if the collection is sorted.
3333
return collection
3434

3535

3636
if __name__ == "__main__":
37+
import time
3738
user_input = input("Enter numbers separated by a comma:").strip()
3839
unsorted = [int(item) for item in user_input.split(",")]
40+
start = time.process_time()
3941
print(*bubble_sort(unsorted), sep=",")
42+
print(f"Processing time: {time.process_time() - start}")

0 commit comments

Comments
 (0)