0% found this document useful (0 votes)
3 views

Algorithms

Linear search is a straightforward algorithm that checks each element in an array for a match with a target value. It involves iterating through the array, comparing each element until a match is found or the end is reached, returning the index of the match or -1 if not found. An example code implementation in Java is provided to illustrate the algorithm's functionality.

Uploaded by

priyanka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Algorithms

Linear search is a straightforward algorithm that checks each element in an array for a match with a target value. It involves iterating through the array, comparing each element until a match is found or the end is reached, returning the index of the match or -1 if not found. An example code implementation in Java is provided to illustrate the algorithm's functionality.

Uploaded by

priyanka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Linear search is a simple search algorithm that works by iterating through

each element in an array until it finds a match.

Linear Search Algorithm

1. Start at the first element of the array.

2. Compare the target value to the current element.

3. If the target value matches the current element, return the index of the
current element.

4. If the target value does not match the current element, move to the
next element in the array.

5. Repeat steps 2-4 until the target value is found or the end of the array
is reached.

6. If the target value is not found, return a value indicating that the
element was not found (e.g. -1).

Example Code

```

int linearSearch(int[] array, int target) {

for (int i = 0; i < array.length; i++) {

if (array[i] == target) {

return i; // return the index of the target value

return -1; // return -1 if the target value is not found

```

You might also like