Homework 02: Big-O of Loops
Name: _____________________________________________ Alpha: ___________________
Describe help received: ________________________________________________________
- Due before class on Friday, September 2
- This is a written homework - be sure to turn in a hard-copy of your completed assignment at the beginning of class on the deadline.
For each of the program-snippets below, what is the big-O of the operation? Show all of your work and clearly indicate your final answer.
int sum = 0; for (int i = 0; i < n; i++) { sum++; }
int sum = 0; for (int i = 0; i < n*n; i++) { sum++; }
int sum = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { sum++; } }
int sum = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { sum++; } }
int sum = 0; for (int i = -5; i < n; i++) { for (int j = -5; j < n; j++) { sum++; } }
int sum = 0; for (int i = 0; i < n; i++) { for (int j = 1; j < n; j*=2) { sum++; } }
int sum = 0; for (int i = 1; i < n; i*=2) { for (int j = 1; j < n; j++) { sum++; } }
int sum = 0; for (int i = 1; i < n; i++) { for (int j = 0; j < n*i; j++) { sum++; } }
int sum = 0; for (int i = 1; i < n; i++) { for (int j = 0; j < i/2; j++) { sum++; } }
int sum = 0; for (int i = 1; i < n; i*=2) { for (int j = 1; j < n*n; j++) { for (int k = 0; k < j; k++) { sum++; } } }