mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-18 16:27:02 +00:00
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:
parent
cd10c944d1
commit
38d7e7073a
|
@ -6,13 +6,13 @@ def bubble_sort(collection):
|
|||
:return: the same collection ordered by ascending
|
||||
|
||||
Examples:
|
||||
>>> bubble_sort([0, 5, 3, 2, 2])
|
||||
>>> bubble_sort([0, 5, 2, 3, 2])
|
||||
[0, 2, 2, 3, 5]
|
||||
|
||||
>>> bubble_sort([])
|
||||
[]
|
||||
|
||||
>>> bubble_sort([-2, -5, -45])
|
||||
>>> bubble_sort([-2, -45, -5])
|
||||
[-45, -5, -2]
|
||||
|
||||
>>> bubble_sort([-23, 0, 6, -4, 34])
|
||||
|
@ -29,11 +29,14 @@ def bubble_sort(collection):
|
|||
swapped = True
|
||||
collection[j], collection[j + 1] = collection[j + 1], collection[j]
|
||||
if not swapped:
|
||||
break # Stop iteration if the collection is sorted.
|
||||
break # Stop iteration if the collection is sorted.
|
||||
return collection
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import time
|
||||
user_input = input("Enter numbers separated by a comma:").strip()
|
||||
unsorted = [int(item) for item in user_input.split(",")]
|
||||
start = time.process_time()
|
||||
print(*bubble_sort(unsorted), sep=",")
|
||||
print(f"Processing time: {time.process_time() - start}")
|
||||
|
|
Loading…
Reference in New Issue
Block a user