Write a Java Program for Linear Search on unsorted array

By | 30.01.2017

Write a Java Program for Linear Search on unsorted array


To search any element present inside the array in Java Programming using linear search technique, you have to use only one for loop to check whether the entered number is found in the list or not as shown in the following program.

Following Java program first ask to the user to enter the array size then it will ask to enter the array elements, then it will finally ask to enter a number to be search in the given array to check whether it is present in the array or not, if it is present then the program will show the position of that number present in the array:

 

SOURCE CODE ::

 

import java.util.Scanner;

public class LinearSearch {

    //----------------Function to Sort Array-------------------
    
    static int[] sorted(int arr[])
    {
        int l,max,temp;
        l=arr.length;
        for(int i=0;i<l;i++)
        {
            for(int j=i+1;j<l;j++)
            {
                if(arr[i]>arr[j])
                {
                    temp=arr[j];
                    arr[j]=arr[i];
                    arr[i]=temp;
                }
            }  
        }
        return arr;        
    }
    
    //------------function to search key in array-------------
    
    static void search(int arr[],int key)
    {
        int i,l,flag=0,position=0;
        l=arr.length;
        for(i=0;i<l;i++)
        {
            if(arr[i]==key)
            {
                position=i+1;
                flag=1;
                break;
            }
        }
        if(flag==0)
        {
            System.out.println("Sorry,Key doesn't exist in the array");
        }
        else
        {
            System.out.println(key+ " found at position " + position);
        }
        
    }
    
    //-----------------------Main Menu------------------------------
    
    public static void main(String[] args) {
        
        int i,n,b[],key,c;
        System.out.print("Enter size of array: ");
        Scanner sc = new Scanner(System.in);
        n=sc.nextInt();
        int a[]=new int[n];
        
        //---------Enter values to array-------------
        
        for(i=0;i<n;i++)
        {
           System.out.print(i+1 +" Element : "); 
           a[i]=sc.nextInt();
        }
        //-----------------Sort the array--------------------
        
        b=sorted(a);
        
        System.out.println("\nSorted Array :-->");
        
        
        //-----------------------Display the Sorted Array-----------
        
        for(i=0;i<n;i++)
        {
           System.out.println(i+1 +" Element : "+a[i]);
        }
        
        //---------------Enter search key u want to find-----------
        
        System.out.print("\nEnter the number u want to search: ");
        
        key=sc.nextInt();
        
        search(b,key);       
        
    }
    
}

 

OUTPUT  ::

 

Enter size of array: 5

1 Element : 4
2 Element : 1
3 Element : 5
4 Element : 6
5 Element : 3

Sorted Array :-->

1 Element : 1
2 Element : 3
3 Element : 4
4 Element : 5
5 Element : 6

Enter the number u want to search: 3

3 found at position 2

 

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments