Compare Online Shopping Experience
Now, experience the power of algorithm efficiency! Run the provided code and compare the performance of four different algorithms (bubble sort and linear search as well as quick sort and binary search).
No coding required. Tweak a parameter, and see how they perform under different conditions. Change the parameter at the top called num_items
. (This parameter simulates how many items are in an online shopping catalog).
Run the code and compare the results and notice as we increase from 1000 to 10000, how much longer it takes one option over the other.
Imagine how much better (i.e. faster) the experience would be with one of these pair of algorithms vs the other if you were sorting a list of items by price on an online e-commerce shopping site.
This exercise is part of the course
Concepts in Computer Science
Exercise instructions
- Change the
num_items
to any number you want to compare. (ex: imagine www.amazon.com has 1,000 items, makenum_items = 1000
).
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Change this parameter
num_items = 1000
####### Leave the code below this line alone ########
catalog = [random.randint(0, num_items) for _ in range(num_items)]
total_time_bubble_linear = time_bubble_sort_and_linear_search(catalog)
catalog = [random.randint(0, num_items) for _ in range(num_items)]
total_time_quick_binary = time_quick_sort_and_binary_search(catalog)
df = pd.DataFrame({"Method": ["Bubble Sort + Linear Search", "Quick Sort + Binary Search"],
"Total Time (seconds)": [total_time_bubble_linear, total_time_quick_binary]})
print(df)