2.11. Discussion QuestionsΒΆ

  1. Give the Big-O performance of the following code fragment:
int count = 0;
for (int i = 0; i < n; i++){
   for (int j = 0; j < n; j++){
       count = count +1;
   }
}
  1. Give the Big-O performance of the following code fragment:
int count = 0;
for (int i = 0; i < n; i++){
   count = count + 1;
}
  1. Give the Big-O performance of the following code fragment:
int i = n;
int count = 0;
while (i > 0){
    count = count + 1;
    i = i / 2; //Note: integer division
}
  1. Give the Big-O performance of the following code fragment:
int count = 0;
for (int i = 0; i < n; i++){
    for (int j = 0; j < n; j++){
        for (int k = 0; k < n; k++){
            count = count +1;
        }
    }
}
  1. Give the Big-O performance of the following code fragment:
int count = 0;
for (int i = 0; i < n; i++){
    count = count + 1;
}
for (int j = 0; j < n; j++){
    count = count + 1;
}
for (int k = 0; k < n; k++){
    count = count + 1;
}
Next Section - 2.12. Programming Exercises