【问题标题】:What is the time complexity of this algorithm(code)?这个算法(代码)的时间复杂度是多少?
【发布时间】:2016-10-30 12:08:33
【问题描述】:

我有将六种类型的 XPath 查询转换为 SQL 查询的算法。所以,我的代码包含 If-elseif-else 语句(多个 if)。我从互联网上读到,If-elseif-else 语句的时间复杂度是具有更多处理的 if 之一的最坏情况时间。我需要知道这段代码的时间复杂度是多少:

} else if (Query_Type == 5){
for (int i = strXPathQuery.length()-1; i > 0; i--) {
        if (strXPathQuery.charAt(i) == '/') {
             position = i;
             break;
        }    
 }   // end for loop                              
 Last_Node = strXPathQuery.substring(position+1);
 strAncestor_Path = ""; 
 int bracket_pos=0;
 for (int i = 0; i < position; i++) {
   if (strXPathQuery.charAt(i) == '[') {
          bracket_pos = i;
          break;
  } else if (strXPathQuery.charAt(i) == '/' && strXPathQuery.charAt(i+1) == '/')  {
           strAncestor_Path = strAncestor_Path + "%";
  }
  else {
            strAncestor_Path = strAncestor_Path + strXPathQuery.charAt(i);
  }  // end if statement
 }   // end for
    int operator_pos = 0; 
    String Node_condition=""; 
    for (int i = bracket_pos+1; i < position-2; i++) {
      if ((strXPathQuery.charAt(i) == '<') || (strXPathQuery.charAt(i) ==  '>') || (strXPathQuery.charAt(i) == '=') || (strXPathQuery.charAt(i) == '!')) {
              operator_pos = i;
              break;
                }
        else {
            Node_condition = Node_condition + strXPathQuery.charAt(i);
        }   // end if            }  
    String Value_condition=""; 
    for (int i = operator_pos; i < position-1; i++) {
        Value_condition = Value_condition + strXPathQuery.charAt(i);
    }  // end for loop 
    strSQLQuery = "SELECT L2.Node_Value \n" +
                  "FROM Leaf_Node L1, Leaf_Node L2, Ancestor_Path P\n" +
                  "WHERE P.Ances_PathExp LIKE '" + strAncestor_Path + "'\n" +
                  "AND L1.Ances_PathID = P.Ances_PathID \n" +
                  "AND L1.Node_Name = '" + Node_condition + "'\n" +
                  "AND L1.Node_Value '".replace("'", "") + Value_condition + "'\n".replace("'", "") +
                  "AND L2.Node_Name = '" + Last_Node + "'\n" +
                  "AND L1.Ances_PathID = L2.Ances_PathID \n" +
                  "AND L1.Ances_Pos = L2.Ances_Pos " ;
    txtSQLQuery.setText(strSQLQuery);
        }
        } 

【问题讨论】:

  • 是什么阻止你计算它?还是让其他人在您的代码中跋涉更容易?

标签: java algorithm computer-science


【解决方案1】:

你有三个可能是O(N^2)。例如。

 for (int i = 0; i < position; i++) {
      ...
      strAncestor_Path = strAncestor_Path + strXPathQuery.charAt(i);
      ...
 }

假设(最坏情况)对于该循环,position 的值是 strXPathQuery.length() ... 或 N。这意味着您将一个字符附加到同一字符串 N 次。由于将字符附加到字符串是 O(N) 操作。 (追加创建一个新字符串,复制现有字符串中的所有字符。)这样做的复杂性N 次是O(N^2)

平均复杂度可能比这更好,但这取决于输入。

(而且我没有耐心了解您在这里实际尝试的内容。您的代码的废话风格让我的眼睛受伤。)


如果你想执行性能,不要构建这样的字符串。使用StringBuilder

 StringBuilder path = new StringBuilder();

 for (int i = 0; i < position; i++) {
      ...
      path.append(strXPathQuery.charAt(i));
      ...
 }

【讨论】:

  • 谢谢。此代码处理一个输入条件,但还有另外 5 个输入条件。我的问题是具有 if-elseif 语句的代码的时间复杂度是多少。我在这里展示了 if-elseif-statement 的部分,它比其他部分有更多的操作。所以,我们可以根据这段代码计算算法的时间复杂度。你确定时间复杂度是O(n^2)
  • 我确信最坏的情况是O(N^2)。最好的情况是O(N)。平均而言……这取决于输入。
猜你喜欢
  • 2015-06-12
  • 1970-01-01
  • 2017-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多