zuai-logo

What is Binary Search?

A search algorithm that repeatedly divides a sorted list in half to find a target value.

Flip to see [answer/question]
Flip to see [answer/question]

All Flashcards

What is Binary Search?

A search algorithm that repeatedly divides a sorted list in half to find a target value.

What is Linear Search?

A search algorithm that checks each item in a list sequentially until the target value is found.

What is Time Complexity?

A measure of the amount of time taken by an algorithm to run as a function of the input size.

What is O(n) Time Complexity?

Linear time complexity, where the time taken increases linearly with the input size.

What is O(log n) Time Complexity?

Logarithmic time complexity, where the time taken increases logarithmically with the input size.

What does 'sorted list' mean?

A list where elements are arranged in a specific order (ascending or descending).

Define 'search space'.

The portion of the data structure that is being examined by a search algorithm.

What is an algorithm?

A step-by-step procedure for solving a problem.

What is a Data Structure?

A way of organizing and storing data.

What is the 'middle element' in binary search?

The element located at the midpoint of the current search space.

What is the key requirement for using binary search?

The list must be sorted.

Why is binary search more efficient than linear search for large, sorted lists?

Binary search eliminates half of the search space with each step, resulting in logarithmic time complexity.

Explain the concept of 'divide and conquer' in relation to binary search.

Binary search uses a divide and conquer approach by repeatedly dividing the search space in half.

What is the worst-case scenario for binary search?

The target element is not in the list, requiring the algorithm to eliminate all possible elements.

What is the best-case scenario for binary search?

The target element is the middle element in the first step.

How does binary search reduce search space?

By comparing the target value to the middle element and eliminating half of the list in each step.

Why is sorting important before applying binary search?

Sorting ensures that elements are in a predictable order, allowing binary search to correctly narrow down the search space.

What is the significance of the middle element in binary search?

It serves as the pivot point for comparison, determining which half of the list to eliminate.

Explain the role of comparisons in binary search.

Comparisons determine whether the target value is greater than, less than, or equal to the middle element, guiding the search.

What is the impact of list size on the performance of binary search?

Binary search performs significantly better on larger lists compared to linear search due to its logarithmic time complexity.

What does the following code output?

python
list = [2, 5, 7, 8, 11, 12]
target = 13
low = 0
high = len(list) - 1
while low <= high:
 mid = (low + high) // 2
 if list[mid] == target:
 print("Found")
 break
 elif list[mid] < target:
 low = mid + 1
 else:
 high = mid - 1
else:
 print("Not Found")

Not Found

What does the following code output?

python
list = [2, 5, 7, 8, 11, 12]
target = 8
low = 0
high = len(list) - 1
while low <= high:
 mid = (low + high) // 2
 if list[mid] == target:
 print("Found")
 break
 elif list[mid] < target:
 low = mid + 1
 else:
 high = mid - 1
else:
 print("Not Found")

Found

Identify the error in the following code:

python
def binary_search(list, target):
 low = 0
 high = len(list) - 1
 while low < high:
 mid = (low + high) // 2
 if list[mid] == target:
 return mid
 elif list[mid] < target:
 low = mid + 1
 else:
 high = mid - 1
 return -1

The code will enter an infinite loop if the target is not found because the condition low < high will not become false, and it does not handle the case when low == high. It should be low <= high.

What does the following code output?

python
list = [1, 3, 5, 7, 9]
target = 4
low = 0
high = len(list) - 1
while low <= high:
 mid = (low + high) // 2
 if list[mid] == target:
 print("Found")
 break
 elif list[mid] < target:
 low = mid + 1
 else:
 high = mid - 1
else:
 print("Not Found")

Not Found

What does the following code output?

python
list = [1, 3, 5, 7, 9]
target = 1
low = 0
high = len(list) - 1
while low <= high:
 mid = (low + high) // 2
 if list[mid] == target:
 print("Found")
 break
 elif list[mid] < target:
 low = mid + 1
 else:
 high = mid - 1
else:
 print("Not Found")

Found

Identify the error in the following code:

python
def binary_search(list, target):
 low = 0
 high = len(list)
 while low <= high:
 mid = (low + high) // 2
 if list[mid] == target:
 return mid
 elif list[mid] < target:
 low = mid + 1
 else:
 high = mid - 1
 return -1

IndexError: list index out of range. high = len(list) should be high = len(list) - 1

What does the following code output?

python
list = [1, 2, 3, 4, 5]
target = 6
low = 0
high = len(list) - 1
while low <= high:
 mid = (low + high) // 2
 if list[mid] == target:
 print("Found")
 break
 elif list[mid] < target:
 low = mid + 1
 else:
 high = mid - 1
else:
 print("Not Found")

Not Found

What does the following code output?

python
list = [1, 2, 3, 4, 5]
target = 1
low = 0
high = len(list) - 1
while low <= high:
 mid = (low + high) // 2
 if list[mid] == target:
 print("Found")
 break
 elif list[mid] < target:
 low = mid + 1
 else:
 high = mid - 1
else:
 print("Not Found")

Found

Identify the error in the following code:

python
def binary_search(list, target):
 low = 0
 high = len(list) - 1
 while low < high:
 mid = (low + high) // 2
 if list[mid] == target:
 return mid
 elif list[mid] < target:
 low = mid + 1
 else:
 high = mid - 1
 return -1

The while loop condition low < high should be low <= high to correctly handle the case when the target is the last element.

What does the following code output?

python
list = [1, 5, 9, 13, 17]
target = 9
low = 0
high = len(list) - 1
while low <= high:
 mid = (low + high) // 2
 if list[mid] == target:
 print("Found")
 break
 elif list[mid] < target:
 low = mid + 1
 else:
 high = mid - 1
else:
 print("Not Found")

Found