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
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...
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...
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...
Restricted structures such as stack and queue are fast, but they do not support access in...
Restricted structures such as stack and queue are fast, but they do not support access in the key field mode. Group of answer choices True False Big O analysis evaluates an algorithm based on its _________ performance. Group of answer choices A. average-case B. best-case C. worst-case Which of the following algorithms is the fastest in speed? Group of answer choices A. Polynomial time algorithm B. Linear time algorithm C. Exponential time algorithm The following code gives an implementation of...
Design a recursive divide-and-conquer algorithm A(n) that takes an integer input n ≥ 0, and returns...
Design a recursive divide-and-conquer algorithm A(n) that takes an integer input n ≥ 0, and returns the total number of 1’s in n’s binary representation. Note that the input is n, not its binary representation. For example, A(9) should return 2 as 9’s binary representation is 1001, while A(7) should return 3 since 7 is 111 in binary. Note that your algorithm should have a running time of O(log n). Justify your answer. You need to do the following: (1)...
public final class SimpleRegister implements ICashRegister { //(value of coin, number of coins) private Map<Integer, Integer>...
public final class SimpleRegister implements ICashRegister { //(value of coin, number of coins) private Map<Integer, Integer> moneyBox; //store the log of transactions for auditing StringBuilder log; /** * Constructs an empty register */ public SimpleRegister() { moneyBox = new TreeMap<Integer, Integer>(); moneyBox.put(1, 0); moneyBox.put(5, 0); moneyBox.put(10, 0); moneyBox.put(25, 0); moneyBox.put(100, 0); moneyBox.put(500, 0); moneyBox.put(1000, 0); log = new StringBuilder(); } @Override public void addPennies(int num) { moneyBox.put(1, moneyBox.get(1) + num); String auditMessage = String.format("Deposit: $%.02f\n", num * 1 / 100.0f);...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT