3 - Fundamentals of Algorithms Flashcards

1
Q

What is the complexity of merge sort?

A

O(nlogn)

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

What is the complexity of bubble sort?

Explain why this is.

A

O(n²)

In each pass through the list n items will be examine. There will be at most n passes through the list.

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

What is the complexity of binary search?

Explain why this is.

A

O(logn)

Each comparison halves the size of the list that has to be searched through.

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

What is the complexity of linear search?

Explain why this is.

A

O(n)

As the size of the list increases the time taken increases at the same rate.

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

What is the complexity of binary-tree search?

A

O(logn)

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

What are the steps for Breadth-First Search?

A
  • Mark all vertices as unvisited
  • Choose a starting vertex
  • Visit the starting vertex and mark it as visited
  • Enqueue starting vertex in the queue
  • Dequeue a vertex from the queue
  • For each neighbouring vertex of this vertex, if it is univisited visit it, mark it as visited and enqueue it in the queue
  • Repeat the processor of dequeuing vertices and visiting its neighbours until the queue is empty
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the steps for Depth-First Search using a stack?

A
  • Mark the starting vertex as discovered
  • Push the starting node onto the stack
  • Pop the top vertex off the stack and mark and visit it
  • Push each of this vertex’s neighbours to the stack and mark them as discovered
  • Repeat the processes of popping vertices and pushing their neighbours to the stack until the stack is empty
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a common use of Breadth-First Search?

A

To find the path containing the least number of nodes between two points in an unweighted graph.

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

What is a common use of Depth-First Search?

A

It is commonly used to navigate a maze.

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

What are the steps for linear search?

A
  • Start with the beginning of the list
  • Compare each item with the item you’re searching for
  • Stop either when the item is found or the end of the list is reached
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the steps for binary search?

A
  • For a sorted list
  • Set the front pointer to 0 and the rear pointer to the index of the last item in the list
  • Compare the item in the middle of the two pointers with the item you’re looking for
  • If the items are the same, the item is in the list
  • If the middle item is less than the item to search for, change the rear pointer to the middle - 1
  • If the middle item is greater than the item to search for, change the front pointer to the middle + 1
  • Repeat the processes of comparing the middle item until either the item is found or the front pointer is greater than the rear pointer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the steps for binary tree search?

A
  • Start at the root
  • Compare the item at the root to the value you’re searching for
  • If it is the same, the item has been found
  • If the root is higher, check the left child
  • If the root is lower, check the right child
  • Keep comparing the value to different nodes until either the item is found or the item has been compared with a leaf node
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the steps for bubble sort?

A
  • Compare the first item to the second item and swap them if they are in the wrong order
  • Compare the second item with the third item and swap them if they are in the wrong order
  • Keep comparing each pair of adjacent items and swapping them if they are in the wrong order
  • After each pair of cards have been compared the first pass is complete
  • For the next pass the item at the rear of the list doesn’t need to be compared with (as it is already in the correct position)
  • For each pass, the number of items at the rear of the list that don’t need to be compared with increases by 1
  • Stop when there is only 1 item that hasn’t been discounted from the comparisons
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the steps for merge sort?

A
  • Repeatedly split the list in half until each sublist contains only 1 item
  • Merge each sublist with the sublist that it was most recently split from so that the new list they form is ordered
  • Continue merging the sublists until they form a single list
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What approach to problem solving is merge sort an example of?

A

‘Divide and conquer’

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

What problem is Dijkstra’s Algorithm designed to solve?

A

Finding the shortest path between two nodes in a graph (it actually find the shortest path between one node and every other node)

17
Q

What is an application of Dijkstra’s Algorithm?

A

Routing packets on the Internet