Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Friday 23 February 2018

Write a Java Program for Linear Search

Linear Search Program in Java

Linear Search Program in Java

Here, We will see how to write a java program for linear search algorithm step-by-step. Linear search algorithm is the most important topic and it is mostly asked in core java interviews.

Linear search is a very simple searching algorithm and it is also known as sequential search algorithm in java. By using linear search algorithm we can easily find particular elements or value in the list.

Linear search is used to search a target element from multiple elements and it is used to check if an element exists in the given list we compare it with every element in the list. If it is found then we print the location of an element at which it occurs.

Linear search algorithm is slower than binary search algorithm in java.

Let's understand, Java Linear search algorithm with a simple example.

Check other Java Programs for Practice.


Java Program for Linear Search

In this linear search program, We will take elements from the user and then we will search particular elements from multiple elements. Also take number of elements.

import java.util.Scanner;
class LinearSearchExample
{
public static void main(String args[])
{
int c, n, search, array[];

Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements");
n = sc.nextInt();
array = new int[n];

System.out.println("Enter " + n + " Integers");

for(c = 0; c < n; c++)
array[c] = sc.nextInt();

System.out.println("Enter value to find");
search = sc.nextInt();

for(c = 0; c < n; c++)
{
if(array[c] == search)
{
sop(search+ " is present at location " + (c + 1)+ " . ");

break;

}
}
if(c == n)
System.out.println(search+ "is not present in array");
}
}

Output: Enter number of elements
             5
             Enter 5 Integers
             47
             56
             30
             15
             89
             Enter value to find
             30
             30 is present at location 3


Java Linear Search Example 2

This is another java linear search example to find target value from the list.

package javatutorial95;
public class Demo2
{
public static int linearSearch(int[] arr, int key)
{
int size = arr.length;

for(int i = 0; i < size; i++)
{
if(arr[i] == key)
{
return i;
}
}
return -1;
}

public static void main(String args[])
{
int arr1[] = {50, 87, 66, 23};
int searchKey = 66;

System.out.println("key " + searchKey + " present at index " +linearSearch(arr1, searchKey));
}
}

Output: key 66 present at index 2

Read More:

Binary Search Program in Java.
Java Program to Check Leap Year or Not.
Java Program to Find Area of Square.
Some Java Basic & Important Programs.
Java String Conversion Examples

Here we saw linear search program in java by 2 simple examples.  
Share:

1 comment:

  1. I was more than happy to uncover this great site. I need to to thank you for your time due to this fantastic read!! I definitely enjoyed every bit of it and I have you bookmarked to see new information on your blog.
    Java Training in Bangalore

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate