Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Time 相依函数的时间复杂度_Time_Big O_Time Complexity_Asymptotic Complexity - Fatal编程技术网

Time 相依函数的时间复杂度

Time 相依函数的时间复杂度,time,big-o,time-complexity,asymptotic-complexity,Time,Big O,Time Complexity,Asymptotic Complexity,我有这个方法 public static void primeSort( String[] list, HashMap< Integer, ArrayList< String >> hm ){ for( int x=0; x<list.length; x++ ){ if( list[ x ] == null ) continue; String curX = list[ x ]; int hashX = primeHa

我有这个方法

public static void primeSort( String[] list, HashMap< Integer, ArrayList< String >> hm ){

  for( int x=0; x<list.length; x++ ){
    if( list[ x ] == null ) continue;

    String curX    = list[ x ];
    int    hashX   = primeHash( curX );

    hm.put( hashX, new ArrayList( Arrays.asList( curX )));

    for( int y=x+1; y<list.length; y++ ){

      String curY    = list[ y ];
      int    hashY   = primeHash( curY );

      if( curY == null || curY.length() != curX.length())  continue;

      if( hashX == hashY ){
        hm.get( hashX ).add( curY );

        list[ y ] = null; // if its an anagram null it out to avoid checking again
      }
    }
  }
}
目标是获取一个字符串列表并将其排序为字谜,返回一个包含分组到列表中的字谜的列表

我试图确定时间复杂度,但这有点棘手。 primeSort将是最坏情况O(n^2)和最佳情况O(n)

primeHash将在每次调用时运行
m
次,其中
m
是当前字符串的长度。我不确定这将如何分析,以及如何与primeSort的分析相结合

感谢您的帮助:)

由于嵌套循环,只有在第一个循环中才会出现
O(n^2)
。然后在每个循环中调用
primeHash
。这里还有一个循环。设
m
为字符串单词的长度。然后在WC中,您会得到
O((n*m)^2)
。然而,m很可能是不同的,这就是为什么我取最长的一个,比如说
m
,进行渐近分析。另外
O((n*M)^2)
。假设WC中的
M
可以接近
n

您可以使用WC:
O((n^2)^2)=O(n^4)

在最佳情况下:在第一个函数中只有一个循环迭代:
O(n)
。然后我们对每个单词的长度进行假设。如果它们只有一个字符,那么在
primehash
中只有一个for循环迭代,或者只是常量。因此
O(1)

BC:
O(n)

由于嵌套循环,只有在第一个循环中才是
O(n^2)
。然后在每个循环中调用
primeHash
。这里还有一个循环。设
m
为字符串单词的长度。然后在WC中,您会得到
O((n*m)^2)
。然而,m很可能是不同的,这就是为什么我取最长的一个,比如说
m
,进行渐近分析。另外
O((n*M)^2)
。假设WC中的
M
可以接近
n

您可以使用WC:
O((n^2)^2)=O(n^4)

在最佳情况下:在第一个函数中只有一个循环迭代:
O(n)
。然后我们对每个单词的长度进行假设。如果它们只有一个字符,那么在
primehash
中只有一个for循环迭代,或者只是常量。因此
O(1)

卑诗省:
O(n)

public static int primeHash( String word ){
  int productOfPrimes = 1;
  int prime[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 
    37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101 };

  for( char ch : word.toCharArray() ){
    productOfPrimes *= prime[ (int) ch - (int) 'a' ];
  }
  return productOfPrimes;
}