2.3.1 Comparison of Algorithms (complexities) Flashcards

1
Q

Time Complexity

A

The amount of time required to solve a particular problem.

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

Why is it good to use time complexity?

A

You can predict the amount of time it takes for an algorithm to finish, given the number of data elements.

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

Describe constant time complexity O(1)

A

The time taken for an algorithm to complete is independent from the number of elements inputted.

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

Describe linear time complexity. O(n)

A

The time taken for an algorithm to complete is directly proportional to the number of elements inputted.

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

Describe polynomial time complexity. O(n^x)

A

The time taken for an algorithm to complete is directly proportional to the number of elements to the power x.

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

Describe exponential time complexity. O(2^n)

A

The time taken for an algorithm to complete will double with every additional item.

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

Describe logarithmic time complexity. O(logn)

A

The time taken for an algorithm to complete will increase at a smaller rate as the number of elements inputted increases.

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

Determine the time complexity of the following code.
~~~
print(“hello”)
~~~

A

Constant Time
O(1)

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

Determine the time complexity of the following code.
~~~
inputtedValue = [a,b,c, … , n]
for i = 0 to inputtedValue.length() -1
print(“hello”)
next i
~~~

A

Linear Time Complexity
O(n)

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

Determine the time complexity of the following code
~~~
inputtedValue = [a,b,c, … , n]
for i = 0 to inputtedValue.length() -1
for j = 0 to inputtedValue.length() - 1
print(“hello”)
next j
next i
~~~

A

Polynomial Time Complexity
O(n^2)

Power = Number of embedded loops

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

Determine the time complexity of a recursive algorithm which solves a problem of Size N by recursively solving two smaller problems of size N-1

A

Exponential Time Complexity
O(2^n)

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

Determine the time complexity of a divide and conquer algorithm, which halves the number of items that needs to be searched through every time.

A

Logarithmic Time Complexity
O(log n)

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

What is the space complexity of an algorithm?

A

The amount of storage the algorithm takes.

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

What two things are incredibly important when analysing the effectiveness of a program?

A

Time complexity and space complexity

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

Does either time complexity or space complexity have more priority than the other?

A

No. Decide whats more important when designing the algorithm.

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

What is an algorithm?

A

A series of steps that completes a task.

17
Q

How do we reduce space complexity?

A

Make sure you perform all of the changes on the original pieces of data.

18
Q

How do we reduce the time complexity?

A
  • Try to reduce the number of embedded loops.
  • Try to reduce the number of items you have to complete operations on
19
Q

Best/Average/Worst Time Complexities: Linear Search

A

BEST: O(1)
AVERAGE: O(n)
WORST: O(n)

20
Q

Best/Average/Worst Time Complexities: Binary Search Array

A

BEST: O(1)
AVERAGE: O(logn)
WORST: O(logn)

21
Q

Best/Average/Worst Time Complexities: Binary Search Tree

A

BEST: O(1)
AVERAGE: O(logn)
WORST: O(n)

22
Q

Best/Average/Worst Time Complexities: Hashing

A

BEST: O(1)
AVERAGE: O(1)
WORST: O(n)

23
Q

Best/Average/Worst Time Complexities: Breadth/Depth first

A

BEST: O(1)
AVERAGE: O(V+E) [Vertices + Edges]
WORST: O(V^2)

24
Q

Best/Average/Worst Time Complexities: Bubble Sort

A

BEST: O(n)
AVERAGE: O(n^2)
WORST: O(n^2)

25
Q

Best/Average/Worst Time Complexities: Insertion Sort

A

BEST: O(n)
AVERAGE: O(n^2)
WORST: O(n^2)

26
Q

Best/Average/Worst Time Complexities: Merge Sort

A

BEST: O(nlogn)
AVERAGE: O(nlogn)
WORST: O(nlogn)

27
Q

Best/Average/Worst Time Complexities: Quick Sort

A

BEST: O(nlogn)
AVERAGE: O(nlogn)
WORST: O(n^2)

28
Q

Compare the space complexities of the four sorting algorithms:
Bubble
Insertion
Merge
Quick

A

Bubble O(1)
Insertion O(1)
Merge O(n)
Quick O(logn)