我有算法將六種類型的XPath查詢轉換爲SQL查詢。所以,我的代碼包含If-elseif-else語句(多個if)。我從互聯網上讀到,If-elseif-else語句的時間複雜度是其中一個如果有更多處理的最壞情況時間。我需要知道什麼是對這個代碼的時間複雜度:這個算法(代碼)的時間複雜度是多少?
} 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);
}
}
什麼阻止你計算它?或者讓別人跋涉一下你的代碼更容易? – shmosel