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()
This commit is contained in:
Sujitkumar Singh 2019-10-20 01:42:54 +05:30 committed by Christian Clauss
parent cd10c944d1
commit 38d7e7073a

View File

@ -6,13 +6,13 @@ def bubble_sort(collection):
:return: the same collection ordered by ascending :return: the same collection ordered by ascending
Examples: Examples:
>>> bubble_sort([0, 5, 3, 2, 2]) >>> bubble_sort([0, 5, 2, 3, 2])
[0, 2, 2, 3, 5] [0, 2, 2, 3, 5]
>>> bubble_sort([]) >>> bubble_sort([])
[] []
>>> bubble_sort([-2, -5, -45]) >>> bubble_sort([-2, -45, -5])
[-45, -5, -2] [-45, -5, -2]
>>> bubble_sort([-23, 0, 6, -4, 34]) >>> bubble_sort([-23, 0, 6, -4, 34])
@ -29,11 +29,14 @@ def bubble_sort(collection):
swapped = True swapped = True
collection[j], collection[j + 1] = collection[j + 1], collection[j] collection[j], collection[j + 1] = collection[j + 1], collection[j]
if not swapped: if not swapped:
break # Stop iteration if the collection is sorted. break # Stop iteration if the collection is sorted.
return collection return collection
if __name__ == "__main__": if __name__ == "__main__":
import time
user_input = input("Enter numbers separated by a comma:").strip() user_input = input("Enter numbers separated by a comma:").strip()
unsorted = [int(item) for item in user_input.split(",")] unsorted = [int(item) for item in user_input.split(",")]
start = time.process_time()
print(*bubble_sort(unsorted), sep=",") print(*bubble_sort(unsorted), sep=",")
print(f"Processing time: {time.process_time() - start}")