繁体   English   中英

寻找程序的时间复杂性

[英]Finding time complexity of a program

我正在解决以下编程问题:

Given a sorted integer array and a number, find the start and end indexes of the number in the array. 

Ex1: Array = {0,0,2,3,3,3,3,4,7,7,9} and Number = 3 --> Output = {3,6} 
Ex2: Array = {0,0,2,3,3,3,3,4,7,7,9} and Number = 5 --> Output = {-1,-1} 

Complexity should be less than O(n)

我的解决方案如下:

public static void  findStartEndIndex(int[] arr, int elem) {

      int elemIndex = binarySearch(arr, elem, 0, arr.length);

      if(elemIndex == -1)
         System.out.println("-1,-1");

      int lowIndex = elemIndex - 1;
      int highIndex = elemIndex + 1;

      //Find the first index in the lower half of the array

      while(lowIndex >= 0 && (elemIndex = binarySearch(arr, elem, 0, lowIndex)) != -1)
         lowIndex = elemIndex -1;

      //Find the last index in the upper half of the array
      while(highIndex < arr.length && (elemIndex = binarySearch(arr, elem, highIndex, arr.length - 1)) != -1)
         highIndex = elemIndex + 1;

      System.out.println((lowIndex + 1) + ", " + (highIndex -1));

   }

我很难找到上述程序的时间复杂度。 以下是我的方法:

根据我的理解,最糟糕的情况是所有元素都相同: {3,3,3,3,3,3}找到第一次出现将花费我: O(logn) //Binary Search

对于数组的每一半(上部和下部),我最多在O(logn)次调用binarySearch。 因此,总复杂度将为O(logn ^2)

这是正确的分析吗? 并且, O(logn^2)O(n)更好吗?

O(log2n) = O(log2+logn)
Now log2 is a constant. 
So O(log2n) = O(log2+logn) = O(1) + O(logn) = O(logn)

但是你的代码会对二进制搜索给定整数的任何出现。 < - log(n)
然后它找出它最左边和最右边的出现。 < - log(a)+log(b)其中第一次出现在aa+b = n

总复杂度为O(log(n)+log(a)+log(b)) = O(log(n*a*b)) = O(log(n))

编辑:等待! 我误读了你的代码。 找到您发现的第一个事件后,可以在O(logn)时间内找到最左侧和最右侧。

基本上,您可以跳过查找任何事件的第一部分,并且可以在O(logn)中完成 你必须给出这样的条件:

while A[i] != q or A[i-1] != A[i]:
    if A[i] < q: low = i + 1
    else: high: = i - 1

在结束循环之后, i将是最左边的q

while A[i] != q or A[i+1] != A[i]:
    if A[i] > q: high = i - 1
    else: low = i + 1

在结束循环之后, i将是q最正确的出现。

lowhigh是指向您查找查询的位置的索引,每个步骤i = low+high/2

警告:您只需要处理其他一些情况,这样i不会超出[0..length of list-1]或者[0..length of list-1]中没有q

两个部分都需要O(logn)时间,所以总时间复杂度为O(logn) ,比O((logn)^2)O((logn)^2)

关于复杂性:

如果你的意思是O((log(n))^2)

定义m = log(n) ,得到:

O((log(n))^2) = O(m^2)

O(n) = O(e^log(n)) = O(e^m)

其中显示O((log(n))^2)渐近地优于O(n)

如果你的意思是O(log(2n)

O(log(2n) = O(log(2)+log(n)) = O(log(n))

所以它也比O(n)

我用这种方式写了程序

int[] secondarr = new int[]{0,0,2,3,3,3,3,4,7,7,9};
    int searchNum = 3;
    int min = -1,max=-1;
    int noOfTimesLoopExecuted = 0;
    for(int ind=0, stop=0;ind<secondarr.length && stop==0;ind++) {
        if(searchNum>=secondarr[ind]){
            if(searchNum==secondarr[ind]){
            if(min==-1){
                min=ind;
            } else {
                max=ind;
            }
            }
        } else {
            stop = 1;
        }
        noOfTimesLoopExecuted = ind;
    }
    System.out.println("start index:"+min+","+"end index:"+max);
    System.out.println("noOfTimesLoopExecuted:"+noOfTimesLoopExecuted);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM