Howdy folks;
I have been assigned a problem by my professor, and I think I have completed all of the points specified, however; he still says that I have not completed the assignment. I would deeply appreciate any help that anyone can provide. I have posted both my code, and the assignment below.
ASSIGNMENT
CODE:
I have been assigned a problem by my professor, and I think I have completed all of the points specified, however; he still says that I have not completed the assignment. I would deeply appreciate any help that anyone can provide. I have posted both my code, and the assignment below.
ASSIGNMENT
Quote
In this program you will prove that on average, binary search works better than linear search.
1. Create a sorted list of 20 numbers.
2. Search for each element using linear search and print out the number of iterations before the item is found.
3. Now use binary search and print the same information.
4. Print the average number of iterations for each search.
5. You should be able to show that binary search does not always perform better than linear search but does perform better on average.
You will find the Binary Search and the Sequential search in the Unit 10 Coding folder.
1. Create a sorted list of 20 numbers.
2. Search for each element using linear search and print out the number of iterations before the item is found.
3. Now use binary search and print the same information.
4. Print the average number of iterations for each search.
5. You should be able to show that binary search does not always perform better than linear search but does perform better on average.
You will find the Binary Search and the Sequential search in the Unit 10 Coding folder.
CODE:
import java.util.*;
public class Assignment1
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
//point 1
int[] numberList = {
1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
16, 17, 18, 19, 20
};
System.out.println("Please enter a number from 1 to 20 to search for");
int userInput = 0;
try
{
userInput = console.nextInt();
}
catch(InputMismatchException e)
{
System.out.println("You must enter an integer");
}
if ((userInput <= 20) && (userInput >=1))
{
seqSearch(numberList, userInput);
binSearch(numberList, userInput);
}
}
public static void seqSearch(int[] anArray, int userInput)
{
int iterations = 0;
int sum = 0;
try
{
for(int x = 0; x < anArray.length; x++)
{
iterations++;
if(anArray[x] == userInput)
{
System.out.println("target found(linear)");
break;
}
sum += iterations;
}
//point 2
System.out.println("total number of iterations(linear): " + iterations);
System.out.println();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Something went wrong");
}
}
public static void binSearch(int[] anArray, int userInput)
{
int iterations = 0;
int start = 0;
int end = anArray.length - 1;
int mid;
int sum;
try{
while (start <= end)
{
iterations++;
mid = ((start + end) / 2);
if (userInput == anArray[mid])
{
System.out.println("target found(binary)");
break;
}
else if (userInput < anArray[mid])
{
end = mid-1;
}
else
{
start = mid + 1;
}
/*System.out.println("i" + iterations);
System.out.println("s" + start);
System.out.println("e" + end);
System.out.println("m" + mid);*/
}
// point 3
System.out.println("total number of iterations(binary): " + iterations);
System.out.println();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Something went wrong");
}
}
}