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
234
votes
32 answers

How to find the kth largest element in an unsorted array of length n in O(n)?

I believe there's a way to find the kth largest element in an unsorted array of length n in O(n). Or perhaps it's "expected" O(n) or something. How can we do this?
MrDatabase
  • 43,245
  • 41
  • 111
  • 153
215
votes
7 answers

What exactly does big Ө notation represent?

I'm really confused about the differences between big O, big Omega, and big Theta notation. I understand that big O is the upper bound and big Omega is the lower bound, but what exactly does big Ө (theta) represent? I have read that it means tight…
user1364768
  • 2,213
  • 3
  • 16
  • 8
201
votes
26 answers

Are there any worse sorting algorithms than Bogosort (a.k.a Monkey Sort)?

My co-workers took me back in time to my University days with a discussion of sorting algorithms this morning. We reminisced about our favorites like StupidSort, and one of us was sure we had seen a sort algorithm that was O(n!). That got me…
womp
  • 115,835
  • 26
  • 236
  • 269
193
votes
4 answers

Big-O summary for Java Collections Framework implementations?

I may be teaching a "Java crash-course" soon. While it is probably safe to assume that the audience members will know Big-O notation, it is probably not safe to assume that they will know what the order of the various operations on various…
Jared
  • 25,520
  • 24
  • 79
  • 114
188
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
  • 854,327
  • 234
  • 1,573
  • 1,953
184
votes
6 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,623
  • 3
  • 18
  • 21
183
votes
3 answers

What are the complexity guarantees of the standard containers?

Apparently ;-) the standard containers provide some form of guarantees. What type of guarantees and what exactly are the differences between the different types of container? Working from the SGI page (about STL) I have come up with this: Container…
Martin York
  • 257,169
  • 86
  • 333
  • 562
182
votes
16 answers

What does "O(1) access time" mean?

I have seen this term "O(1) access time" used to mean "quickly" but I don't understand what it means. The other term that I see with it in the same context is "O(n) access time". Could someone please explain in a simple way what these terms…
Grygori
177
votes
31 answers

O(nlogn) Algorithm - Find three evenly spaced ones within binary string

I had this question on an Algorithms test yesterday, and I can't figure out the answer. It is driving me absolutely crazy, because it was worth about 40 points. I figure that most of the class didn't solve it correctly, because I haven't come up…
Robert Parker
  • 1,145
  • 5
  • 12
  • 14
176
votes
30 answers

How to merge two sorted arrays into a sorted array?

This was asked of me in an interview and this is the solution I provided: public static int[] merge(int[] a, int[] b) { int[] answer = new int[a.length + b.length]; int i = 0, j = 0, k = 0; while (i < a.length && j < b.length) { …
Behrooz Karjoo
  • 4,250
  • 10
  • 38
  • 48
166
votes
14 answers

Why is inserting in the middle of a linked list O(1)?

According to the Wikipedia article on linked lists, inserting in the middle of a linked list is considered O(1). I would think it would be O(n). Wouldn't you need to locate the node which could be near the end of the list? Does this analysis not…
Rob Sobers
  • 20,737
  • 24
  • 82
  • 111
151
votes
3 answers

Time complexity of python set operations?

What is the the time complexity of each of python's set operations in Big O notation? I am using Python's set type for an operation on a large number of items. I want to know how each operation's performance will be affected by the size of the set.…
Stephen Emslie
  • 10,539
  • 9
  • 32
  • 28
151
votes
11 answers

Examples of Algorithms which has O(1), O(n log n) and O(log n) complexities

What are some algorithms which we use daily that has O(1), O(n log n) and O(log n) complexities?
Rachel
  • 100,387
  • 116
  • 269
  • 365
137
votes
10 answers

Can hash tables really be O(1)?

It seems to be common knowledge that hash tables can achieve O(1), but that has never made sense to me. Can someone please explain it? Here are two situations that come to mind: A. The value is an int smaller than the size of the hash table.…
drawnonward
  • 53,459
  • 16
  • 107
  • 112
131
votes
20 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
  • 8,353
  • 10
  • 56
  • 79