Week 10 Comparing Algorithms Flashcards

Big-O Notation, Searching & Sorting (14 cards)

1
Q

List all the time complexities, from most efficient to least

A

O(1), O(logn), O(n), O(nlogn), On^k), O(k^n), O(n!)

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

What does constant time (O(1)) complexity mean?

A

As the size of the input/problem increases;
the amount of time taken remains the same;

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

What does linear time (O(n)) complexity mean?

A

As the size of the input/problem increases;
the amount of time taken increases at the same rate (directly proportional relationship);

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

What can O(n!) be described as?

A

The number of permutations (ways of arranging) of n

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

What is an intractable problem?

A

Problem can be solved but only in exponential time (or greater)

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

What is a tractable problem?

A

Problem can be solved in polynomial time (or less)

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

What is a heuristic algorithm?

A

Finds a close-to-optimal solution by relaxing the constraints of the problem to reduce the size of the problem space

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

What approaches can we take to try and solve intractable problems?

A

Use a heuristic algorithm, providing a close-to-optimal solution;
It makes use of estimates based on experience and relaxes some constraints of the problem;

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

State and explain the time complexity of the linear search [3]

A

O(n);
As the size of the list/problem increases, the time taken increases at the SAME RATE;
The worst-case scenario is that ALL ๐’ items will be visited (if item is at the end of the list);

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

State and explain the time complexity of the binary search [3]

A

O(log n);
Each comparison halves the size of the list that has to be searched through;
If the size of the list doubles then the number of comparisons needed only increases by 1;

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

Explain how the Bubble Sort works [3]

A

On each pass through the list it compares each pair of adjacent items;
If they’re in the wrong order, the items are swapped;
Does at most n passes through the list;

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

State and explain the time complexity of the Bubble Sort [3]

A

O(n^2);
The algorithm makes (at most) ๐’ passes through the list;
On each pass ๐’ items are examined;

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

State two ways to improve the efficiency of the Bubble Sort [2]

A

**Solution 1: **
Have a flag (Boolean) variable that is set to True if a swap is made and reset to False at the start of each pass.
Change the outer loop so that it would stop repeating if no swaps have been made;

**Solution 2: **
After the inner loop subtract 1 fromits upper limit;
by subtractingthe pass numberfromthe length of the list;

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

State and explain the time complexity of the merge sort [2]

A

Merge Sort works by repeatedly halving the list into sublists until there are n lists of 1 element.

The halving process takes ๐‘ถ(๐’๐’๐’ˆโก๐’) operations;

These sublists are then compared with adjacent sublists and ordered. This takes ๐‘ถ(๐’) operations;

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