@@ -106,28 +106,25 @@ def bubble_sort_recursive(collection: list[Any]) -> list[Any]:
106
106
107
107
if __name__ == "__main__" :
108
108
import doctest
109
+ from random import sample
109
110
from timeit import timeit
110
111
111
112
doctest .testmod ()
112
113
113
- user_input = input ("Enter numbers separated by a comma:" ).strip ()
114
- unsorted = [
115
- int (item ) if isinstance (item , int ) else float (item )
116
- for item in user_input .split ("," )
117
- ]
118
-
119
- num_runs = 1000
114
+ # Benchmark: Iterative seems slightly faster than recursive.
115
+ num_runs = 10_000
116
+ unsorted = sample (range (- 50 , 50 ), 100 )
120
117
timer_iterative = timeit (
121
118
"bubble_sort_iterative(unsorted[:])" , globals = globals (), number = num_runs
122
119
)
123
- timer_recursive = timeit (
124
- "bubble_sort_recursive(unsorted[:])" , globals = globals (), number = num_runs
125
- )
126
-
127
120
print ("\n Iterative bubble sort:" )
128
121
print (* bubble_sort_iterative (unsorted ), sep = "," )
129
- print (f"Processing time (iterative): { timer_iterative :.5f} s for { num_runs } runs" )
122
+ print (f"Processing time (iterative): { timer_iterative :.5f} s for { num_runs :, } runs" )
130
123
124
+ unsorted = sample (range (- 50 , 50 ), 100 )
125
+ timer_recursive = timeit (
126
+ "bubble_sort_recursive(unsorted[:])" , globals = globals (), number = num_runs
127
+ )
131
128
print ("\n Recursive bubble sort:" )
132
129
print (* bubble_sort_recursive (unsorted ), sep = "," )
133
- print (f"Processing time (recursive): { timer_recursive :.5f} s for { num_runs } runs" )
130
+ print (f"Processing time (recursive): { timer_recursive :.5f} s for { num_runs :, } runs" )
0 commit comments