Why a programmer would prefer O(N^3) instead of O(N^2)

前端 未结 7 1524
面向向阳花
面向向阳花 2021-01-07 16:57

I was studying for my final exam and there is a question in the archive that I cannot find its answer:

The order-of-growth of the running time of one

相关标签:
7条回答
  • 2021-01-07 17:10

    another thing is, some algorithms have a big constant factor. a O (N^2) might have a big constant factor that won't make it really practical to use ( if N is small enough as kindly noted by Thorban)

    0 讨论(0)
  • 2021-01-07 17:13

    Adding to the already posted answers I'd like to mention cache behaviour. A particular memory access pattern might be so much slower due to repeated cache misses that a theoretically slower algorithm with a more cache friendly memory access pattern performs much better.

    0 讨论(0)
  • 2021-01-07 17:24

    Here is are examples to convince you that O(N³) can be in some cases better than O(N²).

    1. O(N²) algorithm is very complex to code whereas if input size is say N ≤ 100 then for practical use O(N³) can be fast enough

    2. O(N²) has a large constant multiplied to it for example c = 1000 hence for N = 100, c⋅N² = 1000⋅100² = 10⁷ whereas if c = 1 for O(N³) then c⋅N³ = 10⁶

    3. O(N²) algorithm has very high space complexity as compared to O(N³)

    0 讨论(0)
  • 2021-01-07 17:25

    The only reason for choosing one algorithm is not the order-of-growth of the running time. You must analyze:

    • How easy it is to implement
    • The space required
    • How big are the real life inputs (sometimes the time difference is only observable for input sizes that never occurs in reality)
    0 讨论(0)
  • 2021-01-07 17:28

    I can think of the following three reasons:

    • Ease of initial implementation.
    • Ease of maintenance in the future.
    • The O(N^3) algorithm may have a lower space complexity than the O(N^2) algorithm (i.e., it uses less memory).
    0 讨论(0)
  • 2021-01-07 17:28

    Probably the #1 reason: because the O(N2) algorithm has enough higher constants that for the size of task being contemplated, the O(N3) version is faster.

    0 讨论(0)
提交回复
热议问题