Question

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

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
1.You are to write a program name search.java that will do the following: 2.You are to...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a java program: Create a method fillRandom() that accepts an array of int as input...

    Write a java program: Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys) Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10...

  • (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates...

    (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates for an upcoming election. This program needs to allow the user to enter candidates and then record votes as they come in and then calculate results and determine the winner. This program will have three classes: Candidate, Results and ElectionApp Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are...

  • Program Overview This brief exercise is designed for you to consider how reference variables behave when...

    Program Overview This brief exercise is designed for you to consider how reference variables behave when you are passing them as arguments by-value. Instructions Name your class References.java. 1. Implement the following methods. 1.1 Method name: readStudentNames Purpose: This method accepts an array of strings as a parameter. It loops through the array and asks the user to input names. It populates the array of strings with the names. Parameters: an array of Strings, stringArray Return type: void In the...

  • Write a new program called TrickOr Treat that will do the following 1. In a while...

    Write a new program called TrickOr Treat that will do the following 1. In a while loop, say "Trick or Treat!" and allow the user to type in the name of a candy and store it in an ArrayList. Once the user types "Trick", then the loop should stop. Then, print out the total number of candies on the screen. (See example below) [1 points] 2. Write a method called countSnickers that will take an ArrayList of candy as an...

  • in JAVA please thanks. (need answer in a few hours !!) Exercise #1: Design and implement...

    in JAVA please thanks. (need answer in a few hours !!) Exercise #1: Design and implement a program (name it LinearBinarySearch) to implement and test the linear and binary search algorithm discussed in the lecture slides. Define method LinearSearch() to implement linear search of an array of integers. Modify the algorithm implementation to count number of comparisons it takes to find a target value (if exist) in the array. Define method BinarySearch() to implement binary search of an array of...

  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

  • Write a program that compares the execution speed of two different sorting algorithms: bubble sort and...

    Write a program that compares the execution speed of two different sorting algorithms: bubble sort and selection sort. It should do this via functions you must write with the following prototypes: void setRandomValues(int[], int[], int length); This function sets two integer arrays with identical random values. The first two arguments are two integer arrays of the same length, and the third argument is the length of those arrays. The function then sets each element in the first and second argument...

  • Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves...

    Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves searching and sorting an array of integers. Write a java application that initializes an array with the following numbers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Then display the unsorted values. This is required output #1 of 6 for this program. 2) Using a...

  • Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and...

    Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. TO Do 1. Fill an array with 140 real numbers between 0.00 and 945.00. Generate the numbers using the subroutine that generates random numbers. Make a spare copy of the array; you will need it later. 2. Call a subroutine to print the contents of the...

  • Write a C++ program that asks user number of students in a class and their names....

    Write a C++ program that asks user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’ names and ranking. Follow the Steps Below Save the project as A4_StudentRanking_yourname....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT