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:

8444 questions
5102
votes
42 answers

What is a plain English explanation of "Big O" notation?

I'd prefer as little formal definition as possible and simple mathematics.
4354
votes
54 answers

How do I check if an array includes a value in JavaScript?

What is the most concise and efficient way to find out if a JavaScript array contains a value? This is the only way I know to do it: function contains(a, obj) { for (var i = 0; i < a.length; i++) { if (a[i] === obj) { return…
brad
  • 67,670
  • 21
  • 67
  • 84
2322
votes
32 answers

What does O(log n) mean exactly?

I am learning about Big O Notation running times and amortized times. I understand the notion of O(n) linear time, meaning that the size of the input affects the growth of the algorithm proportionally...and the same goes for, for example, quadratic…
Andreas Grech
  • 98,643
  • 98
  • 284
  • 354
1417
votes
29 answers

How can you profile a Python script?

Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With Python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to __main__. What is a good…
Chris Lawlor
  • 41,304
  • 11
  • 45
  • 67
957
votes
9 answers

How to find time complexity of an algorithm

The Question How to find time complexity of an algorithm? What have I done before posting a question on SO ? I have gone through this, this and many other links But no where I was able to find a clear and straight forward explanation for how to…
Yasser Shaikh
  • 44,064
  • 44
  • 190
  • 271
457
votes
5 answers

If strings are immutable in .NET, then why does Substring take O(n) time?

Given that strings are immutable in .NET, I'm wondering why they have been designed such that string.Substring() takes O(substring.Length) time, instead of O(1)? i.e. what were the tradeoffs, if any?
user541686
  • 189,354
  • 112
  • 476
  • 821
435
votes
9 answers

What is the difference between Θ(n) and O(n)?

Sometimes I see Θ(n) with the strange Θ symbol with something in the middle of it, and sometimes just O(n). Is it just laziness of typing because nobody knows how to type this symbol, or does it mean something different?
martinus
  • 16,710
  • 15
  • 65
  • 90
370
votes
5 answers

Difference between Big-O and Little-O Notation

What is the difference between Big-O notation O(n) and Little-O notation o(n)?
354
votes
11 answers

Computational complexity of Fibonacci Sequence

I understand Big-O notation, but I don't know how to calculate it for many functions. In particular, I've been trying to figure out the computational complexity of the naive version of the Fibonacci sequence: int Fibonacci(int n) { if (n <= 1) …
Juliet
  • 76,873
  • 44
  • 191
  • 224
245
votes
22 answers

Are there any cases where you would prefer a higher big-O time complexity algorithm over the lower one?

Are there are any cases where you would prefer O(log n) time complexity to O(1) time complexity? Or O(n) to O(log n)? Do you have any examples?
V.Leymarie
  • 2,638
  • 2
  • 9
  • 16
179
votes
5 answers

Are 2^n and n*2^n in the same time complexity?

Resources I've found on time complexity are unclear about when it is okay to ignore terms in a time complexity equation, specifically with non-polynomial examples. It's clear to me that given something of the form n2 + n + 1, the last two terms are…
matty-d
  • 2,533
  • 3
  • 15
  • 21
168
votes
15 answers

Is a Java hashmap search really O(1)?

I've seen some interesting claims on SO re Java hashmaps and their O(1) lookup time. Can someone explain why this is so? Unless these hashmaps are vastly different from any of the hashing algorithms I was bought up on, there must always exist a…
paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841
150
votes
8 answers

Why is the time complexity of both DFS and BFS O( V + E )

The basic algorithm for BFS: set start vertex to visited load it into queue while queue not empty for each edge incident to vertex if its not visited load into queue mark vertex So I would think the time…
ordinary
  • 5,155
  • 10
  • 40
  • 58
147
votes
14 answers

how to calculate binary search complexity

I heard somebody say that since binary search halves the input required to search hence it is log(n) algorithm. Since I am not from a mathematics background I am not able to relate to it. Can somebody explain it in a little more detail? Does it have…
Bunny Rabbit
  • 7,604
  • 15
  • 55
  • 100
127
votes
19 answers

Maximum single-sell profit

Suppose we are given an array of n integers representing stock prices on a single day. We want to find a pair (buyDay, sellDay), with buyDay ≤ sellDay, such that if we bought the stock on buyDay and sold it on sellDay, we would maximize our…
Ajeet Ganga
  • 7,592
  • 10
  • 53
  • 72
1
2 3
99 100