11 Complexity Flashcards

(34 cards)

1
Q

Algorithmic complexity

A

How required time changes as the problem size increases

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

Time cost equation

A

t = Cg(n) –> O(g(n))
n- size of problem
C - constant
g - function

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

O(1)

A

Algorithm constant in time
t = O(1)
time required for the algorithm is independent of the problem size n

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

O(n^k)

A

Polynomial in time

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

O(log n)

A

Logarithmic in time

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

O(n log n)

A

Log - linear in time
Examples: Quick sort and fast Fourier Transform

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

O(c^n)

A

Exponential in time
Algorithm becomes expensive (time cost) for large n, generally little to no practical use

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

Complexity in terms of space

A

How the computer memory required by an algorithm will change with problem size

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

How do you determine the complexity of an algorithm

A

Count the number of operations

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

What is the complexity of multiplying an array by a scalar

A

cost for x[i] = a*x[i] is O(1) for each i, so overall cost O(n)

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

What is the complexity of multiplying a nxm matrix by scalar a

A

total cost O(n) for each i, and loop i m times, so total cost is O(mn) and if m=n then O(n^2)

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

What do we need to consider when assessing an algorithm?

A

Best case complexity
Worst case complexity
Average case complexity

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

What is the cost of operations (+,-,*,/)

A

O(1)

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

Linear search

A

Search algorithm that iterates over an array one element at a time looking for a target value

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

What is the best-case time complexity of linear search?

A

Value found in the first position - O(1)

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

What is the worst case time complexity of linear search?

A

Value is not in array, so every element must be checked - O(n)

17
Q

What is the average-case time complexity of linear search?

A

On average, value found halfway through the array - O(n)

18
Q

Why is the average case of linear search O(n) and not O(n/2)

A

Constant factors are ignored in ‘Big-O’ notation

19
Q

What is binary search?

A

Search algorithm that repeatedly divides a sorted array in half to find a target value

20
Q

What is the best-case time complexity of binary search?

A

O(1) - the value is found at the middle of the first check

21
Q

What is the worst-case time complexity of binary search?

A

O(log n) - the array is repeatedly halved until the value is found or ruled out

22
Q

What is the average-case time complexity of binary search?

23
Q

Is linear or binary search faster for large arrays?

A

Binary, as O(log n) grows slower than O(n)

24
Q

Why can linear search be faster than binary search for small arrays?

A

Linear search has a smaller constant factor and benefits from CPU cache optimisation

25
How do modern processors help linear search?
They are optimised for sequential memory access using fast CPU cache
26
What is a con for using binary for small problems?
Hard to get good timing as noise, which is caused by things like other processes running on a computer, can be significant
27
Bubble sort
For array of length n, involves iterating over all entries and performing swaps
28
What is the cost of iterating over all entries and performing swaps
O(n)
29
What it the complexity of bubble sort?
We perform a swap with complexity O(n), n times so the complexity is O(n^2)
30
What is the complexity of swapping data
O(1)
31
Downside of bubble sort
Too expensive for large n to be of practical use
32
What is the best-case time complexity of Quicksort?
O(n log n)
33
What is the worst-case time complexity of Quicksort?
O(n^2) - data is already sorted
34
What is the average-case time complexity of Quicksort?
O(n log n)