Week 2 - Binary Search And Merge Sort Flashcards

1
Q

What are the Pros of Linear Search?

A
  • Easy to implement
  • Use less money
  • No need for sorting
  • Efficient for small and unsourced datasets
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the cons of Linear Search?

A
  • Inefficient for large datasets
  • No optimisation potential
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Define Binary Search

A

It an efficient algorithm for finding an element in a sorted array.

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

What an a classic example of a binary search

A

Divide and Conquer approach in algorithm design

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

What are the common design approaches?

A
  1. Brute Force ( Linear Search)
  2. Divide and conquer ( Binary Search, Merge Sort and QuickSort)
  3. Greedy (Dijkstra)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the 4 steps to do Binary Search?

A
  1. Let L be the first element of the array and R be the last element of the array
  2. Calculate M=(L+R)/2
  3. Compare a[M] with x
  4. Repeat step 2 and 3 until M is returned or if L>R, return -1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happens when a[M] is greater or equal to x?

A
  • When a[M] is greater than x, it means R= M-1
  • When a[M] is equal to x, it means R=M-1
  • Otherwise, L=M+1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Define Iterative in Space complexity

A

Iterative: O(1). As it uses a constant amount of space for L,R and M

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

Define Recursive in Space complexity

A

Recursive: O(log n). At each recursive call, a new layer is added to the call stack and will allocate additional memory to it.

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

What is the pre-conditions in a binary search?

A
  • The input must be sorted in either ascending or descending order.
  • The elements must be allow for an order relationship
  • Have direct access to any elements within the input
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the Post-conditions in a Binary search?

A
  • If the target element is in the array, the algorithm returns the index of the element.
  • Otherwise, returns -1
  • The input remains unchanged
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the three invariants found in binary search?

A
  1. Search Space invariant
  2. Convergence Invariant
  3. Ordering Invariant
How well did you know this?
1
Not at all
2
3
4
5
Perfectly