smallest elements from a list of length
Strategy
sorts the list in ascending order, which takes time
then removes the first
elements. Strategy
repeatedly finds the smallest element by scanning the list and removing it until
elements are removed. The time taken to compare two elements is
and the time taken to remove an element is
All other operations take zero time. Prove that strategy
is faster.
-
- Evaluate the sum
- Peter needs to deliver
items to his
friends’ houses. He takes
minutes to go from his house to any of his friends’ houses, but
minutes to go from the
th house to the
th house. If he starts from his house, how fast can he deliver all
items?
- Evaluate the sum
-
Find the total time taken for strategy
-
… by considering the time taken for each step involved?
-
In the worst case, what is the least number of comparisons required to find the smallest element of a list?
-
What is the total time taken to find and remove this element from a list of length
-
What happens to the length of the list after each removal?
-
… and consider that to compute the total time taken for strategy
-
Try to identify a (useful) upper bound for the time taken for strategy
-
… by considering the range of values of
and the expression for strategy
-
To execute strategy
first sort the list in time
and then remove the first
elements, which takes time
So the total time taken is
For strategy
to remove the
smallest elements, we need to scan through the list, look for the smallest element and remove it, then repeat this process
times. For a list of length
finding and removing the smallest element takes time
, as we perform
comparisons to find it then
removal. So the overall time taken for strategy
is
To prove that strategy
is faster, we must show that
for all
and all
We know that
so an immediate upper bound for the left-hand side is
which is indeed smaller than