here are some suggestions for your code.
1) Since the array is not updated, I suggest that you extract the size in a variable.
public static int find(int[] a, int x) {
//[...]
int arrLength = a.length;
//[...]
}
2) In my opinion, there's no gain to check if the boundaries are the current value, since the value can be anywhere.
else if (a[0] == x) {
return 0;
} else if (a[topOdd] == x) {
return topOdd;
}
3) I suggest that you move the check for the size before the variables, it will remove the calculation of the other variables when the array is empty.
int arrLength = a.length;
if(arrLength == 0) {
return -1;
}
int topEven = arrLength - 1;
int topOdd = arrLength - 1;
int bottomEven = 0;
int midEven = 0;
int bottomOdd = 1;
int midOdd = 0;
if (a[0] == x) {
return 0;
} else if (a[topOdd] == x) {
return topOdd;
}
4) I suggest that you add the parentheses on the else
, since it make the code harder to read, in my opinion.
5) I suggest that you make function to check if the number is odd or even.
private static boolean isEven(int value) {
return value % 2 != 0;
}
private static boolean isOdd(int value) {
return !isEven(value);
}
6) I suggest that you extract the loop parts in methods.
public static int find(int[] a, int x) {
//[...]
int maxLastArrayPosition = arrLength - 1;
Integer evenPosition = findNumberEven(a, x, maxLastArrayPosition);
if (evenPosition != null) {
return evenPosition;
}
//[...]
}
private static Integer findNumberOdd(int[] a, int x, int maxLastArrayPosition) {
int topOdd = maxLastArrayPosition;
int midOdd = 0;
int bottomOdd = 1;
while (topOdd > bottomOdd) {
midOdd = ((topOdd + bottomOdd) / 2);
if (isOdd(midOdd)) {
midOdd++;
}
if (x > a[midOdd]) {
topOdd = midOdd - 1;
} else if (x < a[midOdd]) {
bottomOdd = midOdd + 1;
} else {
return midOdd;
}
}
return null;
}
7) I suggest that you make a constant for the invalid value.
public static final int INVALID = -1;
Refactored code
public static final int INVALID = -1;
public static int find(int[] a, int x) {
int arrLength = a.length;
if (arrLength == 0) {
return INVALID;
}
int maxLastArrayPosition = arrLength - 1;
Integer evenPosition = findNumberEven(a, x, maxLastArrayPosition);
if (evenPosition != null) {
return evenPosition;
}
Integer oddPosition = findNumberOdd(a, x, maxLastArrayPosition);
if (oddPosition != null) {
return oddPosition;
}
return INVALID;
}
private static Integer findNumberOdd(int[] a, int x, int maxLastArrayPosition) {
int topOdd = maxLastArrayPosition;
int midOdd = 0;
int bottomOdd = 1;
while (topOdd > bottomOdd) {
midOdd = ((topOdd + bottomOdd) / 2);
if (isOdd(midOdd)) {
midOdd++;
}
if (x > a[midOdd]) {
topOdd = midOdd - 1;
} else if (x < a[midOdd]) {
bottomOdd = midOdd + 1;
} else {
return midOdd;
}
}
return null;
}
private static Integer findNumberEven(int[] a, int x, int maxLastArrayPosition) {
int topEven = maxLastArrayPosition;
int midEven = 0;
int bottomEven = 0;
while (topEven > bottomEven) {
midEven = ((topEven + bottomEven) / 2);
if (isEven(midEven)) {
midEven++;
}
if (x > a[midEven]) {
bottomEven = midEven + 1;
} else if (x < a[midEven]) {
topEven = midEven - 1;
} else {
return midEven;
}
}
return null;
}
private static boolean isEven(int midEven) {
return midEven % 2 != 0;
}
private static boolean isOdd(int value) {
return !isEven(value);
}
```