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
13
Q

What is the Search Space Invariant in Binary Search?

A

After each iteration, if the target is in the array, it must lie between the pointers L and R

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

What is the Convergence Invariant in Binary Search?

A

The size of the search space decreases after each iteration, ensuring the algorithm eventually terminates.

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

What is the Ordering Invariant in Binary Search?

A

The relative order of elements in the array remains the same throughout the search — the array must be sorted and unmodified.

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

What if there are multiple elements with same vale and we want to return the index of the first occurrence?

17
Q

What are the steps to do binary search?

A
  1. Let L be the first element of the array and R be the las element of the array.
  2. Calculate M = (L+R)/2
  3. Compare a[M] with
  4. Repeat step 2 and 3 until M is returned, or if L>R return -1
18
Q

What is the difference in binary search in step 3 a[M] when it comes to multiple elements?

A

if a[M] is equal to x, record the position but keep searching to the left (R=M - 1)

19
Q

What are the pros of binary search?

A
  • Less time consuming
  • Efficient for large datasets
20
Q

What are the cons of binary search?

A
  • Input must be sorted first
  • Not suitable for frequently changing datasets
  • Less effective on data structures that do not provide random/direct access
21
Q

What is Merge Sort?

A

A Divide & Conquer algorithm developed by John von Neumann in 1945.

22
Q

What is the main principle behind Merge Sort?

A

Merging two sorted lists is more efficient than sorting two unsorted lists.

23
Q

What are the two key steps in Merge Sort?

A

1.Split the list into halves recursively.
2.Merge the sorted halves back together.

24
Q

What happens in Step 1 (Split) of Merge Sort?

A

The array is recursively divided into two subarrays until each subarray contains only one element.

25
What is the purpose of Step 2 (Merge) in Merge Sort?
To repeatedly merge two sorted subarrays into a single sorted array until the full array is rebuilt.
26
How is merging done during Merge Sort?
- Maintain two pointers, one for each subarray. - Compare elements at each pointer. - Add the smaller element to the result array. - Move the pointer in the subarray with the smaller element forward. - Repeat until all elements are merged.
27
What is the first invariant during the merge step of Merge Sort?
At the start of each iteration, the elements in the output (merged) array are sorted.
28
What is the second invariant during merging in Merge Sort?
Elements in the input subarrays that haven’t been processed are greater than or equal to the elements already in the output array.
29
What is true when the merging loop terminates in Merge Sort?
One of the input subarrays is fully processed, and the remaining elements in the other subarray are greater than or equal to all elements in the output array.
30