簡體   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