what is Linear Search Algorithm
Linear Search Algorithm
Definition
How to find certain element in the list.
check all elements of the list one by one to search one element.
Time complexity
O(n)
Example
import java.util.NoSuchElementException;
public class Main {
public static void main(String[] args) {
int[] nums = {1, 2, 3, 2, 2, 5};
System.out.println(getIndexValueArr(nums, 5));
}
public static int getIndexValueArr(int[] input, int value) {
for (int i = 0; i < input.length; ++i) {
if (input[i] == value) {
return i;
}
}
throw new NoSuchElementException();
}
}
Explanation
step 1

step 2

step 3

step 4

step 5

0 개의 댓글:
댓글 쓰기