Question
Estimating Number of Operations -- Estimate and justify the number of operations in terms of n (the array length) of the following code snippets. 1.
Estimating Number of Operations -- Estimate and justify the number of operations in terms of n (the array length) of the following code snippets.
1. public static int example1(int[] arr) {
int n = arr.length;
int total = 0;
for(int j = 0; j < n; j++) { total += arr[j]; }
return total; }
2. public static int example2(int[] arr) {
int n = arr.length;
int total = 0;
for(int j = 0; j < n; j += 2) {
total += arr[j];
}
return total;
}
3. public static int example3(int[] arr) {
int n = arr.length;
int total = 0;
for(int j = 0; j < n; j++) {
for(int k = 0; k <= j; k++) {
total += arr[j];
}
}
return total;
}
4. public static int example4(int[] arr) {
int n = arr.length;
int prefix = 0;
int total = 0;
for(int j = 0; j < n; j++) {
prefix += arr[j];
total += prefix;
}
return total;
}
5. public static int example5(int[] first, int[] second) {
int n = first.length;
int count = 0;
for(int i = 0; i < n; i++) {
int total = 0;
for(int j = 0; j < n; j++){
for(int k = 0; k <= j; k++){
total += first[k];
}
}
if(second[i] == total){
count++;
}
}
return count;
}
6. public static int example6(int n) {
int ret = 0;
for(int i = 0; i < (int)(Math.sqrt(n)); ++i) {
ret += i;
}
return ret;
}
7. public static int example7(SinglyLinkedList
// assume that 'l' contains 'n' elements
while(l.size() > 0) {
l.removeTail();
}
}
8. public static int example8(DoublyLinkedList
// assume that 'l' contains 'n' elements
while(l.size() > 0){
l.removeTail();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started