Working on an assignment and I keep running into an error. I'm not really that good at Java so I'm not sure what I'm doing wrong. The assignment wants me to evaluate recursive binary search, and I'm finding an error in the binarySearch method; it says "list = {"1,2,5,7,9,15"};" is an illegal start to expression, not a statement, and ';' expected even though there clearly is one. I don't know...I'm getting a little stressed out.
Here's that method:
//recursive binary search
public boolean binarySearch(int[] list, int target, int low, int high) {
list ={"1,2,5,7,9,15"};
target = 5;
boolean result = binarySearch(list, target, 0, list.length-1);
executions++;
int mid = (low + high) / 2;
if(!result){
System.out.println("Target not found -- bad search.");
}else{
System.out.println("Target found -- sucessful search!");
}
if (list[mid] == target) {
return true;
} else {
if (low > high) {
return false;
} else {
comparisons++;
if (list[mid] < target) {
return binarySearch(list, target, mid + 1, high);
} else {
return binarySearch(list, target, low, mid - 1);
}
}
And here's the entire code:
public class Problem2 {
public static int executions = 0;
public static int comparisons = 0;
public static int[] numbers = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
public static void main(String[] args) {
System.out.println("2nd fibonacci number: " + fibonacci(2));
System.out.println("Number of executions: " + executions);
System.out.println("Number of comparisons: " + comparisons);
executions = 0;
comparisons = 0;
System.out.println();
System.out.println("Number of operations: " + executions);
System.out.println("Number of comparisons: " + comparisons);
executions = 0;
comparisons = 0;
System.out.println();
System.out.println("Factorial of 7: " + nFactorial(7));
System.out.println("Number of operations: " + executions);
System.out.println("Number of comparisons: " + comparisons);
executions = 0;
comparisons = 0;
}
//recursive Nfactorial
public static int nFactorial(int n) {
comparisons++;
if (n <= 1) {
return 1;
} else {
executions++;
return n * nFactorial(n - 1);
}
}
//recursive fibonacci number
public static int fibonacci(int n) {
comparisons++;
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
executions++;
return fibonacci(n - 1) + fibonacci (n - 2);
}
}
//recursive binary search
public boolean binarySearch(int[] list, int target, int low, int high) {
list ={"1,2,5,7,9,15"};
target = 5;
boolean result = binarySearch(list, target, 0, list.length-1);
executions++;
int mid = (low + high) / 2;
if(!result){
System.out.println("Target not found -- bad search.");
}else{
System.out.println("Target found -- sucessful search!");
}
if (list[mid] == target) {
return true;
} else {
if (low > high) {
return false;
} else {
comparisons++;
if (list[mid] < target) {
return binarySearch(list, target, mid + 1, high);
} else {
return binarySearch(list, target, low, mid - 1);
}
}
}
}
}
Please help if you can. If you see anything else that's wrong, please let me know.