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

Differences between time complexity and space complexity?

I have seen that in most cases the time complexity is related to the space complexity and vice versa. For example in an array traversal: for i=1 to length(v) print (v[i]) endfor Here it is easy to see that the algorithm complexity in terms of…
Little
  • 3,363
  • 10
  • 45
  • 74
56
votes
24 answers

O(log N) == O(1) - Why not?

Whenever I consider algorithms/data structures I tend to replace the log(N) parts by constants. Oh, I know log(N) diverges - but does it matter in real world applications? log(infinity) < 100 for all practical purposes. I am really curious for…
phoku
  • 2,082
  • 1
  • 18
  • 16
55
votes
5 answers

What is the runtime complexity of a switch statement?

I'd like to know what the worst-case runtime complexity of a switch statement is, assuming you have n cases. I always assumed it was O(n). I don't know if compilers do anything clever, though. If the answer is implementation-specific, I'd like to…
Fragsworth
  • 33,919
  • 27
  • 84
  • 97
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
54
votes
4 answers

Stack with find-min/find-max more efficient than O(n)?

I am interested in creating a Java data structure similar to a stack that supports the following operations as efficiently as possible: Push, which adds a new element atop the stack, Pop, which removes the top element of the stack, Find-Max, which…
Techkriti
  • 541
  • 1
  • 5
  • 3
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
1 answer

Could anyone explain Big O versus Big Omega vs Big Theta?

Possible Duplicate: Big Theta Notation - what exactly does big Theta represent? I understand it in theory, I guess, but what I'm having trouble grasping is the application of the three. In school, we always used Big O to denote the complexity of…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
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
6 answers

Quicksort superiority over Heap Sort

Heap Sort has a worst case complexity of O(nlogn) while Quicksort has O(n^2). But emperical evidences say quicksort is superior. Why is that?
Nitish Upreti
  • 6,312
  • 9
  • 50
  • 92
52
votes
5 answers

Why is the computational complexity O(n^4)?

int sum = 0; for(int i = 1; i < n; i++) { for(int j = 1; j < i * i; j++) { if(j % i == 0) { for(int k = 0; k < j; k++) { sum++; } } } } I don't understand how when j = i, 2i, 3i... the…
user11452926
  • 641
  • 5
  • 9
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

Which algorithm is faster O(N) or O(2N)?

Talking about Big O notations, if one algorithm time complexity is O(N) and other's is O(2N), which one is faster?
deepdive
  • 9,720
  • 3
  • 30
  • 38
51
votes
4 answers

Levenshtein Distance Algorithm better than O(n*m)?

I have been looking for an advanced levenshtein distance algorithm, and the best I have found so far is O(n*m) where n and m are the lengths of the two strings. The reason why the algorithm is at this scale is because of space, not time, with the…
Jason
  • 14,517
  • 25
  • 92
  • 153