package Basics;

/**
 * Created by matao on 5/26/16.
 */
public class BinarySearch {
    public static void main(String[] args) {
        int a[] = {1, 2, 5, 6, 9, 15, 32, 87, 104};
        System.out.println(rank(17, a));
        System.out.println(rank(2, a));
        System.out.println(rank(32, a));
    }

    public static int rank(int target, int[] array) {
        return rank(target, array, 0, array.length - 1);
    }

    public static int rank(int target, int[] array, int low, int high) {
        if (low > high) {
            return -1;
        }

        int mid = low + (high - low) / 2;
        if (target < array[mid]) {
            return rank(target, array, low, mid - 1);
        } else if (target > array[mid]) {
            return rank(target, array, mid + 1, high);
        } else {
            return mid;
        }
    }
}

@@ -175,16 +175,166 @@
 , 104%7D;%0A
+        System.out.println(recursionRank(17, a));%0A        System.out.println(recursionRank(2, a));%0A        System.out.println(recursionRank(32, a));%0A%0A
         
@@ -469,32 +469,41 @@
 lic static int r
+ecursionR
 ank(int target, 
@@ -525,32 +525,41 @@
         return r
+ecursionR
 ank(target, arra
@@ -582,32 +582,189 @@
 th - 1);%0A    %7D%0A%0A
+    /**%0A     * @param target number to search in array%0A     * @param array  array must be sorted%0A     * @param low%0A     * @param high%0A     * @return%0A     */%0A
     public stati
@@ -770,16 +770,25 @@
 ic int r
+ecursionR
 ank(int 
@@ -978,32 +978,41 @@
         return r
+ecursionR
 ank(target, arra
@@ -1091,16 +1091,25 @@
 return r
+ecursionR
 ank(targ
@@ -1191,11 +1191,434 @@
  %7D%0A    %7D
+%0A%0A    public static int rank(int target, int%5B%5D array) %7B%0A        int low = 0, high = array.length - 1;%0A        while (low %3C= high) %7B%0A            int mid = low + (high - low) / 2;%0A            if (target %3C array%5Bmid%5D) %7B%0A                high = mid - 1;%0A            %7D else if (target %3E array%5Bmid%5D) %7B%0A                low = mid + 1;%0A            %7D else %7B%0A                return mid;%0A            %7D%0A        %7D%0A        return -1;%0A    %7D
 %0A%7D%0A

Refactor binary-search with non-recursion