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...
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
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 ------...
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...
int bsearch(int[] arr, int key) { int lo = 0, mid, hi = arr.length-1; while (...
int bsearch(int[] arr, int key) { int lo = 0, mid, hi = arr.length-1; while ( lo <= hi ) { mid = (lo + hi) / 2; if ( key < arr[mid] ) hi = mid – 1; else if ( arr[mid] < key ) lo = mid + 1; else return mid; } return -1; } Count the number of operations (comparisons and assignments), find an original function with an input n, and give an analysis of the...
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...
public class Point { int x; int y; public Point(int initialX, int initialY){ x = initialX;...
public class Point { int x; int y; public Point(int initialX, int initialY){ x = initialX; y= initialY; } public boolean equals (Object o){ if (o instanceof Point){ Point other = (Point)o; return (x == other.x && y == other.y); }else{ return false; } } } We haev defined "equals" method for our class using "instanceof". We define and use instances (or objects) of this class in the following scenarios. In each case, specify what is the output. (hint: there...
Assume an array a [0 … n –1] of int, and assume the following trace: for...
Assume an array a [0 … n –1] of int, and assume the following trace: for (int i = 0; i < n - 1; i++) if (a [i] > a [i + 1]) System.out.println (i); What is worstTime(n)? Statement Worst Case Number of Executions i = 0 1 i < n – 1 n i++ n - 1 a[i] > a[i+1] n - 1 System.out.println() n - 1 That is, worstTime(n) is: 4n – 2. So this is part...