Binary Search Algorithm Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is Binary Search?

A

An algorithm.

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

Why is it called Binary Search?

A

Binary meaning two, at each step of the search process, the algorithm splits the search space into two halves, discarding one half based on a comaprison, and continues to search in the other half.

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

How does Binary Search work?

A

The process works by repeatedly dividing a sorted array in half, comparing the middle element with the target element, search continues on the left half if it is less than the target value and on the right if it is greater than the target value. This continues until the target value is found or the subarray reduces to zero.

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

What is the time complexity?

A

O(log n), where n is the number of element in the array. The logarithmic complexity comes from halving the search space after each comparison.

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

What is the space complexity?

A

O(1) for iterative approach because no additional space is required.

O(log n) for a recursive approach due to the stack used during recursion.

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

Can binary search be used on unsorted arrays?

A

No, binary search can only be applied to sorted arrays. This is because the assumption is that the elements on one side are greater or lesser than the middle element which is compared with the target value.

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

What is the difference between iterative and recursive binary search?

A

Iterative uses a loop to continously divide the array in half while recursive calls itself until it finds the target

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

What does it mean by search space becomes empty?

A

The algorithm has reduced the number of elements to search through to zero, indicating that the target value is not present in the array.

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