Question. Imagine it takes 1 second to sort 1000 elements. How long does it take to sort 10 000 elements assuming your sorting algorithm time complexity is proportional to
a) n^2
b) n log n
Answer a)
Complexity is proportional to
10,000 * 10,000 / 1000 * 1000 = 100
=> sorting 10,000 items takes 100 seconds
Answer b)
Complexity is proportional to
(10,000 * log_2 (10,000)) / (1000 * log_2 (1000))
Use the change of base rule [log_a (b) = log_c (b) / log_c (a)]
=> log_2 10,000 = log_10 (10,000) / log_10 (2)
and log_2(1000) = log_10 (1000) / log_10(2)
=> (10,000 * log_2 (10,000)) / (1000 * log_2 (1000))
= 10 * (log_10 (10,000) / log_10 (1000)) = 10 * 4/3
a) n^2
b) n log n
Answer a)
Complexity is proportional to
10,000 * 10,000 / 1000 * 1000 = 100
=> sorting 10,000 items takes 100 seconds
Answer b)
Complexity is proportional to
(10,000 * log_2 (10,000)) / (1000 * log_2 (1000))
Use the change of base rule [log_a (b) = log_c (b) / log_c (a)]
=> log_2 10,000 = log_10 (10,000) / log_10 (2)
and log_2(1000) = log_10 (1000) / log_10(2)
=> (10,000 * log_2 (10,000)) / (1000 * log_2 (1000))
= 10 * (log_10 (10,000) / log_10 (1000)) = 10 * 4/3