Question

In: Computer Science

Objective in JAVA (Please show output and have the avergae number of checks also.): Implement both...

Objective in JAVA (Please show output and have the avergae number of checks also.):

Implement both linear search and binary search, and see which one performs better given an array 1,000 randomly generated whole numbers (between 0-999), a number picked to search that array at random, and conducting these tests 20 times.  Each time the search is conducted the number of checks (IE number of times the loop is ran or the number of times the recursive method is called) needs to be counted and at the end the total number of checks should be averaged.

A few notes

Each algorithm (linear search and binary search) is ran 20 times

Each time a new sorted array of whole numbers is created and populated with random values from 0-999

A value to be searched in the said array is randomly selected from the range 0-999

Each algorithm must display if that number was successfully found

Each algorithm must display the number of checks it took to determine the above answer

It is advisable to create a method that returns the sorted array

Populate the array with random numbers

Search the array next

Return whether or not the value was found in the array

Implement both searches as a method

However instead of returning whether or not it found the number it should return the number of checks.

Whether the value is or is not found can be printed in the method

Binary search is fairly simple to create using recursion

Do not count the out of bounds or stopping index as a check

Example:

Welcome to the search tester.  We are going to see which algorithm performs the best out of 20 tests

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks: 753

Binary Checks: 8

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks: 834

Binary Checks: 10

Searching using linear search

Not Found

Searching using binary search

Not Found

Linear Checks: 1000

Binary Checks: 10

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks: 515

Binary Checks: 6

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks: 757

Binary Checks: 7

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks: 395

Binary Checks: 9

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks: 117

Binary Checks: 7

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks: 334

Binary Checks: 10

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks: 521

Binary Checks: 9

Searching using linear search

Not Found

Searching using binary search

Not Found

Linear Checks: 1000

Binary Checks: 10

Searching using linear search

Not Found

Searching using binary search

Not Found

Linear Checks: 1000

Binary Checks: 10

Searching using linear search

Not Found

Searching using binary search

Not Found

Linear Checks: 1000

Binary Checks: 10

Searching using linear search

Not Found

Searching using binary search

Not Found

Linear Checks: 1000

Binary Checks: 10

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks: 901

Binary Checks: 10

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks: 626

Binary Checks: 8

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks: 361

Binary Checks: 9

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks: 630

Binary Checks: 9

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks: 443

Binary Checks: 7

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks: 818

Binary Checks: 10

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks: 288

Binary Checks: 7

The average number of checks for 20 were:

Linear Search 664

Binary Search 8

Solutions

Expert Solution

import java.util.Arrays;
import java.util.Random;

public class Test{  
   public static int linearSearch(int arr[], int key){
       int iterations = 0;
       for(int i = 0; i < arr.length; i++){
           iterations++;
           if(arr[i] == key){
               System.out.println("Found!");
               return iterations;
           }
       }
       System.out.println("Not Found");
       return iterations;
   }  
   public static int binarySearch(int arr[], int key){
       int low = 0, high = arr.length - 1;
       int iterations = 0;
       while(high >= low){
           iterations++;
           int mid = (low + high) / 2;
           if(arr[mid] == key){
               System.out.println("Found!");
               return iterations;              
           }
           else if(arr[mid] > key){
               high = mid - 1;
           }
           else{
               low = mid + 1;
           }
       }
       System.out.println("Not Found");
       return iterations;
   }
   private static int[] generateRandomArray(){
       Random r = new Random();
       int arr[] = new int[1000];
       for(int i = 0; i < 1000; i++){
           arr[i] = r.nextInt(1000);
       }
       Arrays.sort(arr);
       return arr;
   }
   public static void main(String args[]){
       System.out.println("Welcome to the search tester. We are going to see which algorithm performs the best out of 20 tests");
       Random r = new Random();
       int avgLinearChecks = 0;
       int avgBinaryChecks = 0;
       for(int i = 1; i <= 20; i++){
           int arr[] = generateRandomArray();
           System.out.println("Searching using linear search");
           System.out.println("Searching using binary search");
           int keyToSearch = r.nextInt(1000);
           int linearSearchIterations = linearSearch(arr, keyToSearch);
           avgLinearChecks += linearSearchIterations;
           int binarySearchIterations = binarySearch(arr, keyToSearch);
           avgBinaryChecks += binarySearchIterations;
           System.out.println("Linear Checks: " + linearSearchIterations);
           System.out.println("Binary Checks: " + binarySearchIterations);
       }
       System.out.println("The average number of checks for 20 were:");
       avgLinearChecks /= 20;
       avgBinaryChecks /= 20;
       System.out.println("Linear Search " + avgLinearChecks);
       System.out.println("Binary Search " + avgBinaryChecks);
   }
}


