Question

public int linearSearch2(ArrayList<Integer> pList, Integer pKey) { for (int i = 0; i <= pList.size() /...

 public int linearSearch2(ArrayList<Integer> pList, Integer pKey) {
      for (int i = 0; i <= pList.size() / 2; ++i) {
          if (pList.get(i).equals(pKey)) {
              return i;
          } else if (pList.get(pList.size() - 1 - i).equals(pKey)) {
              return pList.size() - 1 - i;
          }
      }
      return -1;
  }
-----
Q1)Which function f(n) counts how many times the key operation(s) is(are) performed as a function of the list size n when pKey is not in pList (the worst case behavior of linearSearch2() occurs when pKey is not found)? Note that when n is a number, ⌊n⌋ is called the floor of n and is defined to be the largest integer that is less than or equal to n. For example, ⌊8⌋ = 8, ⌊8.2⌋ = 8, ⌊8.9⌋ = 8, ⌊8/2⌋ = 4, ⌊7/2⌋ = 3. 

a.) f(n) = ⌊n / 2⌋2
b.) f(n) = 2n
c.) f(n) = n ⋅ log(2 ⋅⌊n / 2⌋)
d.) f(n) = n
e.) f(n) = n / 2
f.) f(n) = 2 ⋅ ⌊n / 2⌋ + 2

-----
Q2) Continuing the questions about linearSearch2(). What is the worst case time complexity of the algorithm?

a.) O(n)
b.) O(n lg n)
c.) O(1)
d.) O(n2)
e.) O(2n + 2)

Homework Answers

Answer #1

Solution:

 public int linearSearch2(ArrayList<Integer> pList, Integer pKey) {
      for (int i = 0; i <= pList.size() / 2; ++i) {
          if (pList.get(i).equals(pKey)) {
              return i;
          } else if (pList.get(pList.size() - 1 - i).equals(pKey)) {
              return pList.size() - 1 - i;
          }
      }
      return -1;
  }

See, in the worst-case pKey is not in the pList, this means that every time if and else both the statements will be executed.

The loop is running n/2 number of times and each time 2 key operations are executed, this means that n/2*2= n

d.) f(n) = n is the correct answer.

2)

O(n) is the worst-case time complexity here.

Hit the thumbs up if you liked the answer. :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Consider the following function: 01: void cpUniq (const int* source, int N, vector<int>& dest) 02: {...
Consider the following function: 01: void cpUniq (const int* source, int N, vector<int>& dest) 02: { 03: list<int> L; 04: for (int i = 0; i < N; ++i) 05: { 06: copy (source, source + i, front_inserter<int>(L)); 07: } 08: for (int j: L) 09: { 10: if (find(dest.begin(), dest.end(), j) != dest.end()) 11: dest.push_back(j); 12: } 13: } and the following list of complexity values: A: O(1), B: O(log N), C: O(N), D: O(N log N), E: O(N2),...
1. public int add100(int[] array) { if (array.length < 100) { return 0; } int sum...
1. public int add100(int[] array) { if (array.length < 100) { return 0; } int sum = 0; for (int i = 0; i < 100; i++) { sum += array[i]; } return sum; } 2. int sum = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= i; j++) { sum++; sum++; } } for (int i = 0; i < n; i += 2) { sum++; } 3. nt...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
Define a recurrence F(n) for the code: int a(int i, int j){ if( j <= 1)...
Define a recurrence F(n) for the code: int a(int i, int j){ if( j <= 1) return i+20; else return j + a(i+10, j-3); } This recurrence I've come up with is F(n) = 2 if j <= 1 and F(n) = 2 + F(i+10, j-3). Now I need to solve this recurrence and represent it in big O notation which I'm unsure how to do. The main thing throwing me off is that there are two variables in the...
Python question: Write a function that takes a number and an integer (X,N) in a time...
Python question: Write a function that takes a number and an integer (X,N) in a time complexity of O(log(N)). for an example: calling power(2, 2) would return 4
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i];...
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i]; } return (double)sum/n; } Rewrite the function to eliminate the array subscripting (a[i]), using pointer arithmatic instead. Write a program that reads a line of input and checks if it is a palindrome (ignoring spaces) Write a program that takes the name of a file as a command line argument, and prints the contents of the file (similar to the linux 'cat' program). Write...
(Java) For the following code segment, match the different values of the empty line with the...
(Java) For the following code segment, match the different values of the empty line with the resulting Big O complexity of the algorithm. int n = int k = 0; for (int i=0; i <= n; i++){ int j = i; while (j > 0) { // ___MISSING CODE___ k++; } } j = 0; j--; j /= 2; j = j * 2; Options for each are: O(n^3)            O(n * log n)            an...
Determine the order of complexity for the following algorithm: function(int n) { int l, u, m;...
Determine the order of complexity for the following algorithm: function(int n) { int l, u, m;    l=0; u= n-1;    while (l<u) {        m= (l+u)/2;        if (a[m] < x) l= m+1;        else if (a[m] >x) u=m-1;            else return “found”    }    return (“not found”); }
1. explain what the worst case for the algorithm is 2.explain how you computed the time...
1. explain what the worst case for the algorithm is 2.explain how you computed the time complexity 3.give the order of the time complexity of the algorithm Code: public class Degree { int degree; public int maxDegree(Node node) { degree = 0; if(node == null) return degree; findMaxDegree(node); return degree; } private void findMaxDegree(Node node) { if(node == null || node.numChildren() == 0) return; degree = Math.max(degree, node.numChildren()); Node[] children = node.getChildren(); for(int i=0; i<children.length; i++) { findMaxDegree(children[i]); } }...
Using Big O notation, indicate the time requirement of each of the following tasks in the...
Using Big O notation, indicate the time requirement of each of the following tasks in the worst case. Computing the sum of the first n even integers by using a for loop            [ Choose ] O(1) O(2n) O(n*log n ) O(2^n) O(log n) O(n^2) O(n) O(2) O(n^3)          Displaying all n integers in an array            [ Choose ] O(1) O(2n) O(n*log n ) O(2^n) O(log n) O(n^2) O(n) O(2) O(n^3)          Displaying all n integers in a sorted linked chain            [ Choose...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT