(3) Algorithms II Flashcards

1
Q

Which issue do we need to consider with regard to efficiency?

A
  1. Time it takes an algorithm to solve a problem

2. Allocation of space (memory) needed for computation

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

Definition Time Complexity

A

Number of steps the algorithm needs to solve a given problem, as a function of the size of the input (relevant operation is considered and not the hardware or time (e.g. minutes))

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

What is the Big O-Notation good for?

A
  • Asymptotic upper bound of the algorithm
  • Worst case time complexity
  • Used to compare algorithms
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the time complexity of naive search?

A

O(n), n = number of elements in the array

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

What are the steps of the binary search algorithm?

A
  1. Start in the middle of the array
  2. If element ? searched element-> done
  3. If element < middle element, go to left part
  4. element > middle element go to right part
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the time complexity of binary search (incl. derivation)?

A

O(log n)

-> we divide the array n/2 times

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

What is a prerequisite to apply binary search?

A

Array must be sorted

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

Does it always make sense to sort prior to searching?

A

Depends on how much sorting costs and how much more you search compared to sort

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

What is the principle of bubble sort?

A
  • steps through list and compares elements next to each other
  • if the elements are unsorted they are swapped
  • repeat until no more swaps are necessary
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the time complexity of bubble sort (worst and best case)?

A

Worst case: O(n^2)

Best case: O(n) -> Bubble sort looks at all elements at least once

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

What is the principle of merge sort?

A
  • divide the unsorted array into two arrays
    sort each of the two arrays recursively until array size= 1
  • Merge the two sorted arrays into one sorted array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the time complexity of merge sort?

A

O(n * log(n))

  • n steps to split
  • n steps to merge
  • 2T(n/2) analogous computation for each of the two bisected arrays
How well did you know this?
1
Not at all
2
3
4
5
Perfectly