1 answer

1.You are to write a program name search.java that will do the following: 2.You are to...

Question:

1.You are to write a program name search.java that will do the following:

2.You are to create 3 arrays - prompt the user for a number that is greater than 100 that will serve as the size for the arrays (all 3 arrays will have the same user input size). Call them A, B & C.

3.Generate this amount of random numbers to fill these arrays – the random numbers must range from 1 to 99.

4.Write 1 sequential search voided function (method) name Seq_search() that accepts two parameters – 1 array and a target value to search for. Call this method 3 times (pass Array A as one of the paramters) à once with a target value of 1, next with a target value of 50 and next with a target value of 100. Each time you call this method, you must invoke a clock to see how long it takes to execute and print out the time with an appropriate message.

5.Write 1 binary search voided function (method) using Loop only, name Bin_search() that accepts two parameters – 1 array and a target value to search for. In the main program first sort array B and time it to see how long it took to sort. Now pass this sorted array as a parameter to this method calling it 3 times à once with a target value of 1, next with a target value of 50 and next with a target value of 100. Each time you call this method, you must invoke a clock to see how long it takes to execute and add the time it took to sort the array to this time and print out the time with an appropriate message.

6.Write 1 binary search voided function (method) that uses recursion only, name BinRe_search() that accepts two parameters – 1 array and a target value to search for. In the main program first sort array C and time it to see how long it took to sort. Now pass this sorted array as a parameter to this method calling it 3 times à once with a target value of 1, next with a target value of 50 and next with a target value of 100. Each time you call this method, you must invoke a clock to see how long it takes to execute and add the time it took to sort the array to this time and print out the time with an appropriate message.

7.After you get the time, give an analysis as to which you think is a better algorithm to use and under what circumstance it is better to be used.


Answers

Complete Program:

// File: search.java
import java.util.Random;
import java.util.Scanner;
public class search
{
public static int Seq_search(int[] arr, int target)
{
  for(int i = 0; i < arr.length; i++)
  {
   if(arr[i] == target)
    return i;
  }
  
  return -1;
}
  
public static void Sel_sort(int[] arr)
{
  for(int i = 0; i < arr.length - 1; i++)
  {
   int minPos = i;
   for(int j = i + 1; j < arr.length; j++)
   {
    if(arr[minPos] > arr[j])
    {
     minPos = j;
    }
   }

   if(i != minPos)
   {
    int temp = arr[i];
    arr[i] = arr[minPos];
    arr[minPos] = temp;
   }
  }
}

public static int Bin_search(int[] arr, int target)
{
  int first = 0;
  int last = arr.length - 1;

  while(first <= last)
  {
   int middle = (first + last) / 2;

   if(arr[middle] == target)
    return middle;
   
   if(arr[middle] < target)
    first = middle + 1;    
   else
    last = middle - 1;
  }

  return -1;
}

public static int BinRe_search(int[] arr, int target)
{
  return BinRe_search(arr, 0, arr.length - 1, target);
}
  
private static int BinRe_search(int[] arr, int first, int last, int target)
{
  if(first > last)
   return -1;
  
  int middle = (first + last) / 2;
  
  if(arr[middle] == target)
   return middle;
  
  if(arr[middle] < target)
   return BinRe_search(arr, middle + 1, last, target);
  else
   return BinRe_search(arr, first, middle - 1, target);
}

public static void main(String[] args)
{
  Scanner input = new Scanner(System.in);
  Random rand = new Random();
  
  System.out.print("Enter the size of the arrays: ");
  int size = input.nextInt();
  
  while(size <= 100)
  {
   System.out.print("Enter the size of the arrays > 100: ");
   size = input.nextInt();
  }
  
  int A[] = new int[size];
  int B[] = new int[size];
  int C[] = new int[size];
  
  for(int i = 0; i < size; i++)
  {
   int number = 1 + rand.nextInt(99);
   
   A[i] = number;
   B[i] = number;
   C[i] = number;
  }
  
  long before, after, elapsed;
  int idx1, idx50, idx100;
    
  // test for sequential search
  before = System.nanoTime();
  idx1 = Seq_search(A, 1);
  after = System.nanoTime();
  elapsed = after - before;
  
  if(idx1 > -1)
   System.out.println("\n1 is found at the index " + idx1 + " in the array. Sequential Search has done in " + elapsed + " nanoseconds.");
  else
   System.out.println("\n1 is not found in the array. Sequential Search has done in " + elapsed + " nanoseconds.");
    
  before = System.nanoTime();
  idx50 = Seq_search(A, 50);
  after = System.nanoTime();
  elapsed = after - before;
  
  if(idx50 > -1)
   System.out.println("50 is found at the index " + idx50 + " in the array. Sequential Search has done in " + elapsed + " nanoseconds.");
  else
   System.out.println("50 is not found in the array. Sequential Search has done in " + elapsed + " nanoseconds.");
  
  before = System.nanoTime();
  idx100 = Seq_search(A, 100);
  after = System.nanoTime();
  elapsed = after - before;
  
  if(idx100 > -1)
   System.out.println("100 is found at the index " + idx100 + " in the array.

Sequential Search has done in " + elapsed + " nanoseconds.");
  else
   System.out.println("100 is not found in the array. Sequential Search has done in " + elapsed + " nanoseconds.");
    
  // test for binary search
  before = System.nanoTime();
  Sel_sort(B);
  after = System.nanoTime();
  elapsed = after - before;
  
  System.out.println("\nSorting has done in " + elapsed + " nanoseconds.");
  
  before = System.nanoTime();
  idx1 = Bin_search(B, 1);
  after = System.nanoTime();
  elapsed = after - before;
  
  if(idx1 > -1)
   System.out.println("1 is found at the index " + idx1 + " in the array. Binary Search has done in " + elapsed + " nanoseconds.");
  else
   System.out.println("1 is not found in the array. Binary Search has done in " + elapsed + " nanoseconds.");
    
  before = System.nanoTime();
  idx50 = Bin_search(B, 50);
  after = System.nanoTime();
  elapsed = after - before;
  
  if(idx50 > -1)
   System.out.println("50 is found at the index " + idx50 + " in the array. Binary Search has done in " + elapsed + " nanoseconds.");
  else
   System.out.println("50 is not found in the array.

Binary Search has done in " + elapsed + " nanoseconds.");
  
  before = System.nanoTime();
  idx100 = Bin_search(B, 100);
  after = System.nanoTime();
  elapsed = after - before;
  
  if(idx100 > -1)
   System.out.println("100 is found at the index " + idx100 + " in the array. Binary Search has done in " + elapsed + " nanoseconds.");
  else
   System.out.println("100 is not found in the array. Binary Search has done in " + elapsed + " nanoseconds.");
    
  // test for recursive binary search
  before = System.nanoTime();
  Sel_sort(C);
  after = System.nanoTime();
  elapsed = after - before;
  
  System.out.println("\nSorting has done in " + elapsed + " nanoseconds.");
  
  before = System.nanoTime();
  idx1 = BinRe_search(C, 1);
  after = System.nanoTime();
  elapsed = after - before;
  
  if(idx1 > -1)
   System.out.println("1 is found at the index " + idx1 + " in the array. Recursive Binary Search has done in " + elapsed + " nanoseconds.");
  else
   System.out.println("1 is not found in the array. Recursive Binary Search has done in " + elapsed + " nanoseconds.");
    
  before = System.nanoTime();
  idx50 = BinRe_search(C, 50);
  after = System.nanoTime();
  elapsed = after - before;
  
  if(idx50 > -1)
   System.out.println("50 is found at the index " + idx50 + " in the array.

Recursive Binary Search has done in " + elapsed + " nanoseconds.");
  else
   System.out.println("50 is not found in the array. Recursive Binary Search has done in " + elapsed + " nanoseconds.");
  
  before = System.nanoTime();
  idx100 = BinRe_search(C, 100);
  after = System.nanoTime();
  elapsed = after - before;
  
  if(idx100 > -1)
   System.out.println("100 is found at the index " + idx100 + " in the array. Recursive Binary Search has done in " + elapsed + " nanoseconds.");
  else
   System.out.println("100 is not found in the array. Recursive Binary Search has done in " + elapsed + " nanoseconds.");
}
}


Sample Output:

5gMOYkbjjvQAAAABJRU5ErkJggg==

From the Sample Output, Bin_search() method takes the least amount of time to search an elelemt in an array. So Bin_search() method is better than Seq_search() and BinRe_search() methods.

.

Similar Solved Questions

1 answer
Compute the limit by using known power series 2. lim 30 · (arctan (2) – sin...
Compute the limit by using known power series 2. lim 30 · (arctan (2) – sin (2)) 1 - cos (22)...
1 answer
Homework o Required information Exercise 3-10A Recording supplies and identifying their effect on financial statements LO...
Homework o Required information Exercise 3-10A Recording supplies and identifying their effect on financial statements LO 3-1, 3-3, 3-4 The following information applies to the questions displayed below! Sye Chase started and operated a small family architectural firm in Year 1. The firm was affecte...
1 answer
IP Point charges 4.4 μC and -2.4 μC are placed on the x axis at (15...
IP Point charges 4.4 μC and -2.4 μC are placed on the x axis at (15 m , 0) and (-15 m , 0), respectively. Find the point to the left of the negative charge where the electric potential vanishes....
1 answer
1. Suppose that an ion source in a mass spectrometer produces doubly ionized gold ions -25...
1. Suppose that an ion source in a mass spectrometer produces doubly ionized gold ions -25 potential difference of 1.30 kV. Then, a 0.440 T magnetic field causes the ions to follow a circular path. Determine the radius of the path....
1 answer
Olyanlle Chemistry, 3e Assignment Gradebook ORION Downloadable eTextbook ent Question 9 3.46a1 x Incorrect. Please...
Olyanlle Chemistry, 3e Assignment Gradebook ORION Downloadable eTextbook ent Question 9 3.46a1 x Incorrect. Please analyze the reaction process. For the reaction given below, draw a mechanism (curved arrows) and then predict which side of the reaction is favored under equilibrium conditions LINK TO ...
1 answer
7. You have one observation Y , which has one of the discrete pdf’s y f0(y)...
7. You have one observation Y , which has one of the discrete pdf’s y f0(y) f1(y) 0 0.1 0.3 1 0.1 0.1 2 0.1 0.1 3 0.1 0.2 4 0.2 0.1 5 0.1 0.1 6 0.3 0.1 You want to test H0 : f0 is true Ha : f1 is true (a) Here is a test: reject H0 if Y = 0, 1, 2, 3, or 5. What are the probabilities of the two ...
1 answer
Teslalik, QueSLIUII U22 x Your answer is incorrect. Try again. What is the IUPAC name of...
Teslalik, QueSLIUII U22 x Your answer is incorrect. Try again. What is the IUPAC name of the following compound?...
1 answer
Rampart Corporation has a dividend yield of 1.6 %. Its equity cost of capital is 7.5...
Rampart Corporation has a dividend yield of 1.6 %. Its equity cost of capital is 7.5 %​, and its dividends are expected to grow at a constant rate. a. What is the expected growth rate of​ Rampart's dividends? b. What is the expected growth rate of​ Rampart's share​ pr...
1 answer
Can please answer 250 or more words 300 .i appreciate your help ! Type it please...
Can please answer 250 or more words 300 .i appreciate your help ! Type it please A http://www.stratford.edu/ Stratford University Eleni Gebe Student LibraryooksResources Email Facu ty Dashboard > My courses > 2019,01 TERM-C-H IM270-SDFC-Pati > January 14-January 20 > Week 2: DB Thread...
1 answer
Mary's Mugs produces and sells various types of ceramic mugs. The business began operations on January...
Mary's Mugs produces and sells various types of ceramic mugs. The business began operations on January 1, year 1, and its costs incurred during the year include these Variable costs (based on mugs produced): Direct materials cost Direct manufacturing labor costs Indirect manufacturing costs Admi...
1 answer
Please answer question 16 and 18 ASAP even B. Exercises Use Table 7.1 to find the...
please answer question 16 and 18 ASAP even B. Exercises Use Table 7.1 to find the amount that should be budgeted for automobile costs weekly (nearest dollar). Use this amount to find the monthly and annual costs (nearest dollar). 12. mi./week: 180, compact 14. mi./week: 420, pickup 13. mi./week:...
1 answer
YouTube Maps Lati Introduction to Pharmacology Test CLOSE LLLLL Question: 6 of 25 Time Remaining: 16:22:53...
YouTube Maps Lati Introduction to Pharmacology Test CLOSE LLLLL Question: 6 of 25 Time Remaining: 16:22:53 B FLAG A nurse is teaching a client who has a prescription for a drug that has a receptor agonist effect. Which of the following information should the nurse include in the teaching? O "Thi...
1 answer
3. The following information is about type of tooth fillings and the presence of an adverse...
3. The following information is about type of tooth fillings and the presence of an adverse health condition. Mercury Present in Fillings (Source: JAMA) Yes No Adverse Health Yes 135 145 Condition Present No 132 122 Is there evidence at a = .05 to show there is a relationship between whether or not ...
1 answer
What does the word helminths mean
what does the word helminths mean...
1 answer
Let T : R3 → R2 be a linear map. Recall that the image of T,...
Let T : R3 → R2 be a linear map. Recall that the image of T, Im(T), is the set {T(i) : R*) (a) Suppose that T(v)- Av. Describe the image of T in terms of A Using this description, explain why Im(T) is a subspace of R2. (b) What are the possible dimensions of Im(T)? (c) Pick one of the possible ...
3 answers
Calculate the free energy DeltaG at 25 degrees C for the nonstandard conditions at point B where the reaction quotient...
Calculate the free energy DeltaG at 25 degrees C for the nonstandard conditions at point B where the reaction quotient Q is 2.75 *10^-5. In solving Part A, we found that \Delta G=-40.82kJ/mol...
1 answer
Overland's preferred stock was issued 3 years ago to yield 10% of its par value of...
Overland's preferred stock was issued 3 years ago to yield 10% of its par value of $30. The stock is selling in the market today for $50. Assuming that Overland pays 15% in flotation costs on new security issues, calculate the cost of preferred stock financing. a. 9.2% b. 6.3% c. ...