Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 l) {

// assume that 'l' contains 'n' elements

while(l.size() > 0) {

l.removeTail();

}

}

8. public static int example8(DoublyLinkedList l) {

// 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

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial and Managerial Accounting the basis for business decisions

Authors: Jan Williams, Susan Haka, Mark Bettner, Joseph Carcello

16th edition

0077664078, 978-0077664077, 78111048, 978-0078111044

Students also viewed these Databases questions

Question

305 mg of C6H12O6 in 55.2 mL of solution whats the molarity

Answered: 1 week ago