- 1. Program of Linear searching using python.
- def linear_search(arr, target):
- for i in range(len(arr)):
- if arr[i] == target:
- return i # Return the index of the target if found
- return -1 # Return -1 if the target is not found
- my_list = input("Enter the n elements: ")
- target_value = input("Enter the elements to be searching in list: ")
- result = linear_search(my_list, target_value)
- if result != -1:
- print(f"Target value {target_value} found at index {result}")
- else:
- print(f"Target value {target_value} not found in the list")
- OUTPUT :
- Enter the n elements: 1 2 3 4 23 34 45 56 67 78 89 90
- Enter the elements to be searching in list: 2
- Target value 2 found at index 2
- 2. Program of Binary Searching using python .
- def binary_search(arr, target):
- low = 0
- high = len(arr) - 1
- while low <= high:
- mid = (low + high) // 2
- mid_value = arr[mid]
- if mid_value == target:
- return mid # Return the index of the target if found
- elif mid_value < target:
- low = mid + 1
- else:
- high = mid - 1
- return -1 # Return -1 if the target is not found
- # Example usage
- my_list = [1, 2, 3, 4, 5, 8, 9, 10, 12, 16] #user create own list.
- target_value = 8 #search user desire elements
- result = binary_search(my_list, target_value)
- if result != -1:
- print(f"Target value {target_value} found at index {result}")
- else:
- print(f"Target value {target_value} not found in the list")
- OUTPUT:
- Target value 8 found at index 5
Searching techniques in python
Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated