Algorithm Efficiency CS Thinking Example 3

Follow the full solution, then compare it with the other examples linked below.

Example 3

hard
Rank these time complexities from most to least efficient: O(n2)O(n^2), O(1)O(1), O(nlogโกn)O(n \log n), O(n)O(n), O(2n)O(2^n).

Solution

  1. 1
    Step 1: O(1)O(1) is constant โ€” best. Then O(n)O(n) linear. Then O(nlogโกn)O(n \log n). Then O(n2)O(n^2) quadratic. Then O(2n)O(2^n) exponential โ€” worst.
  2. 2
    Step 2: Ranking: O(1)<O(n)<O(nlogโกn)<O(n2)<O(2n)O(1) < O(n) < O(n \log n) < O(n^2) < O(2^n).

Answer

O(1),O(n),O(nlogโกn),O(n2),O(2n)O(1), O(n), O(n \log n), O(n^2), O(2^n) โ€” from most to least efficient.
Understanding the hierarchy of time complexities helps us evaluate and choose algorithms. Exponential algorithms become impractical very quickly as input grows.

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