对于一个类,我必须编写一个处理表格的程序。我做了困难的部分,但现在我需要为它写一个算法复杂度分析。我基本上了解这是什么,我可以将其应用于循环等简单的事情,但教授想要对整个代码进行分析。我该怎么做呢?
第一的:
// CARTESIAN PRODUCT
// headers first
for (int i = 0; i < relation1[0].length; i++) {
cartProd[0][i] = relation1[0][i];
}
for (int i = 0; i < relation2[0].length; i++) {
cartProd[0][(i + relation1[0].length)] = relation2[0][i];
}
// Merge rows, but skip the first row of everything (headers)
for (int z = 1; z < relation1.length; z++) {
for (int i = 1; i < relation2.length; i++) {
for (int j = 0; j < relation2[0].length + relation1[0].length; j++) {
int cartRow = (i + ((relation2.length - 1) * (z - 1)));
if (j < relation1[0].length) {
cartProd[cartRow][j] = relation1[z][j];
} else {
cartProd[cartRow][j] = relation2[i][j - relation1[0].length];
}
}
}
}
System.out.println("\nCartesian Product: \n"
+ Arrays.deepToString(cartProd).replaceAll("],", "]," + System.getProperty("line.separator")));
第二:
for (int i = 1, colCount = 0; i < relation1.length; i++) {
for (int j = 0; j < relation1[0].length; j++) {
if (relation1[0][j].equals(commonColumnName)) {
commonWord = relation1[i][j];
for (int x = 0; x < relation1[0].length; x++) {
if (CurrentColumnValid(relation1[0][x], header_natural)) {
naturalJoinString += ((relation1[i][x]) + " ");
if(i==1) {
myNaturalHeaders[colCount]=relation1[0][x]; colCount++;
}
}
}
// Right
for (int a = 0; a < relation2.length; a++) {
for (int b = 0; b < relation2[0].length; b++) {
if (relation2[0][b].equals(commonColumnName) && relation2[a][b].equals(commonWord)) {
for (int c = 0; c < relation2[0].length; c++) {
if (CurrentColumnValid(relation2[0][c], header_natural)
&& (!relation2[a][c].equals(commonWord))) {
naturalJoinString += ((relation2[a][c]) + " ");
if(i==1) {
myNaturalHeaders[colCount]=relation2[0][c]; colCount++;
}
}
}
}
}
}
}
}
}
问题是,我很确定我把它弄得尽可能复杂。据我所知,我编写程序的方式是防弹测试的,所以我想保留它。但是,我不知道如何为整个程序编写复杂性分析,更不用说四个 for 循环了:/