Time complexity Flashcards

(9 cards)

1
Q

Time Complexity

A

Describes how the runtime of an algorithm changes as the size of its input ($n$) grows.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Big O Notation ($O$)

A

A mathematical notation that describes the limiting behavior (worst-case scenario) of an algorithm’s runtime as the input size ($n$) grows.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

$O(1)$ - Constant Time

A

Runtime is the same regardless of input size. \n\nPython Example: def get_first(data):\n return data[0] (Accessing an element by index)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

$O(\log n)$ - Logarithmic Time

A

Runtime grows slowly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

$O(n)$ - Linear Time

A

Runtime grows directly and proportionally with the input size ($n$). \n\nPython Example: for item in my_list:\n print(item) (Iterating through a list)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

$O(n \log n)$ - Quasilinear Time

A

A very efficient time complexity for sorting algorithms. \n\nPython Example: sorted_list = sorted(my_list) (Python’s built-in Timsort)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

$O(n^2)$ - Quadratic Time

A

Runtime grows as the square of the input size. Often involves nested loops. \n\nPython Example: for i in data:\n for j in data:\n print(i, j) (Nested iteration)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

$O(2^n)$ - Exponential Time

A

Runtime doubles with each new element added to the input. Extremely slow. \n\nPython Example: A basic recursive Fibonacci function fib(n) = fib(n-1) + fib(n-2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Big O Hierarchy (Fastest to Slowest)

A

$O(1) \ll O(\log n) \ll O(n) \ll O(n \log n) \ll O(n^2) \ll O(2^n)$

How well did you know this?
1
Not at all
2
3
4
5
Perfectly