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
104
votes
2 answers

What are the time complexities of various data structures?

I am trying to list time complexities of operations of common data structures like Arrays, Binary Search Tree, Heap, Linked List, etc. and especially I am referring to Java. They are very common, but I guess some of us are not 100% confident about…
Bhushan
  • 18,329
  • 31
  • 104
  • 137
101
votes
4 answers

What is the time complexity of my function?

Started studying about complexity, I'm struggling with this one: void what(int n) { int i; for (i = 1; i <= n; i++) { int x = n; while (x > 0) x -= i; } } Well, the first for loop is clearly O(n). The first…
Ilan Aizelman WS
  • 1,630
  • 2
  • 21
  • 44
95
votes
2 answers

What is time complexity of a list to set conversion?

I've noticed the table of the time complexity of set operations on the python official website. But i just wanna ask what's the time complexity of converting a list to a set, for instance, l = [1, 2, 3, 4, 5] s = set(l) I kind of know that this is…
lxuechen
  • 1,129
  • 1
  • 9
  • 9
90
votes
5 answers

Worst case in Max-Heapify - How do you get 2n/3?

In CLRS, third Edition, on page 155, it is given that in MAX-HEAPIFY, The children’s subtrees each have size at most 2n/3—the worst case occurs when the bottom level of the tree is exactly half full. I understand why it is worst when the bottom…
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
90
votes
4 answers

JavaScript runtime complexity of Array functions

Is the runtime complexity defined by the JS standard on common Array functions like push, pop, shift, slice or splice? Esp. I'm interested in removing and inserting entries at random positions. If the complexity is not defined, what can I expect,…
Albert
  • 65,406
  • 61
  • 242
  • 386
88
votes
12 answers

Sorting in Computer Science vs. sorting in the 'real' world

I was thinking about sorting algorithms in software, and possible ways one could surmount the O(nlogn) roadblock. I don't think it IS possible to sort faster in a practical sense, so please don't think that I do. With that said, it seems with almost…
Mmm Donuts
  • 9,551
  • 6
  • 27
  • 49
87
votes
8 answers

What is the time complexity of the sleep sort?

Given this sort algorithm how do you express its time complexity? Originally presented here (partial archive). #!/bin/bash function f() { sleep "$1" echo "$1" } while [ -n "$1" ] do f "$1" & shift done wait example usage: ./sleepsort.bash 5…
justinhj
  • 11,147
  • 11
  • 58
  • 104
80
votes
8 answers

Is complexity O(log(n)) equivalent to O(sqrt(n))?

My professor just taught us that any operation that halves the length of the input has an O(log(n)) complexity as a thumb rule. Why is it not O(sqrt(n)), aren't both of them equivalent?
white_tree
  • 978
  • 1
  • 11
  • 11
79
votes
3 answers

Why is the time complexity of Python's list.append() method O(1)?

As seen in the documentation for TimeComplexity, Python's list type is implemented using an array. So if an array is being used and we do a few appends, eventually you will have to reallocate space and copy all the information to the new…
ohad edelstain
  • 1,425
  • 2
  • 14
  • 22
77
votes
17 answers

nth fibonacci number in sublinear time

Is there any algorithm to compute the nth fibonacci number in sub linear time?
Biswajyoti Das
  • 7,881
  • 11
  • 34
  • 26
76
votes
6 answers

Time complexity for java ArrayList

Is ArrayList an array or a list in java? what is the time complexity for the get operation, is it O(n) or O(1)?
Hidayat
  • 995
  • 2
  • 7
  • 7
75
votes
8 answers

Complexities of binary tree traversals

What is the time complexity of inorder,postorder and preorder traversal of binary trees in data structures?? Is it O(n) or O(log n) or O(n^2)??
Mishthi
  • 1,101
  • 3
  • 12
  • 13
73
votes
11 answers

Why is O(n) better than O( nlog(n) )?

I just came around this weird discovery, in normal maths, n*logn would be lesser than n, because log n is usually less than 1. So why is O(nlog(n)) greater than O(n)? (ie why is nlogn considered to take more time than n) Does Big-O follow a…
Ritveak
  • 2,930
  • 2
  • 13
  • 28
72
votes
7 answers

Lazy Evaluation and Time Complexity

I was looking around stackoverflow Non-Trivial Lazy Evaluation, which led me to Keegan McAllister's presentation: Why learn Haskell. In slide 8, he shows the minimum function, defined as: minimum = head . sort and states that its complexity is…
leco
  • 1,989
  • 16
  • 30
69
votes
4 answers

A better way for a Python 'for' loop

We all know that the common way of executing a statement a certain number of times in Python is to use a for loop. The general way of doing this is, # I am assuming iterated list is redundant. # Just the number of execution matters. for _ in…
Max Paython
  • 1,595
  • 2
  • 13
  • 25