2.3.1 Algorithms Flashcards

(17 cards)

1
Q

Describe what is meant by Best time O(n) complexity [3]

A
  1. Linear.
  2. Best time grows at the same rate as the number of elements.
  3. This is the case when the data is already in order.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Describe what is meant by Average and worst time O(n²) complexity [3]

A
  1. Polynomial / Quadratic
  2. Worst and average time is proportional to the square (polynomial) of the number of elements
  3. Worst case is when the data is initially in the reverse order
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe what is meant by Worst Space O(1) complexity [3]

A
  1. Constant
  2. Will always take the same amount of memory (in addition to the list itself)
  3. Binary Tree / Binary Search Tree
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The contents of “processedData” are shown: 0|0.5|0|1|2|1.5|1
The data needs to be sorted into ascending order.
Explain how a bubble sort algorithm sorts data. Use the current contents of “processedData” in your explanation [5]

A
  1. Compares each pair of data e.g. 0 and 0.5
  2. If they are in the correct order it moves to the next pair e.g. 0.5
    and 0
  3. If they are in the wrong order it swaps them e.g. 0.5 and 0
    becomes 0 and 0.5
  4. Continues to the end of the array e.g. Pass 1 complete
  5. If there has been a swap it checks again e.g. Pass 2 complete
  6. If there have been no swaps it is sorted
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain why a quicksort is known as a divide and conquer algorithm [2]

A

Because it decomposes data sets into smaller subsets [1 mark]
• ..and then sorts each split subset [l mark]

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

A one dimensional array holds data that needs to be sorted.
Describe how a quicksort would sort data into ascending order [5]

A

• Choose a pivot // identify start and end pointers
• Compare each element to the pivot… Il compare start and end pointers
• Put items < pivot in the left sublist
• Put items > pivot in the right sublist
• Choose a pivot in each sublist
• If start pointer is larger than end pointer…
• …then swap data items around
• And repeat the process until each item becomes a pivot

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

What is the precondition for a binary search?

A

The data must be in order

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

Describe how a linear search works [5]

A

• Compare the search item with the first value
• ….then compare the search item with the next value
• ….repeat the above process until either
• ….the end of the array has been reached or
• ….the search item is found and then stop
• ….then return the array position

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

An array is shown here:
2|12|1|9|3|5|15|7
Show how a merge sort will sort this section of the array numberArray into ascending numerical order. [4]

A
  1. A split the data into individual items [2][12][1][9][3][5][15][7]
  2. Combine into pairs which are in the right order:
    [2|12][1|9][3|5][15|7]
  3. Merge the pairs so that they are in the right order:
    [1|2|9|12][3|5|7|15]
  4. Merge for final result:
    1|2|3|5|7|9|12|15
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How does an insertion sort work? [3]

A
  1. Compare the current item being sorted (starting from the first item), to each previous item
  2. If it is smaller than the previous item then the previous item is moved to the right and the current item takes its place
  3. If the current item is larger than the previous item then it is in the correct position and the next current item is then sorted
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does a binary search work? [3]

A
  1. It compares the middle item to the searched for target item. If the values match then the index is returned.
  2. If not then the list is adjusted such that:
    the top half is ignored if the target item is less than the middle value
    or the bottom half is ignored if the target item is larger than the middle value
  3. Once the list has been adjusted, the search continues until the item is found or the item is not in the list
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the worst-case, best-case and average time complexity for a binary search? [3]

A

Worst case: O(1)
Best case: O(1)
Average time: O(log(n))

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

What is the worst-case, best-case and average time complexity for a linear search? [3]

A

Worst case: O(1)
Best case: O(1)
Average time: O(n)

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

What is meant by O(2^n)? [1]

A

An algorithm that takes double the time to execute for each additional data input value.

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

What is meant by O(log(n))? [1]

A

An algorithm that takes very little time as the size of the input grows larger

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

How do breadth first and depth first traversals work? [4]

A

Breadth first visits the root node then visits all the nodes on the level below from left to right, before doing the same on the next level until all nodes are visited.
Depth first traverses as far down the left most subtree, then visit the right subtree, then visit the root node.

17
Q

Explain how backtracking is used in a depth-first traversal?

A
  1. Depth first starts at the root
  2. and goes all the way down one branch to the bottom
  3. When it cannot go any further
  4. It then backtracks to the previous node
  5. And continues to backtrack until a node is reached with unvisited children.
  6. and checks down that branch