Home > Enterprise >  How to count steps for complete the linear Search
How to count steps for complete the linear Search

Time:11-25

I have an array,I Search one element using linear Search.But I want to count after how many steps I got the result. But I am not able to count the steps ,I am able to only search the element from the array,But not able to find the steps. LinearSearhc.java

public class ArrayRotation {
public static int linearSearch(int []arr,int x){
    int n = arr.length-1;
    for (int i=0;i<=n;i  ){
        if (arr[i]== x)
            return i;
    }
    return -1;
}

    public static void main(String[] args) {
        ArrayRotation arrRotation = new ArrayRotation();
        int arr[]={4,56,44,152,54,845};
        int x = 26;
        int result = linearSearch(arr,x);
        if (result == -1)
            System.out.println("searching element not Present in this array");
        else
            System.out.println("Searching element present at the  index position"  result);




    }
}

CodePudding user response:

Since we cannot return 2 values in a method in Java, we can instead return a Array with 2 values, one being if the linear search found the element and other being the amount of steps. Change the linearSearch Method to

public static int[] linearSearch(int []arr,int x){
    int steps = 0;
    int[] result = {-1,0};
    int n = arr.length-1;
    for (int i=0;i<=n;i  ){
        steps  ;
        if (arr[i]== x) {
            result[0] = i;
            break;
        }        
    }
    result[1] = steps;
    return result;
}

Then change the code in the Main Method to take in a array as result and the first index will be the Integer if found and the second will be the amount of steps. Example

public static void main(String[] args) throws IOException {
    
     int arr[]={4,56,44,152,54,845};
     
     int[]result = linearSearch(arr,54);
        if (result[0] == -1)
            System.out.println("searching element not Present in this array");
        else
            System.out.println("Searching element present at the  index position "  result[0] " in " result[1] " steps");       

}

CodePudding user response:

Since is a linear search it will search all the elements to say -1 or stop when it finds it

you can count the steps adding a counter inside the for loop

    class Playground {

    public static void main(String[] args) {
        ArrayRotation arrRotation = new ArrayRotation();
        int arr[]={4,56,44,152,54,845};
        int x = 26;
        int result = arrRotation.linearSearch(arr,x);
        if (result == -1)
            System.out.println("searching element not Present in this array");
        else
            System.out.println("Searching element present at the  index position "  result);

    }
}

public class ArrayRotation {

    public static int linearSearch(int []arr, int x){
    int n = arr.length-1;
    int counter = 0;
    int position = -1;

    for (int i=0; i <= n; i  ) {
        counter  ;
        if (arr[i] == x) {
            position = i;
            break;
        }
    }
    
    System.out.println("searching element stop after counting "   counter);


    return position;
    }
}
  • Related