Question

In: Computer Science

Questions 1a and 1b are about the linear search algorithm that was discussed in the lectures....

Questions 1a and 1b are about the linear search algorithm that was discussed in the lectures.

1a. Write a Java method called stringyLinear that performs linear search on an array of String’s, so it is defined as follows. Your code appears in place of the three dots ‘‘⋅⋅⋅’’. Do not write a class, only a method.


If a String equal to key appears in keys, then stringyLinear must return an index in keys where it appears. If no String equal to key appears in keys, then stringyLinear must return −1. Assume that no element of keys is null, and that key is not null.

1b. Suppose that a linear search method is given an array whose length is n, where n ≥ 1. Show that the algorithm performs O(n) key comparisons in the average case. For full credit, you must do this by finding constants c and n₀.

Solutions

Expert Solution

Ia.
    public int StringyLinear()throws IOException
{
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

   System.out.println("Enter the size of the array: ");
   int n= Integer.parseInt(br.readLine());

   String A[]= new int [n];

   System.out.println("Enter the elements of the array: ");
   for(int i=0; i<n; i++)
   {
       System.out.println("Enter the "+i+"th element of the array: ");
       A[i]= br.readline();
   }
   int pos=-1;
   System.out.println("Enter the key to be searched: ");
   String key= br.readLine();
   boolean found = false;

   for(int i=0; i<n; i++)
   {
       if(key.equalsIgnoreCase(A[i])
       {   found=true;
           pos=i; break; }
   }      

           if(found)
                  return pos;
            else
              return -1;
   }

1b. In average case analysis, we take all possible inputs that may or may not be uniformly distributed and calculate the time taken.Sum all the calculated values and divide the sum by total number of inputs.

f(n)<=c.g(n), n>=n0,

c=1, n>=0, n0=0.


Related Solutions

Question - 1 Using the structural node and methods discussed in Binary Search Tree lectures, create...
Question - 1 Using the structural node and methods discussed in Binary Search Tree lectures, create a method for the Binary Search Tree that takes an unsorted input list and constructs a Binary Search Tree based on its values. Any duplicate value will only appear once on the tree. This method outputs a Binary Search Tree structure (not an enumeration of the tree). Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly...
Consider the following linear program Max 3A + 2B St 1A + 1B <= 10 3A...
Consider the following linear program Max 3A + 2B St 1A + 1B <= 10 3A + 1B <= 24 1A + 2B <= 16 A, B >= 0 The value of the optimal solution is 27. Suppose that the right hand side for constraint one is increased from 10 to 11. a) Use the graphical solution procedure to find the new optimal solution. b) Use the solution to part A to determine the shadow price for constraint 1 C)...
Ch 2: Linear Programming Max 6A + 4B ST 2A + 1B <= 12 1A +...
Ch 2: Linear Programming Max 6A + 4B ST 2A + 1B <= 12 1A + 1B <= 10 1A <= 4 A, B >= 0 Solve the above model using Excel Solver. Write down the optimal solution value and values for each decision variable (i.e. variable ‘A’ and ‘B’). Write down the slack for each constraint Which constraints are binding constraints?
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) { while (f <= l) { int m = f + (l - l) / 2; // Check if x is present at mid if (stgrade[m] == t) return m; // If x greater, ignore...
1f. Compare 1a and 1d, and 1b and 1e, explain why the percentage in 1a is...
1f. Compare 1a and 1d, and 1b and 1e, explain why the percentage in 1a is much larger than that in 1d and why the value in 1b is much smaller than that in 1e? 1. Suppose that for Edwardsville High School, distances between students’ homes and the high school observe normal distribution with the average distance being 4.76 miles and the standard deviation being 1.74 miles. Express distances and z scores to two decimal places. Write the formula to...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in chapter 19 (NOT Java's pre-built binarySearch() method from imported Java library) to search for an integer in a random array of size 30 in the range of 0 to 1000 inclusive. You should use Java's random number generator to randomize the numbers in your array. b.) The application's main() method should display unsorted array and sorted array, prompt user for a search key, allow...
Discrete Math Problems: From the below algorithm (linear search) construct the function f(n) which computes the...
Discrete Math Problems: From the below algorithm (linear search) construct the function f(n) which computes the number of steps the algorithm executes for a list of n integers and compute O(f) using the definition                     ALGORITHM 2 The Linear Search Algorithm.                 procedure linear search(x: integer, a1, a2,…, an: distinct integers)                 i := 1                 while (i ≤ n and x ≠ ai)                i := i + 1                if i ≤ n then location := i               ...
Please answer 1a and 1b. 1a. What is dark matter? How was its existence discovered, and...
Please answer 1a and 1b. 1a. What is dark matter? How was its existence discovered, and why do astronomers think it must be present? 1b. What are quasars and how are they used to study the evolution of the Universe?
44. Consider the following linear program Max 1a+1b s.t. 5a+3b<15 3a+5b,<15 a,b >0 A. What is...
44. Consider the following linear program Max 1a+1b s.t. 5a+3b<15 3a+5b,<15 a,b >0 A. What is the optimal solution for this problem? B. Suppose that the objective function is changed to 1a+2b. Find the new optimal solution. I am using excel for this homework question so I need the formulas to help not just the answers and I also trying to figure desmos to graph the question.
Create a List object that uses the binary search algorithm to search for the string "A"....
Create a List object that uses the binary search algorithm to search for the string "A". Display a message box indicating whether the value was found. Language: C#
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT