【问题标题】:How to count steps for complete the linear Search如何计算完成线性搜索的步骤
【发布时间】:2021-11-25 05:01:59
【问题描述】:

我有一个数组,我使用线性搜索搜索一个元素。但我想 在我得到结果的步骤后数一数。但我无法 计算步骤,我只能从 数组,但是找不到步骤。 线性搜索.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);




    }
}

【问题讨论】:

  • 既然您正在执行线性搜索并返回项目的索引,那么您返回的内容不也表明步数吗? Number of steps = value returned + 1

标签: java linear-search


【解决方案1】:

由于我们不能在 Java 中的方法中返回 2 个值,因此我们可以返回一个包含 2 个值的数组,一个是线性搜索是否找到了元素,另一个是步数。 将线性搜索方法更改为

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;
}

然后更改 Main 方法中的代码以将数组作为结果,第一个索引将是 Integer(如果找到),第二个索引是步数。 示例

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");       

}

【讨论】:

    【解决方案2】:

    由于是线性搜索,它会搜索所有元素以说 -1 或在找到时停止

    您可以计算在 for 循环中添加计数器的步骤

        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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 2019-07-28
      • 2012-02-03
      • 2016-11-29
      • 2016-08-27
      • 2013-06-08
      相关资源
      最近更新 更多