3 ms·
I'll try my hand at a "plain english" explanation of an O(n^2) algorithm. It's a function which takes a list of numbers as inputs. You give it a list of two n
by sillysaurus2 13y ago
I'll try my hand at a "plain english" explanation of an O(n^2) algorithm.
It's a function which takes a list of numbers as inputs. You give it a list of two numbers. It executes four operations before terminating.
You give it a list of three numbers. It executes nine operations before terminating.
You give it a list of four numbers. It executes sixteen operations before terminating.
You see how the time required grows as n^2 with respect to the input? That's an O(n^2) algorithm, and that's why such an algorithm can be bad. A list of 1,000,000 numbers would require a trillion operations.
The way this can happen is if you write a double-for loop:
for x in input:
for y in input:
...
So, there you go. You probably won't get any plainer of an explanation without becoming oversimplified and losing meaning.
- adsche 13y agoIt's plain, I'll give you that. And it gives you an idea. But it's not an explanation as I think it already lost meaning. The explanation is closer to o(n^2) (small o) or maybe Theta(n^2) but still lost the important aspect of only being bound asymptotically.