Linear Search in Java with Examples

Linear Search is a simple search algorithm that scans the array sequentially and compares each element with the key (target value). If the key is found, it returns the index; otherwise, it returns -1.

This article shows you how the Linear search algorithm works with two examples to demonstrate the concept of Linear Search while catering to different use cases.

How Linear Search Works?

  1. Start at the first element of the array.
  2. Compare each element with the key.
  3. If a match is found, return the index of the element.
  4. If the end of the array is reached without finding the key, return -1.

1. Linear Search Program in Java (Basic example)

This program example demonstrates a straightforward Linear Search on a 1D array of integers.

package com.javacodepoint.search;

public class BasicLinearSearch {

	public static int linearSearch(int[] array, int key) {
		for (int i = 0; i < array.length; i++) {
			if (array[i] == key) {
				return i; // Key found, return the index
			}
		}
		return -1; // Key not found
	}

	public static void main(String[] args) {
		int[] numbers = { 5, 8, 12, 15, 20 };
		int key = 12;

		int result = linearSearch(numbers, key);

		if (result != -1) {
			System.out.println("Element found at index: " + result);
		} else {
			System.out.println("Element not found.");
		}
	}
}

2. Linear Search in Java Program (Advanced example)

This program example demonstrates Linear Search on a list of objects, where we search for a specific name in a list of Person objects.

package com.javacodepoint.search;

import java.util.ArrayList;
import java.util.List;

class Person {
	private String name;
	private int age;

	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	@Override
	public String toString() {
		return "Person{name='" + name + "', age=" + age + "}";
	}
}

public class AdvancedLinearSearch {
	public static Person linearSearch(List<Person> people, String key) {
		for (Person person : people) {
			if (person.getName().equalsIgnoreCase(key)) {
				return person; // Return the matching Person object
			}
		}
		return null; // Key not found
	}

	public static void main(String[] args) {
		List<Person> people = new ArrayList<>();
		people.add(new Person("Alice", 25));
		people.add(new Person("Bob", 30));
		people.add(new Person("Charlie", 35));

		String key = "Bob";

		Person result = linearSearch(people, key);

		if (result != null) {
			System.out.println("Person found: " + result);
		} else {
			System.out.println("Person not found.");
		}
	}
}

Key Differences Between the Two Examples

AspectBasic ExampleAdvanced Example
Data Type1D array of integersList of objects (Person)
Search TargetIntegerString (name property)
ResultIndex of the keyObject matching the search criteria
FlexibilityLimited to primitives or simple dataWorks with complex data structures

Benefits of Advanced Example

  1. Custom Objects: Works with any class or object, not just primitive types.
  2. Flexible Search Criteria: Can search by any property of the object.
  3. Real-World Applications: Suitable for business logic like searching for customers, employees, or products.

Linear Search Efficiency

Time Complexity: O(n).

You can explore other search algorithms like Binary Search, Interpolation Search, Fibonacci Search, Exponential Search, Ternary Search, and Jump Search.

Java logical programs list


Java Basic Programs

Java String Programs

Java Miscellaneous Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *