Objective: Implement both linear search and binary search, and see which one per
ID: 3666621 • Letter: O
Question
Objective:
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
Sort the array next
Return sorted 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
Explanation / Answer
import java.util.Arrays;
import java.util.Random;
public class LinearBinarySearch {
static Random random = new Random();
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");
int totalLinearCount = 0;
int totalBinaryCount = 0;
for (int i = 0; i < 20; i++) {
int[] dataArray = generateRandomArrayWithRandomNum();
int search = random.nextInt(1000);
int linearCount = performLinearSearch(dataArray, search);
int binaryCount = performBinarySearch(dataArray, search);
System.out.println("Linear Checks: "+linearCount);
System.out.println("Binary Checks: "+binaryCount);
totalLinearCount+=linearCount;
totalBinaryCount+=binaryCount;
}
System.out.println(" The average number of checks for 20 were:");
System.out.println("Linear Search "+totalLinearCount/20);
System.out.println("Binary Search "+totalBinaryCount/20);
}
private static int[] generateRandomArrayWithRandomNum() {
int size = random.nextInt(1000);
int[] dataArray = new int[size];
for (int i = 0; i < size; i++) {
dataArray[i] = random.nextInt(1000);
}
Arrays.sort(dataArray);
return dataArray;
}
private static int performLinearSearch(int[] dataArray, int search) {
System.out.println("Searching using linear search");
int count = 0;
boolean found = false;
for (int i = 0; i < dataArray.length; i++) {
count++;
if (dataArray[i] == search) {
found = true;
break;
}
}
if (found) {
System.out.println("Found!");
} else {
System.out.println("Not Found!");
}
return count;
}
private static int performBinarySearch(int[] dataArray, int search) {
System.out.println("Searching using binary search");
int count = 0;
boolean found = false;
int high = dataArray.length-1;
int low = 0;
int mid = 0;
while (low <= high) {
count++;
mid = (low + high) / 2;
if (dataArray[mid] > search) {
high = mid - 1;
} else if (dataArray[mid] < search) {
low = mid + 1;
} else {
found = true;
break;
}
}
if(found){
System.out.println("Found!");
}else{
System.out.println("Not Found!");
}
return count;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.