Related Solutions

IN JAVA. I have the following code (please also implement the Tester to test the methods...
IN JAVA. I have the following code (please also implement the Tester to test the methods ) And I need to add a method called public int remove() that should remove the first integer of the array and return it at the dame time saving the first integer and bring down all other elements. After it should decrease the size of the array, and after return and save the integer. package ourVector; import java.util.Scanner; public class ourVector { private int[]...
Please show solution and comments for this data structure using java.​ Implement a program in Java...
Please show solution and comments for this data structure using java.​ Implement a program in Java to convert an infix expression that includes (, ), +, -, *,     and / to postfix expression. For simplicity, your program will read from standard input (until the user enters the symbol “=”) an infix expression of single lower case and the operators +, -, /, *, and ( ), and output a postfix expression.
Write a recursive a Java code that checks if a number is Palindrome. A palindrome number...
Write a recursive a Java code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you...
This question is about java program. Please show the output and the detail code and comment...
This question is about java program. Please show the output and the detail code and comment of the each question and each Class and interface. And the output (the test mthod )must be all the true! Thank you! Question1 Create a class Animal with the following UML specification: +-----------------------+ | Animal | +-----------------------+ | - name: String | +-----------------------+ | + Animal(String name) | | + getName(): String | | + getLegs(): int | | + canFly(): boolean | |...
Part 1 (Objective C++ and please have output screenshot) The purpose of this part of the...
Part 1 (Objective C++ and please have output screenshot) The purpose of this part of the assignment is to give you practice in creating a class. You will develop a program that creates a class for a book. The main program will simply test this class. The class will have the following data members: A string for the name of the author A string for the book title A long integer for the ISBN The class will have the following...
Please use Java Eclipse and show code/output Please create a program that determines when a good...
Please use Java Eclipse and show code/output Please create a program that determines when a good day to go to the beach is. Please use the users input and its returning output. If the weather is 70 degree or greater, the program should say yes it is a good day to go If the weather is less than 70 degrees to say no the weather is not a good day to go
If I want to show an exception in JAVA GUI that checks if the date format...
If I want to show an exception in JAVA GUI that checks if the date format is correct or the date doesn't exist, and if no input is put in how do I do that. For example if the date format should be 01-11-2020, and user inputs 11/01/2020 inside the jTextField it will show an error exception message that format is wrong, and if the user inputs an invalid date, it will print that the date is invlaid, and if...
If I want to show an exception in JAVA GUI that checks if the date format...
If I want to show an exception in JAVA GUI that checks if the date format is correct or the date doesn't exist, and if no input is put in how do I do that. For example if the date format should be 01-11-2020, and user inputs 11/01/2020 inside the jTextField it will show an error exception message that format is wrong, and if the user inputs an invalid date, it will print that the date is invlaid, and if...
Please explain step by step what is going on each step Also show how the output...
Please explain step by step what is going on each step Also show how the output is coming. I would rate positively. Thank you so much . #include     <stdio.h>   #include   <string.h>    int   main()   {          char   buffer[16],   *pos;          printf("Enter   string:   ");          if   (!(fgets(buffer,sizeof(buffer),stdin)))   {                      printf("End   of   file\n");                      return   0;          }          printf("Length   of   %s   is   %lu   \n",buffer,strlen(buffer));          if   ((pos=strchr(buffer,  ...
Please in C++ thank you! Please also include the screenshot of the output. I have included...
Please in C++ thank you! Please also include the screenshot of the output. I have included everything that's needed for this. Pls and thank you! Write a simple class and use it in a vector. Begin by writing a Student class. The public section has the following methods: Student::Student() Constructor. Initializes all data elements: name to empty string(s), numeric variables to 0. bool Student::ReadData(istream& in) Data input. The istream should already be open. Reads the following data, in this order:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT