Algorithm Efficiency CS Thinking Example 1
Follow the full solution, then compare it with the other examples linked below.
Example 1
hardAlgorithm A takes 100 steps for an input of size 10 and 400 steps for size 20. Algorithm B takes 10 steps for size 10 and 20 steps for size 20. Which is more efficient and what might their time complexities be?
Solution
- 1 Step 1: Algorithm A: when size doubles (10โ20), steps quadruple (100โ400). This suggests .
- 2 Step 2: Algorithm B: when size doubles, steps double (10โ20). This suggests .
- 3 Step 3: Algorithm B is more efficient โ it grows linearly while A grows quadratically.
Answer
Algorithm B is more efficient: vs .
Efficiency measures how an algorithm's resource usage (time or space) grows with input size. Linear growth () is much better than quadratic () for large inputs.
About Algorithm Efficiency
The ratio of useful output energy (or power) to total input energy, expressed as a percentage โ always less than 100% due to energy losses.
Learn more about Algorithm Efficiency โMore Algorithm Efficiency Examples
Example 2 hard
Explain why binary search ([formula]) is more efficient than linear search ([formula]) for a sorted
Example 3 hardRank these time complexities from most to least efficient: [formula], [formula], [formula], [formula
Example 4 hardAlgorithm X checks every pair of students in a class of size `n`. Algorithm Y checks each student on