Yes, this algorithm is O (n ^ 2).
To calculate the complexity, we have a table of complexity:
O (1) O (log n) On) O (n log n)
O (N²) O (n ^ a) O (a ^ n) O (n!)
Each line is a set of algorithms. The set of algorithms that is in O (1) is also in O (n) and O (n ^ 2), etc. But not in reverse order. So your algorithm implements n * n / 2 sentences.
O (n) O (nlogn) O (n * n / 2) O (N²)
So the set of algorithms that include the complexity of your algorithm is O (n²), since O (n) and O (nlogn) are smaller.
For example: For n = 100, the sum = 5000. => 100 O (n) 200o (n * logn) 5000 (n * n / 2) 10000 (n ^ 2)
Sorry for my english.
Francisco Manuel Garca Botella Nov 22 '15 at 19:45 2015-11-22 19:45
source share