From 6d515cc63fab695c49774d2c3e9ed93ff287770b Mon Sep 17 00:00:00 2001 From: SinghSujitkumar <2017.sujitkumar.singh@ves.ac.in> Date: Sat, 19 Oct 2019 12:55:43 +0530 Subject: [PATCH 1/5] added timer in bubble sort --- sorts/bubble_sort.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index ccd8a2e11ee1..0cca0d3bbfc1 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -1,3 +1,5 @@ +import time +start = time.process_time() def bubble_sort(collection): """Pure implementation of bubble sort algorithm in Python @@ -37,3 +39,4 @@ def bubble_sort(collection): user_input = input("Enter numbers separated by a comma:").strip() unsorted = [int(item) for item in user_input.split(",")] print(*bubble_sort(unsorted), sep=",") +print(time.process_time() - start) From 5730eb7bb7e8e4c5d38907cf5a937e05f9c2f442 Mon Sep 17 00:00:00 2001 From: SinghSujitkumar <2017.sujitkumar.singh@ves.ac.in> Date: Sat, 19 Oct 2019 21:22:49 +0530 Subject: [PATCH 2/5] Updated time of execution --- sorts/bubble_sort.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index 0cca0d3bbfc1..b628a717b940 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -1,5 +1,5 @@ import time -start = time.process_time() +start = 0 def bubble_sort(collection): """Pure implementation of bubble sort algorithm in Python @@ -23,6 +23,7 @@ def bubble_sort(collection): >>> bubble_sort([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34]) True """ + start = time.process_time() length = len(collection) for i in range(length - 1): swapped = False @@ -31,12 +32,16 @@ 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__": user_input = input("Enter numbers separated by a comma:").strip() unsorted = [int(item) for item in user_input.split(",")] + start = 0 print(*bubble_sort(unsorted), sep=",") -print(time.process_time() - start) + print(time.process_time() - start) + + From 29061b94a9227e9afff6e6174b975f239c07ded1 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 19 Oct 2019 20:20:09 +0200 Subject: [PATCH 3/5] import time in main only --- sorts/bubble_sort.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index b628a717b940..a970d4774a07 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -1,5 +1,3 @@ -import time -start = 0 def bubble_sort(collection): """Pure implementation of bubble sort algorithm in Python @@ -8,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]) @@ -34,14 +32,12 @@ def bubble_sort(collection): if not swapped: 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 = 0 print(*bubble_sort(unsorted), sep=",") - print(time.process_time() - start) - - + print(f"Processing time: {time.process_time() - start}") From d80fc3c39a3a5a9a6eae28cbc8140f50915eecb6 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 19 Oct 2019 21:59:29 +0200 Subject: [PATCH 4/5] Update bubble_sort.py --- sorts/bubble_sort.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index a970d4774a07..0c8171c7754e 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -21,7 +21,6 @@ def bubble_sort(collection): >>> bubble_sort([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34]) True """ - start = time.process_time() length = len(collection) for i in range(length - 1): swapped = False From f800499a205378a31bfd8d917912d26b34795e37 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 19 Oct 2019 22:01:36 +0200 Subject: [PATCH 5/5] start = time.process_time() --- sorts/bubble_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index 0c8171c7754e..4faa40da1d8e 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -37,6 +37,6 @@ def bubble_sort(collection): import time user_input = input("Enter numbers separated by a comma:").strip() unsorted = [int(item) for item in user_input.split(",")] - start = 0 + start = time.process_time() print(*bubble_sort(unsorted), sep=",") print(f"Processing time: {time.process_time() - start}")