pub fn linear_search(array: &[i32], target: i32) -> Option<usize>
Expand description
Performs a linear search to find the index of a target value in an array.
This function iterates through each element in the array and compares it with the target. If a match is found, the index of the target element is returned.
§Arguments
array
- A reference to a slice of integers where the target value is searched.target
- The integer value to search for in the array.
§Returns
Some(usize)
- If the target is found, returns the index of the target in the array.None
- If the target is not found, returnsNone
.
§Examples
use dsa::algorithms::searching::linear_search;
let array = [1, 2, 3, 4, 5];
assert_eq!(linear_search(&array, 3), Some(2));
assert_eq!(linear_search(&array, 6), None);