Brute force

linear search

Pinterest LinkedIn Tumblr

Linear search is a method to find the particular item in an array. It compares each item with search item until it finds. The linear search performs on non-sorted items in the array.

Linear search is a simple search algorithm to find the items from the unsorted array. If the array sorted, the developer can use Binary search which search the item in less number of comparison.

Write a program to implement linear search.

source Code

package com.dsacode.Algorithm.search;
 
import java.util.Arrays;
 
public class LinearSearch {
 
    public static int search(int a[], int x){
        for(int i = 0; i < a.length; i++){
            if(a[i] ==x)
                return i+1;
        }
        return -1;
    }
    public static void main(String[] args) {
         int[] array = {12, 55, 45, 11, 23, 20, 17, 24, 9};
         System.out.println("List of items in the array: "+ Arrays.toString(array));
         int pos = -1;
         System.out.println("Search '12' in the array using linear search");
         if( (pos= search(array,12)) == -1)
             System.out.println("Not found!");
         else
             System.out.println("Found in " + pos + " Position!");
    }
}

Output

List of items in the array: [12, 55, 45, 11, 23, 20, 17, 24, 9]
Search '12' in the array using linear search
Found in 1 Position!

Algorithm Explanation

Linear search takes each item from the list sequentially and compare with search item.
If the search process finds the item in an array, it returns the item or position.
Otherwise, it return -1.

Time Complexity

Best CaseAverage CaseWorst Case
O(1)O(n/2)O(n)

Reference

  1. http://en.wikipedia.org/wiki/Linear_search
  2. http://www.cs.toronto.edu/~andria/csc/108s01/lecture/slides/lec12_complexity.html

17 Comments

  1. Nicely done & written my friend!
    I’ve just started writing a blog veery recently and realised that lot of articles simply rework old ideas but
    add very little of worth. It’s great to read an informative
    write-up of some genuine value to me.
    It’s going down oon the list of creteria I need to replicate being a
    new blogger. Audience engagement and content quality are king.

    Many good thoughts; you’ve certainly made it on my
    list of peopple to watch!

    Keep upp the great work!
    Well done,
    Kitti

  2. Does your site have a contact page? I’m having trouble
    locating it but, I’d like to send you an e-mail. I’ve got some ideas for
    your blog you might be interested in hearing. Either way,
    great site and I look forward to seeing it expand over time.

  3. It’s not my first time to visit this site, i am visiting this website
    dailly and obtain good data from here daily.

  4. I was recommended this web site by my cousin. I’m not positive whether or not
    this post is written by means of him as nobody else recognize such detailed approximately my problem.
    You’re wonderful! Thanks!

  5. Great beat ! I would like to apprentice while you amend your web site,
    how can i subscribe for a blog web site? The account aided me a
    acceptable deal. I had been tiny bit acquainted of this your broadcast offered bright clear concept

  6. I don’t know if it’s just me or if perhaps everyone else experiencing issues with your website.
    It appears as though some of the written text in your
    posts are running off the screen. Can someone else please comment and
    let me know if this is happening to them as well? This might
    be a issue with my web browser because I’ve had this happen before.
    Thank you

  7. Wonderful, what a weblog it is! This webpage presents helpful information to us,
    keep it up.

  8. Hello, Neat post. There is a problem with your website in internet explorer, might check this?
    IE nonetheless is the market leader and a large component of other people will
    pass over your magnificent writing due to this problem.

  9. It’s impressive that you are getting ideas from this post as
    well as from our discussion made here.

  10. I think this is one of the most significant information for me.
    And i’m glad reading your article. But should remark on few general things, The site style is ideal, the articles
    is really nice : D. Good job, cheers

  11. Oh my goodness! Impressive article dude! Thanks,
    However I am encountering issues with your RSS.

    I don’t know why I am unable to join it. Is there anybody else having the same RSS
    issues? Anybody who knows the solution can you
    kindly respond? Thanx!!

  12. This is very interesting, You are a very skilled blogger.

    I have joined your feed and look forward to seeking more of your great post.
    Also, I have shared your website in my social networks!

  13. Hello! Do you know if they make any plugins to safeguard against
    hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any suggestions?

Write A Comment