Something stuck))) What's wrong with the search?

public class task22 { public static void main(String[] args) { int[] x = new int[100]; for (int i = 0; i < 100; i++) { x[i] = i; } System.out.println("Index: " + new BinarySearch().preRank(x, 36)); } static class BinarySearch { int deep = 0; int preRank(int[] x, int key) { if (null != x) { return rank(x, 0, x.length - 1, key); } return -1; } private int rank(int[] a, int lo, int hi, int key) { if (lo <= hi) { deep++; for (int i = 0; i < deep; i++) { System.out.print("*"); } System.out.println(" " + lo + " " + hi); int mid = lo + (hi - lo) / 2; if (a[mid] == key) return mid; else if (key < a[mid]) rank(a, lo, mid, key); else if (key > a[mid]) rank(a, mid, hi, key); } return -1; } } } 

    1 answer 1

    You have this:

     else if (key < a[mid]) rank(a, lo, mid, key); else if (key > a[mid]) rank(a, mid, hi, key); 

    And you need:

     else if (key < a[mid]) return rank(a, lo, mid, key); else if (key > a[mid]) return rank(a, mid, hi, key); 

    When a value is found, the recursion begins to reverse, and then return missed and -1 is returned, at the end of the method that ...