哪种算法在大 O 意义上更好?
Which algorithm is better in the big-O sense?
我有一个教授给我们的问题:
Algorithms A and B spend exactly TA(n) = 0.1n^2log10(n) and TB(n) =
2.5n^2 microseconds, respectively, for a problem of size n. Choose the algorithm, which is better in the Big-Oh sense, and find out a problem
size n0 such that for any larger size n > n0 the chosen algorithm
outperforms the other. If your problems are of the size n ≤ 10^9,
which algorithm will you recommend to use?
起初,我认为算法A在Big-Oh意义上会更好,但他的回答是A更好。我认为 A 更好的理由是它比 B 长得慢。我是对的还是我的教授是对的?
这是他的回答:
In the Big-Oh sense, the algorithm B is better. It
outperforms the algorithm A when TB(n) ≤ TA(n), that is, when 2.5n^2 ≤
0.1n^2log10(n). This inequality reduces to log10(n) ≥ 25, or n ≥ n0 = 1025. If n≤ 10^9, the algorithm of choice is A
他对还是我对?
B 在 big-o 方面更好,因为它花费的时间与 n 的平方成正比,但 A 与 n 的平方乘以更大的 log n 成正比。
所以对于足够大的 n B 值会更快。
我有一个教授给我们的问题:
Algorithms A and B spend exactly TA(n) = 0.1n^2log10(n) and TB(n) = 2.5n^2 microseconds, respectively, for a problem of size n. Choose the algorithm, which is better in the Big-Oh sense, and find out a problem size n0 such that for any larger size n > n0 the chosen algorithm outperforms the other. If your problems are of the size n ≤ 10^9, which algorithm will you recommend to use?
起初,我认为算法A在Big-Oh意义上会更好,但他的回答是A更好。我认为 A 更好的理由是它比 B 长得慢。我是对的还是我的教授是对的?
这是他的回答:
In the Big-Oh sense, the algorithm B is better. It outperforms the algorithm A when TB(n) ≤ TA(n), that is, when 2.5n^2 ≤ 0.1n^2log10(n). This inequality reduces to log10(n) ≥ 25, or n ≥ n0 = 1025. If n≤ 10^9, the algorithm of choice is A
他对还是我对?
B 在 big-o 方面更好,因为它花费的时间与 n 的平方成正比,但 A 与 n 的平方乘以更大的 log n 成正比。 所以对于足够大的 n B 值会更快。