Question

In: Computer Science

I need original java code that completes this program and gets it to print out results...

I need original java code that completes this program and gets it to print out results just like in the example. Also please upload answer in a word document format only.

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

RandomNumbers.java :

import java.util.Random;

import java.util.Scanner;

public class RandomNumbers {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

Random r = new Random();

int arr[] = new int[1000];

for(int i=0;i<1000;i++)

arr[i] = r.nextInt()%1000 + 1;

int linearcount[] = new int[20];

int binarycount[] = new int[20];

System.out.println("Welcome to the search tester. We are going to see which algorithm performs the best out of 20 tests");

for(int i=0;i<20;i++){

System.out.print("\nEnter key element:");

int key = sc.nextInt();

System.out.println("Searching using linear search");

int lcount = linearSearch(arr,key);

System.out.println("Searching using binary search");

int bcount = binarySearch(arr,key);

System.out.println("Linear Checks:"+lcount);

System.out.println("Binary Checks:"+bcount);

linearcount[i] = lcount;

binarycount[i] = bcount;

}

double linear_sum = 0,binary_sum =0,linear_avg = 0,binary_avg = 0;

System.out.println("\nThe average number of checks for 20 were");

for(int i=0;i<20;i++){

linear_sum += linearcount[i];

binary_sum += binarycount[i];

}

linear_avg = linear_sum /20;

binary_avg = binary_sum /20;

System.out.println("Linear Search: "+linear_avg);

System.out.println("Binary Search: "+binary_avg);

}

public static int linearSearch(int a[],int key){

int compCount = 0;

for(int i=0;i<1000;i++){

compCount++;

if(a[i] == key){

System.out.println("Found!");

return compCount;

}

}

System.out.println("Not Found");

return compCount;

}

public static int binarySearch(int a[],int key){

int compCount = 0;

int i,temp;

for(i=0;i<1000;i++){

for(int j=i+1;j<1000;j++){

if(a[i]>a[j]){

temp = a[i];

a[i] = a[j];

a[j] = temp;

}

}

}

int mid,low = 0,high = 1000-1;

while(low<=high){

mid = (low + high)/2;

compCount++;

if(a[mid] == key){

System.out.println("Found!");

return compCount;

}

else if(a[mid] > key){

high = mid - 1;

}

else{

low = mid + 1;

}

}

System.out.println("Not Found");

return compCount;

}

}

Sample Input & Output :

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

Enter key element:839

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks:3

Binary Checks:9

Enter key element:45

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks:516

Binary Checks:10

Enter key element:34

Searching using linear search

Not Found

Searching using binary search

Not Found

Linear Checks:1000

Binary Checks:10

Enter key element:57

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks:521

Binary Checks:9

Enter key element:29

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks:508

Binary Checks:9

Enter key element:71

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks:526

Binary Checks:10

Enter key element:39

Searching using linear search

Not Found

Searching using binary search

Not Found

Linear Checks:1000

Binary Checks:10

Enter key element:42

Searching using linear search

Not Found

Searching using binary search

Not Found

Linear Checks:1000

Binary Checks:10

Enter key element:896

Searching using linear search

Not Found

Searching using binary search

Not Found

Linear Checks:1000

Binary Checks:10

Enter key element:734

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks:868

Binary Checks:9

Enter key element:234

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks:606

Binary Checks:10

Enter key element:436

Searching using linear search

Not Found

Searching using binary search

Not Found

Linear Checks:1000

Binary Checks:10

Enter key element:509

Searching using linear search

Not Found

Searching using binary search

Not Found

Linear Checks:1000

Binary Checks:10

Enter key element:987

Searching using linear search

Found!

Searching using binary search

Found!

Linear Checks:994

Binary Checks:10

Enter key element:765

Searching using linear search

Not Found

Searching using binary search

Not Found

Linear Checks:1000

Binary Checks:10

Enter key element:345

Searching using linear search

Not Found

Searching using binary search

Not Found

Linear Checks:1000

Binary Checks:10

Enter key element:382

Searching using linear search

Not Found

Searching using binary search

Not Found

Linear Checks:1000

Binary Checks:10

Enter key element:231

Searching using linear search

Not Found

Searching using binary search

Not Found

Linear Checks:1000

Binary Checks:10

Enter key element:56

Searching using linear search

Not Found

Searching using binary search

Not Found

Linear Checks:1000

Binary Checks:10

Enter key element:69

Searching using linear search

Not Found

Searching using binary search

Not Found

Linear Checks:1000

Binary Checks:10

The average number of checks for 20 were

Linear Search: 827.1

Binary Search: 9.8


Related Solutions

I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
I need to make this into a Java Code: Write a program that prompts the user...
I need to make this into a Java Code: Write a program that prompts the user for a double value representing a radius. You will use the radius to calculate: circleCircumference = 2πr circleArea = πr2 sphereArea = 4πr2 sphereVolume = 43πr3 You must use π as defined in the java.lang.Math class. Your prompt to the user to enter the number of days must be: Enter radius: Your output must be of the format: Circle Circumference = circleCircumference Circle Area...
Write a complete Java program to print out the name bob
Write a complete Java program to print out the name bob
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
I need the Java Code and Flowchart for the following program: Suppose you are given a...
I need the Java Code and Flowchart for the following program: Suppose you are given a 6-by-6 matrix filled with 0s and 1s. All rows and all columns have an even number of 1s. Let the user flip one cell (i.e., flip from 1 to 0 or from 0 to 1) and write a program to find which cell was flipped. Your program should prompt the user to enter a 6-by-6 array with 0s and 1s and find the first...
i need this program to also print out the number of combinations #include <stdio.h> #include <string.h>...
i need this program to also print out the number of combinations #include <stdio.h> #include <string.h> #define N 10 void generate(char *array, int n) {    if (n==0)    {        printf("%s\n",array);        return;    }    for (int i = 0; i < n; ++i)    {        // generate all of the permutations that end with the last element        generate(array, n-1);        // swap the element        char temp = array[n-1];...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main {...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main { static int count=0; int calculate(int row, int column) { count++; if(row==1&&column==1) { return 0; } else if(column==1) { return ((200+calculate(row-1,column))/2); } else if(column==row) { return (200+calculate(row-1,column-1))/2; } else { return ((200+calculate(row-1,column-1))/2)+((200+calculate(row-1,column))/2); }    } public static void main(String[] args) { int row,column,weight; Main m=new Main(); System.out.println("Welcome to the Human pyramid. Select a row column combination and i will tell you how much weight the...
In java coding, write a program that will print out a representation of a “W” using...
In java coding, write a program that will print out a representation of a “W” using a character that a user provides.
Write a Java program that accepts a sequence of commands and print out the number of...
Write a Java program that accepts a sequence of commands and print out the number of successful insertions, the number of successful deletions, the number of successful searches (through the “find” command), the number of items remaining in the list after executing all commands and the final contents of the list. Three commands that will be taken as inputs are “insert”, “delete” and “find”. Input Line 1: The number of transactions m on the list, where 1  m 200. Line 2...
I NEED IT PRINT IN THIS FORM JAVA How is a Java source file executed? 1....
I NEED IT PRINT IN THIS FORM JAVA How is a Java source file executed? 1. It is compiled and runs natively 2. It is interpreted but not compiled 3. It is compiled into byte code and executed by the Java Virtual Machine 4. It is magic Please enter your answer 1 Sorry that answer is not correct. The correct answer is 3 Correct output: How is a Java source file executed? 1. It is compiled and runs natively 2....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT