Skip to content

Commit fdda499

Browse files
committed
docs: improve LinearSearch Javadoc with steps, example and complexity
1 parent 2927595 commit fdda499

1 file changed

Lines changed: 32 additions & 28 deletions

File tree

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,51 @@
1-
/**
2-
* Performs Linear Search on an array.
3-
*
4-
* Linear search checks each element one by one until the target is found
5-
* or the array ends.
6-
*
7-
* Example:
8-
* Input: [2, 4, 6, 8], target = 6
9-
* Output: Index = 2
10-
*
11-
* Time Complexity: O(n)
12-
* Space Complexity: O(1)
13-
*/
141
package com.thealgorithms.searches;
152

163
import com.thealgorithms.devutils.searches.SearchAlgorithm;
4+
175
/**
18-
* Linear Search is a simple searching algorithm that checks
19-
* each element of the array sequentially until the target
20-
* value is found or the array ends.
6+
* LinearSearch - searches for a value in an array by checking each element one by one.
7+
*
8+
* I come from C and this is basically the same as a simple for loop with an if inside.
9+
* The idea is straightforward: start from index 0, compare each element to the target,
10+
* return the index if found, or -1 if we reach the end without finding anything.
11+
*
12+
* How it works step by step:
13+
* 1. We start at the first element (index 0)
14+
* 2. We compare it with the value we are looking for
15+
* 3. If it matches, we return its index
16+
* 4. If not, we move to the next element
17+
* 5. We repeat until we find it or we go through the whole array
18+
* 6. If nothing matched, we return -1 to indicate "not found"
2119
*
22-
* It works for both sorted and unsorted arrays.
20+
* Example:
21+
* array = [3, 7, 1, 9, 4], target = 9
22+
* - index 0: 3 != 9, continue
23+
* - index 1: 7 != 9, continue
24+
* - index 2: 1 != 9, continue
25+
* - index 3: 9 == 9, return 3
26+
*
27+
* array = [3, 7, 1, 9, 4], target = 5
28+
* - nothing matched -> return -1
2329
*
24-
* Time Complexity:
25-
* - Best case: O(1)
26-
* - Average case: O(n)
27-
* - Worst case: O(n)
30+
* Time complexity: O(n) because in the worst case we check every element.
31+
* Space complexity: O(1), we do not use any extra memory.
2832
*
29-
* Space Complexity: O(1)
33+
* Note: unlike binary search, this works on unsorted arrays too.
3034
*
3135
* @author Varun Upadhyay
3236
* @author Podshivalov Nikita
3337
* @see BinarySearch
3438
* @see SearchAlgorithm
3539
*/
36-
3740
public class LinearSearch implements SearchAlgorithm {
3841

3942
/**
40-
* Generic Linear search method
43+
* Goes through the array and returns the index of the target value.
44+
* Returns -1 if the value is not in the array.
4145
*
42-
* @param array List to be searched
43-
* @param value Key being searched for
44-
* @return Location of the key
46+
* @param array the array we search in
47+
* @param value the value we are looking for
48+
* @return index of value if found, -1 otherwise
4549
*/
4650
@Override
4751
public <T extends Comparable<T>> int find(T[] array, T value) {
@@ -52,4 +56,4 @@ public <T extends Comparable<T>> int find(T[] array, T value) {
5256
}
5357
return -1;
5458
}
55-
}
59+
}

0 commit comments

Comments
 (0)