Algorithms Flashcards

(40 cards)

1
Q

Explain the steps of binary search

A
  • Binary search is more efficient than linear search
  • In binary search, the list must be ordered
  • First, the middle term of the list is compared with the target term. If the term matches the target then the index is returned
  • If not, the top half of the list is ignored if the target term is less than the middle term
  • If the target term is greater than the middle term, the bottom half of the list is ignored
  • This is repeated until the target term is found
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain the steps of linear search

A
  • Each item in the list is compared with the search item. If it does not match, the next term is compared
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain the stages of bubble sort

A
  • Starting at the beginning, two adjacent terms are compared with eachother and then sorted in order
  • The last term of the first comparison is then compared with the next adjacent term, ordering them aswell
  • Once the end of the list has been reached, the last term will be in the correct place. The algorithm then resets for another pass
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain the steps of insertion sort

A
  • Starts with one of the items from the list
  • The next item is compared with the first item. If it is greater than the first item is shifted to the left and the next item takes its place. if the next item is less than the first item than the first item is shifted to the right and the next item takes its place
  • This is repeated until the list is ordered
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain the steps of merge sort

A
  • The list is broken up into individual terms
  • Adjacent terms (in twos) are put back together in order, creating many mini lists
  • Adjacent mini lists are then compared and put back together in order
  • This is repeated until the whole list is back together in order
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Dijkstra’s shortest path algorithm?

A
  • An example of an optimisation algorithm that calculates the shortest path from a starting location to all other locations
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is Big O notation?

A
  • Big O notation is used to express the complexity of an algorithm
  • Describes the scalability of an algorithm as it’s order (O)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the main rules of Big O notation?

A
  • Remove all terms except the one with the largest factor or exponent
  • Remove any constants
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is O(1)?

A
  • Constant
  • An algorithm that takes a constant amount of time
  • Requires the same number of instructions, regardless of the size of the input data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is O(n)?

A
  • Linear
  • As the input size grows, the number of instructions required increases proportionally
  • E.G: a linear search of 100 items will take 10 times longer than that with 10 items
  • Shown with loops (not embedded)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How is O(n) written in code?

A
  • Linear Big O is shown using loops
  • Each loop adds an extra n onto the Big O
  • The loops must NOT be embedded
  • E.G: 3 for loops in the algorithm = O(3n)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is O(n²)?

A
  • Polynomial
  • The time taken to complete the algorithm is proportional to the square of the size of the input data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is an example of an algorithm which is O(n)?

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

How is O(n²) written in code?

A
  • Requires the use of nested loops (embedded)
  • Regardless of the number of loops nested, they are all referred to as polynomial (hence still O(n²))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is an example of an algorithm which is O(n²)?

A
  • Bubble sort
  • n passes are made, and for each pass, n comparisons are made
  • Nests loops
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is O(2ⁿ)?

A
  • Exponential
  • An algorithm which takes double the time to execute for each additional data value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the issue with O(2ⁿ)?

A
  • The number of instructions executed becomes very large, very quickly
  • Hence, few everyday algorithms use exponential time due to its sheer inefficiency
18
Q

What is O(logn)?

A
  • Logarithmic
  • An algorithm that takes very little time as the size of the input grows larger
19
Q

What is an example of an algorithm which is O(logn)?

A
  • Binary search
  • Doubling the size of the input data has very little effect on the overall number of instructions executed by the algorithm
20
Q

What are the different cases of complexity?

A
  • All algorithms have a best case, average case and worst case scenario
  • Best case: where the algorithm is most efficient (e.g: a linear search finding the item on the first element)
  • Average case: where an algorithm performs at neither its best nor its worst (e.g: a linear search finding the item at the middle of the list)
  • Worst case: where an algorithm is least efficient (e.g: a linear search finding the item on the last element)
21
Q

In what case is complexity measured?

A
  • Big-O notation is always measured by the worst case performance of an algorithm
  • This allows programmers to select algorithms based on minimum performance levels
22
Q

Rank the different Big O algorithms

A
  • O(1)
  • O(logn)
  • O(n)
  • O(nlogn)
  • O(x^2)
  • O(x^3)
  • O(2^x)
  • O(x!)
23
Q

What is an example of O(nlogn)?

24
Q

What is the time complexity of binary search?

25
What is the space complexity of binary search?
- Iterative binary search (fixed number of variables): O(1) - Recursive binary search: O(logn)
26
What is the time complexity of linear search?
- O(n)
27
What is the space complexity of linear search?
- O(1) - Does not require any additional memory that grows with the size of the input
28
What is the time complexity of bubble sort?
- O(n²) - Nested loops
29
What is the space complexity of bubble sort?
- O(1) - Operates in space with a fixed amount of memory
30
What is the time complexity of insertion sort?
- O(n²)
31
What is the space complexity of insertion sort?
- O(1) - Requires no additional space
32
What is merge sort?
- A sorting algorithm that uses divide and conquer - Reduces the size of the problem into smaller problems that are more easily solvable
33
Explain the steps of merge sort
- The merge sort list is divided in half to create two sub-lists - Each sub-list is then divided again until each sub-list contains only one item - Groups of two adjacent sub-lists are then sorted and merged together to form a new, larger, sorted sub-list - Repeat merging until all sub-lists are merged into one, sorted list
34
What is the time complexity of merge sort?
- O(nlogn)
35
What is the space complexity of merge sort?
- O(n) - Required to store the sub-lists
36
What is an A* search algorithm?
- Builds on Dijkstra's by using a heuristic - A heuristic is a rule of thumb, best guess or estimate that helps the algorithm accurately approximate the shortest path to each node
37
Why is Dijkstra's algorithm inefficient?
- Dijkstra's algorithm uses a single cost function: the real distance from the initial node to every other node - The cost function is unreliable, as A to B may be larger than A to C but Dijkstra's will follow C anyway as it is faster, even if it doesn't lead to the goal node
38
How does the A* search algorithm work?
- It uses two functions - g(x): the real distance cost function - cost from start to current node - h(x): heuristic function - estimated cost from current node to goal - Therefore, f(x) = g(x) + h(x) - If h(x) increases, it means the algorithm is travelling further away from the goal node
39
What is the heuristic function in the A* search algorithm?
- Approximate the straight line distance from the current node to the goal node - By following nodes based on shorter heuristic distances, the algorithm can be sure it is getting closer to the goal node
40
What is the difference between the A* search algorithm and Dijkstra's algorithm?
- A* search focuses on reaching the goal node - Dijkstra's algorithm calculates the distance from the initial node to every other node