"""
Perform binary search on the given sorted list to find the target value.
Parameters:
- arr (list): The sorted list to be searched.
- target: The value to be searched for.
Returns:
- int: Index of the target value if found, otherwise -1.
"""
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2 # Calculate mid index
if arr[mid] == target:
return mid # Target found, return its index
elif arr[mid] < target:
low = mid + 1 # Adjust the search range to the right half
else:
high = mid - 1 # Adjust the search range to the left half
return -1 # Target not found
# Example usage:
my_sorted_list = [2, 5, 8, 12, 16, 23, 38, 45, 50]
target_value = 16
result = binary_search(my_sorted_list, target_value)
if result != -1:
print(f'Target {target_value} found at index {result}.')
else:
print(f'Target {target_value} not found in the list.')
---------------------------------------------------------------------------------------------
def binary_search(arr, target,low, high):
while low <= high:
mid = (low + high) // 2 # Calculate mid index
if arr[mid] == target:
return mid # Target found, return its index
elif arr[mid] < target:
low = mid + 1 # Adjust the search range to the right half
else:
high = mid - 1 # Adjust the search range to the left half
return -1 # Target not found
# Example usage:
my_sorted_list = [2, 5, 8, 12, 16, 23, 38, 45, 50]
target_value = 16
result = binary_search(my_sorted_list, target_value,0,8)
if result != -1:
print(f'Target {target_value} found at index {result}.')
else:
print(f'Target {target_value} not found in the list.')
---------------------------------------------------------------------------------------------
def binary_search_recursive(arr, target, start, end):
"""
Perform binary search on the given sorted list to find the target value.
Parameters:
- arr (list): The sorted list to be searched.
- target: The value to be searched for.
- start: The starting index for the search.
- end: The ending index for the search.
Returns:
- int: Index of the target value if found, otherwise -1.
"""
if start <= end:
mid = start + (end - start) // 2
# Check if the target is present at the middle
if arr[mid] == target:
return mid
# If the target is smaller, ignore the right half
elif arr[mid] > target:
return binary_search_recursive(arr, target, start, mid - 1)
# If the target is larger, ignore the left half
else:
return binary_search_recursive(arr, target, mid + 1, end)
else:
# Target is not present in the array
return -1
# Example usage:
sorted_list = [2, 5, 8, 12, 16, 23, 38, 45, 50]
target_value = 16
result = binary_search_recursive(sorted_list, target_value, 0, len(sorted_list) - 1)
if result != -1:
print(f'Target {target_value} found at index {result}.')
else:
print(f'Target {target_value} not found in the list.')
---------------------------------------------------------------------------------------------
def efficient_bubble_sort(names, dobs, phone_numbers):
n = len(names)
swapped = True
while swapped:
swapped = False
for i in range(0, n - 1):
if names[i] > names[i + 1]:
# Swap names
names[i], names[i + 1] = names[i + 1], names[i]
# Swap date of birth
dobs[i], dobs[i + 1] = dobs[i + 1], dobs[i]
# Swap phone numbers
phone_numbers[i], phone_numbers[i + 1] = phone_numbers[i + 1], phone_numbers[i]
swapped = True
return names, dobs, phone_numbers
# Example usage:
# Assuming you have three lists 'names', 'dob', and 'phone_numbers' with 200 elements each
# Sample data initialization (replace it with your actual data)
names = ["John", "Alice", "Bob", ...] # Replace with actual names
dob = ["1990-01-01", "1985-05-12", "1998-09-20", ...] # Replace with actual dates of birth
phone_numbers = ["123-456-7890", "987-654-3210", "555-123-4567", ...] # Replace with actual phone numbers
# Sorting the arrays using efficient bubble sort
names, dob, phone_numbers = efficient_bubble_sort(names, dob, phone_numbers)
# Now, 'names', 'dob', and 'phone_numbers' are sorted based on the 'names' array in ascending order.
Comments
Post a Comment