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
54
votes
8 answers

Why is it impossible to find a specified value in a sorted array faster than O(log n)?

I'm pretty new to computer science. In the closing to a lecture, my AP Computer Science teacher mentioned the comparison model for finding a specified value in a sorted array is big omega (log n) i.e. Ω(log n), which as I understand it, means it’s…
Kapernski
  • 709
  • 4
  • 7
54
votes
26 answers

Find the majority element in array

The majority element is the element that occurs more than half of the size of the array. How to find the majority element in an array in O(n)? Example input: {2,1,2,3,4,2,1,2,2} Expected output: 2
Ali Tarhini
  • 5,278
  • 6
  • 41
  • 66
53
votes
7 answers

Why is the Big-O complexity of this algorithm O(n^2)?

I know the big-O complexity of this algorithm is O(n^2), but I cannot understand why. int sum = 0; int i = 1; j = n * n; while (i++ < j--) sum++; Even though we set j = n * n at the beginning, we increment i and decrement j during each…
53
votes
5 answers

Big-O complexity of a piece of code

I have a question in algorithm design about complexity. In this question a piece of code is given and I should calculate this code's complexity. The pseudo-code is: for(i=1;i<=n;i++){ j=i do{ k=j; j = j / 2; }while(k is…
Behzad Hassani
  • 2,129
  • 4
  • 30
  • 51
52
votes
3 answers

What's the time complexity of array.splice() in Google Chrome?

If I remove one element from an array using splice() like so: arr.splice(i, 1); Will this be O(n) in the worst case because it shifts all the elements after i? Or is it constant time, with some linked list magic underneath?
Ivan
  • 2,314
  • 3
  • 18
  • 22
52
votes
6 answers

When can an algorithm have square root(n) time complexity?

Can someone give me example of an algorithm that has square root(n) time complexity. What does square root time complexity even mean?
vaibhav9225
  • 523
  • 1
  • 4
  • 6
52
votes
7 answers

How do I find the median of numbers in linear time using heaps?

Wikipedia says: Selection algorithms: Finding the min, max, both the min and max, median, or even the k-th largest element can be done in linear time using heaps. All it says is that it can be done, and not how. Can you give me some start on how…
Lazer
  • 90,700
  • 113
  • 281
  • 364
52
votes
2 answers

Why are difference lists more efficient than regular concatenation in Haskell?

I am currently working my way through the Learn you a Haskell book online, and have come to a chapter where the author is explaining that some list concatenations can be inefficient: For example ((((a ++ b) ++ c) ++ d) ++ e) ++ f is supposedly…
Craig Innes
  • 1,573
  • 11
  • 23
51
votes
2 answers

Python collections.Counter: most_common complexity

What is the complexity of the function most_common provided by the collections.Counter object in Python? More specifically, is Counter keeping some kind of sorted list while it's counting, allowing it to perform the most_common operation faster than…
Romain G
  • 1,276
  • 1
  • 15
  • 27
50
votes
7 answers

What is the time complexity of std::sort() in the C++ standard library?

What is the complexity of std::sort() in the C++ Standard Library? Which sort is applied? Is there any rule of applying any particular sorting algorithm there?
Hari Chaudhary
  • 501
  • 1
  • 4
  • 3
49
votes
24 answers

Worse is better. Is there an example?

Is there a widely-used algorithm that has time complexity worse than that of another known algorithm but it is a better choice in all practical situations (worse complexity but better otherwise)? An acceptable answer might be in a form: There are…
jfs
  • 399,953
  • 195
  • 994
  • 1,670
48
votes
4 answers

Time complexity of contains(Object o), in an ArrayList of Objects

As the title says, I was wondering what the time complexity of the contains() method of an ArrayList is.
Samuel
  • 18,286
  • 18
  • 52
  • 88
48
votes
1 answer

How does redis claim O(1) time for key lookup?

I have a question - lookup of a key value pair in an index - let's say on cassandra or postgres - is typically at around O(logn) source: https://github.com/tinkerpop/blueprints/wiki/Graph-Indices. In the redis documentation it states that runtime…
JasonG
  • 5,794
  • 4
  • 39
  • 67
48
votes
4 answers

Priority Queue remove complexity time

What is the complexity (big-oh) for the remove() function on the Priority Queue class in Java? I can't find anything documented anywhere, I think it's O(n), considering you have to find the element before you remove it and then reshuffle the tree.…
samturner
  • 2,213
  • 5
  • 25
  • 31
48
votes
7 answers

Merge sort time and space complexity

Let's take this implementation of Merge Sort as an example void mergesort(Item a[], int l, int r) { if (r <= l) return; int m = (r+l)/2; mergesort(a, l, m); ------------(1) mergesort(a, m+1, r); ------------(2) merge(a, l, m, r); a) The time…
Frank Q.
  • 6,001
  • 11
  • 47
  • 62