Я обнаружил, что оператор if в цикле while не работает, даже если условие выведено как true.< /p>
Код: Выделить всё
public class Main {
public static void main(String[] args) {
int element = 47;
int[] array = {1, 2, 3, 7, 8, 11, 12, 15, 47, 67, 879, 12345, 3463457, 367847884};
System.out.println(
(binarySearch(array, element) != -1) ?
("Element found at index: " + binarySearch(array, element)) :
"Element not found"
);
}
public static int binarySearch(int[] array, int target) {
int left = 0;
int right = array.length - 1;
while (left
Подробнее здесь: [url]https://stackoverflow.com/questions/78654967/if-statement-isnt-working-properly-in-binary-search-even-the-condition-is-true[/url]