Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » ArraysRSS Feeds

Program to search an element in an array using Linear Search

Posted By: Easy Tutor     Category: Java     Views: 36469

A Java Program to search an element in an array using Linear Search.

Code for Program to search an element in an array using Linear Search in Java

publicclass JAVA_038
 {


    publicstaticvoid main(String[] args)
    {
       int[] array={10,-1,28,13,44,5,36,97,-18,11};

       System.out.println(" The contents of the Array are :");

       for(int i=0;i<array.length;i++)
          System.out.println("\t\t\t\t\t Array[" + i + "] = " + array[i]);

       int search_element=44;
       int find_index=-1;

       for(int j=0;j<(array.length-1);j++)
       {
          if(array[j]==search_element)
          {
             find_index=j;

             break;
          }
       }

       if(find_index!=-1)
       {
          System.out.println(" The search element is : " + search_element);
          System.out.println(" It is found in the array at position : " + find_index);
       }

       else
          System.out.println("\n The search element is not found in the array.");
    }
 }
  
Share: 



Easy Tutor
Easy Tutor author of Program to search an element in an array using Linear Search is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Yemi Ade from United States Comment on: Mar 03
Dear tutor,

This is not a comment but I need help to solve below problem

Start with this array of unsorted names. You can copy and paste it directly into your Java file. Do not change the names or the order in which they appear. Use this array exactly as it is here, otherwise your results will not match the expected results. DO NOT make this array global, I want you to pass it as a parameter to the methods that will use it.

String[] names = { "fred" , "barney", "tom", "jerry", "larry", "moe", "curly",
"betty" , "wilma", "bart", "homer", "marge", "maggie", "lisa",
"pebbles" , "bambam", "smithers", "burns", "milhouse", "george", "astro",
"dino" , "mickey", "minnie", "pluto", "goofy", "donald", "huey",
"louie" , "dewey", "snowwhite", "happy", "doc", "grumpy", "sneezy",
"dopey" , "sleepy", "bambi", "belle", "gaston", "tarzan", "jane",
"simba" , "scar", "mufasa", "ariel", "flounder", "bugs", "daffy",
"elmer" , "foghorn", "chickenhawk", "roger", "jessica", "hank", "bobby",
"peggy" , "spot", "pongo", "perdy", "buzz", "potatohead", "woody",
"chuckie" , "tommy", "phil", "lil", "angelica", "dill", "spike",
"pepe" , "speedy", "yosemite", "sam", "tweety", "sylvester", "granny",
"spiderman" , "batman", "superman", "supergirl", "robin", "catwoman","penguin",
"thing" , "flash", "silversurfer", "xmen", "pokemon", "joker", "wonderwoman"
};


Write your own Linear Search method that takes an array as a parameter and returns the number of comparisons to find the item:

int linearSearch ( String [ ] namesarray )
In the main method of the program write a simple loop that enters a name to search for and reports the number of comparisons it took to find that one name and an average count for ALL the searches conducted. When the user enters the name "exit" or "quit", leave the loop and the program will stop execution.

Example:

Enter a name to search for: tarzan
Linear search: 62 comparisons, average 42 comparisons per linear search


Do not make the variables used to calculate the average global, and make sure to use integer arithmetic ( no decimal point ). Test your program, obviously the average will match the first search, but verify your calculation for the second and third search using a calculator.

Do not move on to the next phase until you are sure your linearSearch and average calculation are working properly.

Thanks

View All Comments