Blog Detail

  • Sorting-and-Searching-MSA-Technosoft_83.jpg

    Data Structure- Linear Search

    What is searching and why do we need searching?

    Searching: Searching is the process of finding an item with specified properties from a collection of items.
    The items may be sorted as records in a database, simple data elements in arrays, text in files, nodes in trees, vertices and edges in graphs, or maybe elements of other search space.
    Searching is one of the core computer science algorithms.

    Why do we need Searching:
    We know that today's computers store a lot of information. To retrieve this information proficiently we need very efficient searching algorithms.
    There are certain ways of organizing the data which improves the searching process. That means, if we keep the data in proper order, it is easy to search the required element. Sorting is one of the techniques for making the elements ordered.

    Types of Searching: Following are the type of searches...

    Unordered Linear Search
    Sorted/Ordered Linear Search
    Binary Search
    Symbol Tables And Hashing
    String Searching Algorithms: Trees, Ternary Search and Suffix Trees.

    Linear Search:

    Linear search checks for a value within an array one by one until it finds it. In an unordered array, the linear search would have to check the entire array until it found the desired value. But ordered arrays, it is different. The reason is once a linear search finds a value larger than its desired value, then it can stop and say it found the value or not. Basically, ordered arrays take fewer steps and unordered arrays with linear search.

    Linear search is used to search a key element from multiple elements. Linear search is less used today because it is slower than binary search and hashing.

    liner search


    Step 1: Traverse the array
    Step 2: Match the key element with the array element
    Step 3: If the key element is found, return the index position of the array element
    Step 4: If the key element is not found, return -1

    public class LinearSearchExample{
        public static int linearSearch(int[] arr, int key){
            for(int i=0;i<arr.length;i++){
                if(arr[i] == key){
                    return i;
             return -1;
        public static void main(String a[]){
            int[] a1= {10,20,30,50,70,90};
            int key = 50;
            System.out.println(key+" is found at index: "+linearSearch(a1, key));

    50 is found at index: 3