Java Program for Linear Search Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 10 Likes Like Report Try it on GfG Practice Linear Search is the simplest searching algorithm that checks each element sequentially until a match is found. It is good for unsorted arrays and small datasets.Given an array a[] of n elements, write a function to search for a given element x in a[] and return the index of the element where it is present. If the element is not present in the array, then return -1.Input/Output:Input: a = [ 1, 2, 3, 5, 7], x = 3Output = Element found at index: 2Input a = [1, 2, 3, 5, 7] x = 8Output = -1Algorithm for Linear SearchStart Declare an array and search element as the key.Traverse the array until the number is found.If the key element is found, return the index position of the array elementIf the key element is not found, return -1Stop.Please refer to the complete article on Linear Search for more details.Linear Search in JavaExample: Java // Java code for linearly search x in arr[]. If x // is present then return its location, otherwise // return -1 class Geeks { static int search(int a[], int n, int x) { for (int i = 0; i < n; i++) { if (a[i] == x) return i; } // return -1 if the element is not found return -1; } public static void main(String[] args) { int[] a = { 3, 4, 1, 7, 5 }; int n = a.length; int x = 4; int index = search(a, n, x); if (index == -1) System.out.println("Element is not present in the array"); else System.out.println("Element found at index: " + index); } } OutputElement found at position 1 Time Complexity:Best Case Complexity: The best-case time complexity of the linear search is O(1).Average Case Complexity: The average case time complexity of the linear search is O(n).Worst-Case Complexity: The worst-case time complexity of the linear search is O(n).Space Complexity: O(1) Create Quiz Comment K kartik Follow 10 Improve K kartik Follow 10 Improve Article Tags : Searching Java Programs DSA Java-Arrays Linear Search +1 More Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 15 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 1 min read Problem of The Day - Develop the Habit of Coding 5 min read Like