Questions tagged [time-complexity]

The time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the size of the input to the problem. The time complexity of an algorithm is commonly expressed using big O notation, which suppresses multiplicative constants and lower order terms.

In computer science, the time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the size of the input to the problem. The time complexity of an algorithm is commonly expressed using big O notation, which suppresses multiplicative constants and lower order terms.

When expressed this way, the time complexity is said to be described asymptotically, i.e., as the input size goes to infinity. For example, if the time required by an algorithm on all inputs of size n is at most 5n^3 + 3n, the asymptotic time complexity is O(n^3).

Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm, where an elementary operation takes a fixed amount of time to perform. Thus the amount of time taken and the number of elementary operations performed by the algorithm differ by at most a constant factor.

Since an algorithm may take a different amount of time even on inputs of the same size, the most commonly used measure of time complexity, the worst-case time complexity of an algorithm, denoted as T(n), is the maximum amount of time taken on any input of size n. Time complexities are classified by the nature of the function T(n).

For instance, an algorithm with T(n) = O(n) is called a linear time algorithm, and an algorithm with T(n) = O(2^n) is said to be an exponential time algorithm.

Useful links:

Related tags:

10064 questions
68
votes
4 answers

What is the time complexity of HashMap.containsKey() in java?

I need to know: What is the time complexity of HashMap.containsKey() in java?
Hossein
  • 40,161
  • 57
  • 141
  • 175
68
votes
8 answers

Which is better: O(n log n) or O(n^2)

Okay so I have this project I have to do, but I just don't understand it. The thing is, I have 2 algorithms. O(n^2) and O(n*log2n). Anyway, I find out in the project info that if n<100, then O(n^2) is more efficient, but if n>=100, then O(n*log2n)…
user3579272
  • 683
  • 1
  • 6
  • 6
66
votes
9 answers

Are algorithms with high time complexity ever used in the real world for small inputs?

Let's say we have a problem where a certain algorithm, let's call it algorithm_1, solves it in time complexity of O(n^2) and another algorithm, let's call it algorithm_2, solves it in time complexity O(n), but in reality we see that for n < 1000…
Ak2399
  • 827
  • 3
  • 6
66
votes
10 answers

Time complexity of nested for-loop

I need to calculate the time complexity of the following code: for (i = 1; i <= n; i++) { for(j = 1; j <= i; j++) { // Some code } } Is it O(n^2)?
yyy
  • 912
  • 1
  • 9
  • 13
63
votes
7 answers

Is there an O(n) integer sorting algorithm?

The last week I stumbled over this paper where the authors mention on the second page: Note that this yields a linear running time for integer edge weights. The same on the third page: This yields a linear running time for integer edge weights…
Karussell
  • 17,085
  • 16
  • 97
  • 197
63
votes
4 answers

Example of a factorial time algorithm O( n! )

I'm studying time complexity in school and our main focus seems to be on polynomial time O(n^c) algorithms and quasi-linear time O(nlog(n)) algorithms with the occasional exponential time O(c^n) algorithm as an example of run-time perspective.…
recursion.ninja
  • 5,377
  • 7
  • 46
  • 78
62
votes
6 answers

Can an O(n) algorithm ever exceed O(n^2) in terms of computation time?

Assume I have two algorithms: for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { //do something in constant time } } This is naturally O(n^2). Suppose I also have: for (int i = 0; i < 100; i++) { for (int j = 0; j < n; j++) { …
Brian
  • 7,098
  • 15
  • 56
  • 73
61
votes
2 answers

Object.keys() complexity?

Anyone know the time-complexity of ECMAScript5's Object.keys() in common implementations? Is it O(n) for n keys? Is time proportional to the size of the hash table, assuming a hash implementation? I'm looking for either guarantees by language…
hurrymaplelad
  • 26,645
  • 10
  • 56
  • 76
60
votes
2 answers

What is O(log(n!)), O(n!), and Stirling's approximation?

What is O(log(n!)) and O(n!)? I believe it is O(n log(n)) and O(n^n)? Why? I think it has to do with Stirling's approximation, but I don't get the explanation very well. Am I wrong about O(log(n!) = O(n log(n))? How can the math be explained in…
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
59
votes
2 answers

What is the time complexity of a size() call on a LinkedList in Java?

As the title asks, I wonder if the size() method in the LinkedList class takes amortized O(1) time or O(n) time.
Martin Andersson
  • 1,801
  • 4
  • 21
  • 25
58
votes
5 answers

Time complexity of memory allocation

What is the time complexity of dynamic memory allocation using new, malloc, etc.? I know very little about how memory allocators are implemented, but I assume the answer is that it depends on the implementation. Therefore, please answer for some…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
55
votes
7 answers

Complexity of len() with regard to sets and lists

The complexity of len() with regards to sets and lists is equally O(1). How come it takes more time to process sets? ~$ python -m timeit "a=[1,2,3,4,5,6,7,8,9,10];len(a)" 10000000 loops, best of 3: 0.168 usec per loop ~$ python -m timeit…
Omid
  • 2,617
  • 4
  • 28
  • 43
55
votes
3 answers

Finding Big O of the Harmonic Series

Prove that 1 + 1/2 + 1/3 + ... + 1/n is O(log n). Assume n = 2^k I put the series into the summation, but I have no idea how to tackle this problem. Any help is appreciated
user2092408
  • 553
  • 1
  • 5
  • 4
55
votes
5 answers

O(n log n) vs O(n) -- practical differences in time complexity

n log n > n -- but this is like a pseudo-linear relationship. If n=1 billion, log n ~ 30; So n log n will be 30 billion, which is 30 X n, order of n. I am wondering if this time complexity difference between n log n and n are significant in real…
brain storm
  • 30,124
  • 69
  • 225
  • 393
55
votes
8 answers

Detecting if a string has unique characters: comparing my solution to "Cracking the Coding Interview?"

I am working through the book "Cracking the Coding Interview" and I have come across questions here asking for answers, but I need help comparing my answer to the solution. My algorithm works, but I am having difficulty understand the solution in…
Seephor
  • 1,692
  • 3
  • 28
  • 50