this post was submitted on 22 Jun 2024
404 points (97.4% liked)
Programmer Humor
19564 readers
513 users here now
Welcome to Programmer Humor!
This is a place where you can post jokes, memes, humor, etc. related to programming!
For sharing awful code theres also Programming Horror.
Rules
- Keep content in english
- No advertisements
- Posts must be related to programming or programmer topics
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
A quadratic function is just one possible polynomial. They're also not really related to big-O complexity, where you mostly just care about what the highest exponent is:
O(n^2) vs O(n^3)
.For most short programs it's fairly easy to determine the complexity. Just count how many nested loops you have. If there's no loops, it's probably
O(1)
unless you're calling other functions that hide the complexity.If there's one loop that runs N times, it's
O(n)
, and if you have a nested loop, it's likelyO(n^2)
.You throw out any constant-time portion, so your function's actual runtime might be the polynomial:
5n^3 + 2n^2 + 6n + 20
. But the big-O notation would simply beO(n^3)
in that case.I'm simplifying a little, but that's the overview. I think a lot of people just memorize that certain algorithms have a certain complexity, like binary search being
O(log n)
for example.Spot on. Good explanation.