For each of the following algorithms, give its worst-case and best-case asymptotic runtime class.
Let = side_length
and give your answer in terms of .
/** Print a multiplication table listing the pairwise products
* of all integers in 0..side_length. */
public static void multTable(int side_length) {
for (int i = 0; i < side_length; i++) {
for (int j = 0; j < side_length; j++) {
System.out.print(i*j);
System.out.print(" ");
}
System.out.println();
}
}
Give your answer in terms of n
.
Let = A.length
and give your answer in terms of .
Give your answer in terms of n
.
Let = height
and give your answer in terms of .
/** Print an ASCII art picture of a width-6 ladder with the given number of rungs.
* For example, printLadder(4) prints:
* |----|
* |----|
* |----|
* |----|
*/
public static void printLadder(int height) {
for (int i = 0; i < height; i++) {
System.out.print("|");
for (int j = 0; j < 4; j++) {
System.out.print("-");
}
System.out.println("|");
}
}
Assume that this method has access to the linearSearch
method we’ve discussed. Let = A.length
and give your answer in terms of n
and .
Let = side_length
and give your answer in terms of .
/** Print a lower-triangular multiplication table listing the products i*j
* for all pairs i <= j in 0..side_length. */
public static void multTable(int side_length) {
for (int i = 0; i < side_length; i++) {
for (int j = 0; j < side_length; j++) {
if (i <= j) {
System.out.print(i*j);
System.out.print(" ");
}
}
System.out.println();
}
}
Give your answer in terms of n
.