Linear Search
In computer science, linear search or sequential search is a method for finding a target value within a list. It sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched.
Credit : Tutorials Point |
Here are the java programs for Linear Search.
1. Using IO package
/* Java Program Example - Linear Search */
import java.io.*;
class LinearSearch
{
public static void main(String arg[]) throws IOException
{
int n, i, search, position, array[];
boolean flag;
position=0;
flag=false;
DataInputStream in=new DataInputStream(System.in);
System.out.println();
System.out.println("Linear Search");
System.out.println("================");
System.out.println();
System.out.print("Enter the array size ");
n=Integer.parseInt(in.readLine());
array=new int[n];
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
{
array[i]=Integer.parseInt(in.readLine());
}
System.out.println();
System.out.print("Enter the search number ");
search=Integer.parseInt(in.readLine());
System.out.println();
for(i=0;i<n;i++)
{
if (array[i]==search)
{
flag=true;
position=i;
break;
}
}
if(flag==true)
System.out.print("Search number is found in "+position+"rd position");
else
System.out.print("Search number is not found in the array elements");
System.out.println();
}
}
import java.io.*;
class LinearSearch
{
public static void main(String arg[]) throws IOException
{
int n, i, search, position, array[];
boolean flag;
position=0;
flag=false;
DataInputStream in=new DataInputStream(System.in);
System.out.println();
System.out.println("Linear Search");
System.out.println("================");
System.out.println();
System.out.print("Enter the array size ");
n=Integer.parseInt(in.readLine());
array=new int[n];
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
array[i]=Integer.parseInt(in.readLine());
}
System.out.println();
System.out.print("Enter the search number ");
search=Integer.parseInt(in.readLine());
System.out.println();
for(i=0;i<n;i++)
if (array[i]==search)
{
flag=true;
position=i;
break;
}
}
if(flag==true)
System.out.print("Search number is found in "+position+"rd position");
else
System.out.print("Search number is not found in the array elements");
System.out.println();
}
}
2. Using Scanner utility
/* Java Program Example - Linear Search */
import java.util.Scanner;
class LinearSearch
{
public static void main(String arg[])
{
int n, i, search, position, array[];
boolean flag;
position=0;
flag=false;
Scanner in=new Scanner(System.in);
System.out.println();
System.out.println("Linear Search"); System.out.println("================"); System.out.println();
System.out.print("Enter the array size "); n=in.nextInt();
array=new int[n];
System.out.println("Enter the array elements"); for(i=0;i<n;i++)
{
array[i]=in.nextInt();
}
System.out.println();
System.out.print("Enter the search number ");
search=in.nextInt();
System.out.println();
for(i=0;i<n;i++)
{
if (array[i]==search)
{
flag=true;
position=i;
break;
}
}
if(flag==true)
System.out.print("Search number is found in "+position+"rd position");
else
System.out.print("Search number is not found in the array elements");
System.out.println();
}
}
import java.util.Scanner;
class LinearSearch
{
public static void main(String arg[])
{
int n, i, search, position, array[];
boolean flag;
position=0;
flag=false;
Scanner in=new Scanner(System.in);
System.out.println();
System.out.println("Linear Search"); System.out.println("================"); System.out.println();
System.out.print("Enter the array size "); n=in.nextInt();
array=new int[n];
System.out.println("Enter the array elements"); for(i=0;i<n;i++)
{
array[i]=in.nextInt();
}
System.out.println();
System.out.print("Enter the search number ");
search=in.nextInt();
System.out.println();
for(i=0;i<n;i++)
{
if (array[i]==search)
{
flag=true;
position=i;
break;
}
}
if(flag==true)
System.out.print("Search number is found in "+position+"rd position");
else
System.out.print("Search number is not found in the array elements");
System.out.println();
}
}
When the above Java Program is compiled and executed, it will produce the following output.
Output
Linear Search
================
Enter the array size 5
Enter the array elements
1
3
2
5
4
Enter the search number 5
Search number is found in 3rd position
================
Enter the array size 5
Enter the array elements
1
3
2
5
4
Enter the search number 5
Search number is found in 3rd position
No comments:
Post a Comment