binary

Cards (29)

  • Binary Search continuously divides the sorted list, comparing the middle element with the target value
  • What is binary search?
    A fundamental search algorithm
  • How does binary search find a target value?
    It eliminates half of the remaining values
  • How do you calculate the middle value in binary search?
    Average of leftmost and rightmost indices
  • What happens if the middle value is greater than the target?
    Eliminate it and values greater
  • What happens if the middle value is less than the target?
    Eliminate it and values less
  • What is the final step if the target is found?
    Return the index of the target
  • How is the binary search represented in a binary tree format?
    • Root node: middle element of the array
    • Left child: middle of the lower half
    • Right child: middle of the upper half
    • Follow similar pattern for remaining nodes
  • What does the height of the binary search tree represent?
    Floor of log base 2 of n plus one
  • What does each iteration of the while loop in binary search do?
    Descend one level down the tree
  • What should you do if the target is not found in the array?
    Return negative one
  • What are the key components of the binary search algorithm code?
    • Initialize left pointer to zero
    • Initialize right pointer to last element
    • While loop: iterate while left ≤ right
    • Calculate mid: average of left and right
    • Update pointers based on comparison with target
    • Return index if found, else return -1
  • What is the purpose of a binary search?
    To find an item in a sorted list
  • How does a binary search reach its end?
    It finds the item or exhausts the search
  • What does a binary search do when it finds the target?
    It identifies the target item in the list
  • What is the process of dividing in a binary search?
    It repeatedly halves the search space
  • What does a binary search compare?
    The target item and the middle item
  • What happens if the target item is less than the middle item?
    The search continues in the left half
  • If the target item is greater than the middle item, what does the binary search do?
    It searches in the right half of the list
  • What is the significance of keeping the list in order during a binary search?
    It ensures the search method works correctly
  • What should be done if the item is not found in a binary search?
    The search concludes without finding the item
  • What is the role of the middle item in a binary search?
    It acts as a reference point for comparison
  • How does a binary search handle larger or smaller items?
    It adjusts the search space accordingly
  • What is the final step in a binary search?
    To confirm whether the item was found
  • What are the steps involved in a binary search?
    1. Start with a sorted list.
    2. Compare the target with the middle item.
    3. If equal, return the item.
    4. If less, search the left half.
    5. If greater, search the right half.
    6. Repeat until found or exhausted.
  • What is the initial condition for a binary search?
    The list must be sorted
  • Why is it important to divide the search space in binary search?
    To efficiently narrow down the possible locations
  • What should you do if the list is not sorted before performing a binary search?
    Sort the list before searching
  • How does the efficiency of binary search compare to linear search?
    Binary search is generally faster