1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/// 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, returns `None`.
///
/// # 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);
/// Performs a binary search to find the index of a target value in a sorted array.
///
/// This function assumes that the array is sorted in ascending order. It works by
/// repeatedly dividing the search interval in half. If the target is less than
/// the middle element, the search continues in the left half, and if the target
/// is greater, the search continues in the right half. This process continues
/// until the target is found or the search interval is empty.
///
/// # Arguments
///
/// * `array` - A reference to a sorted 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, returns `None`.
///
/// # Examples
///
/// ```
/// use dsa::algorithms::searching::binary_search;
///
/// let array = [1, 2, 3, 4, 5];
/// assert_eq!(binary_search(&array, 3), Some(2));
/// assert_eq!(binary_search(&array, 6), None);
/// ```