Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/main/java/com/thealgorithms/searches/LinearSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.thealgorithms.searches;

import com.thealgorithms.devutils.searches.SearchAlgorithm;

/**
* Linear Search is a simple searching algorithm that checks
* each element of the array sequentially until the target
Expand All @@ -33,7 +34,6 @@
* @see BinarySearch
* @see SearchAlgorithm
*/

public class LinearSearch implements SearchAlgorithm {

/**
Expand All @@ -45,14 +45,17 @@ public class LinearSearch implements SearchAlgorithm {
*/
@Override
public <T extends Comparable<T>> int find(T[] array, T value) {
if (array == null || array.length == 0) {

if (array == null || array.length == 0 || value == null) {
return -1;
}

for (int i = 0; i < array.length; i++) {
if (array[i].compareTo(value) == 0) {
if (array[i] != null && array[i].compareTo(value) == 0) {
return i;
}
}

return -1;
}
}
Loading