Searching Algorithms:
1.1 Linear Search with list
- Simple search algorithm that sequentially checks each element in a list until the target element is found or all elements are checked. Goes through each element sequentially.
- is straightforward but has a time complexity of O(n), where n is the number of elements in the list. This means it may not be efficient for large datasets compared to more advanced search algorithms like Binary Search (O(log n)) which require the data to be sorted. However, it is useful when the list is not sorted or when a simple implementation is needed.
- Simple to implement, but inefficient for large datasets.
- Good for unsorted lists.
Function defintion takes two arguments
arr
: The array (list) where you want to search.target
: The value you’re looking for in the array.
List Characteristics:
- Ordered collection of elements.
- Allows for duplicate elements.
- Supports indexing and iteration.
Why Use a List for Linear Search:
- Indexing: Lists in Python allow direct access to elements by their index. This makes it efficient to access elements sequentially during a linear search.
- Iteration: Lists support iteration using for loops, which is essential for scanning through each element to find the target.
- Ease of Implementation: Implementing a linear search in a list is straightforward and intuitive, utilizing basic indexing and looping constructs.
Step 1: Function Definition: The linear_search function takes two parameters: arr (the list to be searched) and target (the element to search for).
Step 2: Iteration: The function iterates over each element in the list using a for loop. The range(len(arr)) generates indices from 0 to len(arr) - 1.
Step 3: Comparison: Inside the loop, the function compares the current element (arr[index]) with the target. If they are equal, the function returns the current index.
Step 4: Return Statement: If the loop completes without finding the target, the function returns -1, indicating that the target is not in the list.
"""
Perform linear search to find the index of target in arr.
Returns the index if found, otherwise returns -1.
"""
def linear_search(arr, target):
# Iterate through each element in the list
for i in range(len(arr)):
# Check if the current element is equal to the target
if arr[i] == target:
# Return the index of the element if found
return i
# Return -1 if the target is not found in the list
return -1
# Example
arr = [10, 20, 30, 40, 50]
target = 30
result = linear_search(arr, target)
if result != -1:
print(f"Element {target} found at index {result}")
else:
print(f"Element {target} not found in the list")
1.2 Linear Search with a Dictionary
def linear_search_dict(d, target):
# Iterate through each key-value pair in the dictionary
for key, value in d.items():
# Check if the current value is equal to the target
if value == target:
# Return the key of the element if found
return key
# Return None if the target is not found in the dictionary
return None
# Example usage:
d = {'a': 10, 'b': 20, 'c': 30, 'd': 40, 'e': 50}
target = 30
result = linear_search_dict(d, target)
if result is not None:
print(f"Value {target} found with key '{result}'")
else:
print(f"Value {target} not found in the dictionary")
Step 1: Function Definition: The linear_search_dict function takes two parameters: d (the dictionary to be searched) and target (the value to search for).
Step 2: Iteration: The function iterates over each key-value pair in the dictionary using a for loop. The d.items() method returns a view object that displays a list of dictionary’s key-value tuple pairs.
Step 3: Comparison: Inside the loop, the function compares the current value (value) with the target. If they are equal, the function returns the corresponding key.
Step 4: Return Statement: If the loop completes without finding the target, the function returns None, indicating that the target is not in the dictionary.