Jump to content

[JAVA] Linear Search


Azain
 Share

Recommended Posts

In this Tutorial, I will show you how to perform Linear Search on an Array in Java, I assume that you know the basics of programming and Java language.

What is Linear Search?

In computer science, a linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched. Wikipedia

 

Why Linear Search?

Linear search is usually very simple to implement, and is practical when the list has only a few elements, or when performing a single search in an un-ordered list. When many values have to be searched in the same list, it often pays to pre-process the list in order to use a faster method. For example, one may sort the list and use binary search, or build an efficient search data structure from it. Should the content of the list change frequently, repeated re-organization may be more trouble than it is worth. As a result, even though in theory other search algorithms may be faster than linear search (for instance binary search), in practice even on medium-sized arrays (around 100 items or less) it might be infeasible to use anything else. On larger arrays, it only makes sense to use other, faster search methods if the data is large enough, because the initial time to prepare (sort) the data is comparable to many linear searches. Wikipedia

 

How to do Linear Search?
To do Linear Search on an array, we have to traverse through each element until we have found the element, we were searching for.
 

1. We'll create a method that takes an integer array and a key as input

public static int linearSearch(int arr[], int key) 
    {
  	
    }

2. Declare two variables, one for storing the index and other for storing the length of the array
 

//In Java, you can get length of an array by typing .length after the name of array
int index, length = arr.length;

3. To traverse through each element of the array, we will use a for loop, initialize the index to 0 and keep incrementing by 1, inside the the loop there is a condition if indexth element of array is equal to the key then we'll return the index, otherwise if we have traversed through each element and have not found the key then we'll return -1;
 

for (index = 0; index < arr.length; index++) 
        {
            if (arr[index] == key) 
            {
                return index;
            }
        }

        return -1;


That's how you can do linear search on an array, following is the full code
 

package com.azain.algorithms;

/**
 *
 * @author Azain
 */

public class Search {

    //Linear Search
    public static int linearSearch(int arr[], int key) 
    {
        int index, length = arr.length;
        for (index = 0; index < arr.length; index++) 
        {
            if (arr[index] == key) 
            {
                return index;
            }
        }

        return -1;
    }

}

 

Link to comment
Share on other sites

  • Tupi locked this topic
Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.