Questions tagged [big-o]

The Big-O notation is used to represent asymptotic upper bounds. It describes relevant time or space complexity of algorithms. Big-O analysis provides a coarse and simplified estimate of a problem difficulty.

The Big-O notation is used to represent asymptotic upper-bounds. It allows a person to see if a problem will take years or seconds to compute on a modern computer.

In computer science, it is most commonly used when talking about the time complexity of algorithms, but can also refer to the storage required.

For example, a linear search on an unsorted array of size N, is O(N). If we put the elements first in a hash table, the space used is O(N) (Theta(N) to be more precise), but the search time is O(1) in the average case.

It should be noted that Big-O only represents an upper bound for a function. Therefore an O(N) function will also be O(NlogN), O(N²), O(N!), etc. In many cases Big-O is used imprecisely and Big-Theta should be used instead.

If a complexity is given by a recurrence relation, an analysis can often be carried out via the Master Theorem.

Properties

  • Summation
    O(f(n)) + O(g(n)) -> O(max(f(n), g(n)))
    For example: O(n^2) + O(n) = O(n^2)

  • Multiplication by a positive constant
    O(c * f(n)) -> O(f(n))
    For example: O(1000 * n^2) = O(n^2)

  • Multiplication
    O(f(n)) * O(g(n)) -> O(f(n) * g(n))
    For example: O(n^2) * O(n) = O(n^2 * n) = O(n^3)

  • Transitivity
    f(n) = O(g(n)) and g(n) = O(h(n)) then f(n) = O(h(n))

Groups of Big-O

Complexity Sample algorithms
O(N!) Get all permutations of N items
O(2^N) Iterating over all subsets of N items
O(N^3) Calculating all triplets from N items
O(N^2) Enumerating all pairs from N items, insert sort
O(NLog(N)) Quick sort, merge sort
O(N) Getting min, max, average, iterating over N items
O(Log(N)) Binary search
O(1) Getting an item by the index in the array

More info

6779 questions
5342
votes
43 answers

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

I'd prefer as little formal definition as possible and simple mathematics.
2690
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
  • 105,982
  • 98
  • 297
  • 360
975
votes
24 answers

Big O, how do you calculate/approximate it?

Most people with a degree in CS will certainly know what Big O stands for. It helps us to measure how well an algorithm scales. But I'm curious, how do you calculate or approximate the complexity of your algorithms?
sven
  • 18,198
  • 10
  • 51
  • 62
850
votes
19 answers

How can building a heap be O(n) time complexity?

Can someone help explain how can building a heap be O(n) complexity? Inserting an item into a heap is O(log n), and the insert is repeated n/2 times (the remainder are leaves, and can't violate the heap property). So, this means the complexity…
GBa
  • 17,509
  • 15
  • 49
  • 67
518
votes
8 answers

What is Constant Amortized Time?

What is meant by "Constant Amortized Time" when talking about time complexity of an algorithm?
VarunGupta
  • 6,127
  • 5
  • 27
  • 31
464
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
  • 17,736
  • 15
  • 72
  • 92
433
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)?
415
votes
7 answers

Determining complexity for recursive functions (Big O notation)

I have a Computer Science Midterm tomorrow and I need help determining the complexity of these recursive functions. I know how to solve simple cases, but I am still trying to learn how to solve these harder cases. These were just a few of the…
Michael_19
  • 4,799
  • 6
  • 24
  • 20
395
votes
12 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
  • 80,494
  • 45
  • 196
  • 228
389
votes
4 answers

List of Big-O for PHP functions

After using PHP for a while now, I've noticed that not all built-in PHP functions are as fast as expected. Consider these two possible implementations of a function that finds if a number is prime using a cached array of primes. //very slow for…
Kendall Hopkins
  • 43,213
  • 17
  • 66
  • 89
361
votes
33 answers

Are there any O(1/n) algorithms?

Are there any O(1/n) algorithms? Or anything else which is less than O(1)?
Shalmanese
  • 5,254
  • 10
  • 29
  • 41
321
votes
25 answers

Big-O for Eight Year Olds?

I'm asking more about what this means to my code. I understand the concepts mathematically, I just have a hard time wrapping my head around what they mean conceptually. For example, if one were to perform an O(1) operation on a data structure, I…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
270
votes
10 answers

Is log(n!) = Θ(n·log(n))?

I am to show that log(n!) = Θ(n·log(n)). A hint was given that I should show the upper bound with nn and show the lower bound with (n/2)(n/2). This does not seem all that intuitive to me. Why would that be the case? I can definitely see how to…
Mark
  • 6,123
  • 13
  • 41
  • 52
257
votes
17 answers

Append an object to a list in R in amortized constant time, O(1)?

If I have some R list mylist, you can append an item obj to it like so: mylist[[length(mylist)+1]] <- obj But surely there is some more compact way. When I was new at R, I tried writing lappend() like so: lappend <- function(lst, obj) { …
Nick
  • 21,555
  • 18
  • 47
  • 50
251
votes
23 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,708
  • 2
  • 11
  • 18
1
2 3
99 100