简体   繁体   English

分析此方法

[英]Analyse this Method

Im having a problem analyzing this method. 我在分析此方法时遇到问题。 Im trying to figure out the big Oh complexity. 我试图找出很大的复杂性。 If I'm right it is O(n^2) hence the two for loops, which is the domninant joint of the algorithm. 如果我是对的,那么它是O(n ^ 2),因此是两个for循环,这是该算法的主要联合。 But i can't seem to figure out how to prove it. 但我似乎无法弄清楚如何证明这一点。

The thing i got this far is. 我到目前为止已经知道了。

1+n*((n-1)+1)/2+n. 1 + n *(((n-1)+1)/ 2 + n。 But when i test this it cant be right, can it? 但是,当我测试此方法时,它不可能正确吗?

Text is in norwegian, if you have any problem with understanding the code just yell out. 如果您对理解代码有任何疑问,请使用挪威语文字。 (Should code in english but because our teachers is giving all the material in norwegian, they want it back in norwegian. Hence the english and norwegian text :P ) (应该用英语编码,但由于我们的老师以挪威语授课,所以他们希望用挪威语返回。因此,英语和挪威语文字:P)

public void skrivUtStat(){
    LinearNode<CD> temp = start;
    int[] amt = new int[CD.GenreAmt()];
    String[] genre = CD.sendGenre();

    if(amount != 0){
        for(int i=0; i<amount; i++){
            for(int j=0; j<CD.genreAmount(); j++){
                if(temp.getElement().getGenreNr() == j){
                    ant[j] += 1;
                }
            } // End inner for-loop
            temp = temp.GetNext();                              // O(1)
        }// End outer for-loop

        for(int a=0; a<CD.GenreAmt(); a++){
            System.out.println(Genre[a] + ":");
            System.out.println(ant[a]);
        }
    }
}

Edited to english now. 现在已编辑成英文。

I understand that CD.genreAmount() is O(1) and you have a fixed list of genres. 我知道CD.genreAmount()是O(1),并且您有固定的类型列表。 Right? 对?

In that case you're iterating over all of your CD's ( for i ) and checking each one against your entire genre global listing ( for j ). 在这种情况下,您要遍历所有CD( for i ),并对照整个流派全局列表( for j )检查每张CD。 So: 所以:

Being: 存在:

  • N = CD count N = CD数量
  • M = Genre count M =流派数

Order will by O(NM) 订单由O(NM)

Of course: your algorithm is iterating over you genres again (at the end of method), but that's will be NM + M , and of course O(M) is always lower than O(NM)... so O(N.M+M) is equivalent to O(NM) 当然:您的算法会再次遍历您的流派(在方法末尾),但这将是NM + M ,当然O(M)始终低于O(NM)...因此O(N.M+M)等于O(NM)

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

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