Big-O Crossover Calculator

When does c₁·n log n beat c₂·n²? Crossover points, log-log chart and run times.

Cost functions (required)
Cost functions
#NameGrowthConstant ck (for n^k)
1
2
3
4
More options

For converting cost into time; 10^9 is a rough figure for simple operations on one CPU core.

Try an example: ·

More about this tool

Part of Proofline Math & science

How it works

Compares cost functions such as 50·n log n against 2·n² or 2ⁿ with your constant factors, finds every input size where one overtakes another, draws them on a log-log chart, and converts operation counts into running time at a given number of operations per second.

  • Each cost is c·f(n) with log₂ for logarithms; n! is evaluated as Γ(n + 1), so every function is continuous in n. All comparisons are done on log10 of the cost, so 2ⁿ or n! for large n never overflow.
  • Crossovers: the difference of the log costs is sampled at 2,000 log-spaced sizes between the smallest and largest n; every sign change is refined by 80 bisection steps. Crossovers closer together than the grid spacing can be missed.
  • Time = cost ÷ operations per second. Real programs also depend on memory access, caches and constant overheads that a single constant c only approximates.

Sources

  • Knuth, D. E. (1976): Big Omicron and big Omega and big Theta (SIGACT News 8(2))

Use this tool in an app

These free programs for your browser have this tool built in, next to everything else the task needs.

  • Proofline Math notebook

    Solve, graph, test and visualise in one notebook — with data, findings and a report.

    Open Proofline

Related tools

How to use the Big-O Crossover Calculator

Find the input size where an n log n algorithm really beats an n² one once constant factors count, and turn operation counts into running times.

What it is for

Big-O notation says how cost grows, but it hides the constant factors. That's why real sorting libraries switch to insertion sort for small arrays even though it's O(n²). Below some size the "worse" algorithm is cheaper. This calculator makes that concrete. You enter two or more cost functions with constants, such as 6·n log n against 0.5·n². It finds every input size where one overtakes another, draws the curves on a log-log chart, and converts operation counts into time at an assumed speed.

It's a model, not a benchmark. One constant per algorithm can't capture caches, memory allocation or branch prediction. Measure on your own hardware for real decisions, and use this to understand the shape of the trade-off and to sanity-check your numbers.

Worked example

Default example: Merge sort 6·n log₂ n against Insertion sort 0.5·n², at 10⁹ operations per second.

Result: "Merge sort becomes cheaper than Insertion sort from n ≈ 74.7." The first whole size where merge sort wins is 75.

Worked example
nMerge sortInsertion sort
10199 ops, 199 ns50 ops, 50.0 ns
1003.99 µs5.00 µs
10,000797 µs50.0 ms
1,000,000120 ms8.33 min

At 10 elements insertion sort is four times cheaper. At a million it's the difference between a blink and a coffee break.

The second example compares brute force 2ⁿ, DP 100·n³ and a "fancy" 10⁶·n^2.373. DP wins from n ≈ 19.5 and the fancy algorithm beats brute force from n ≈ 31.8, but at n = 1,000 the fancy one still needs 3.65 h against DP's 100 s. Its huge constant only pays off far beyond the table. Brute force at n = 100 would take about 10^13.6 years.

How to read the result

  • Crossovers give the exact n (refined by bisection), the first whole n and which function is cheaper above it. Several crossovers are possible when curves cross more than once.
  • Cost table: operations and time for each size in your list. Very large counts are shown as powers of ten.
  • Log-log chart: straight lines are polynomials, and the slope is the exponent. Exponential curves bend upwards. Costs above 10⁴⁰ are left out of the chart so they don't flatten everything else.
  • Cheapest at max n tells you who wins at the top of your range.

Tips and common mistakes

  • Get constants from measurements. Time both implementations at one size, divide by f(n), and use the ratio. Guessing constants makes the crossover meaningless.
  • Logarithms are base 2. If your analysis uses natural logs, multiply the constant by about 0.69.
  • Set the n range to sizes you actually meet. A crossover at n = 10¹² doesn't matter for an array of 10,000 items.
  • The search samples 2,000 points between n min and n max. Two crossovers very close together can be missed, so narrow the range if the curves look tangled.
  • 10⁹ simple operations per second is a rough single-core figure. For disk or network-bound work, one "operation" may take microseconds or milliseconds, so set the speed accordingly.

FAQ

Why does an O(n²) algorithm win for small n?

Because constants dominate when n is small. Insertion sort does very little work per step, while merge sort pays for recursion and copying. Hybrid sorts use exactly this crossover.

Which complexity classes are available?

Constant, log₂ n, √n, n, n log₂ n, n², n³, a custom power n^k, 2ⁿ and n!, each with its own constant. n! is extended to non-integer n with the gamma function.

Can I trust the time column?

Only as an order of magnitude. It's operations divided by your operations-per-second figure. Real time also depends on memory access and overhead.

What does "no crossover" mean?

The summary then says, for example, "No crossover between n = 2 and 50; B is cheapest throughout." One function wins over the whole range. Widen the range if you expect them to cross.

Guide last updated:

For scripts and AI agents

  • JSON API: POST https://bigocrossovercalculator.usefulweb.site/api/v1/calculate (same calculation as this page)
  • Interface description: OpenAPI 3.1 · AI guide: llms.txt
  • MCP server (Streamable HTTP): https://bigocrossovercalculator.usefulweb.site/mcp · tool name big_o_crossover_calculator
Example request
curl -s -X POST https://bigocrossovercalculator.usefulweb.site/api/v1/calculate \
  -H 'content-type: application/json' \
  -d '{"functions":[{"label":"Merge sort","complexity":"nlogn","constant":6},{"label":"Insertion sort","complexity":"n2","constant":0.5}]}'
Feedback · ideas & bug reports welcome