3

I have an array with boolean values. But sequence of elements like these: first go true values, and then false values. For example,

boolean[] booleans = {true, true, true, true, true,
                false, false, false, false, false, false};

So now we have a sorted array with boolean values, starting with true values if true value exists.

The task is to find first false element.
I created a class with search method using binary search algorithm.

public class BinarySearch {

    public static int search(boolean[] array) {

        int low = 0, mid = 0;
        int high = array.length - 1;
        boolean booleanValue;

        while (low <= high) {
            mid = (low + high) >>> 1;
            booleanValue = array[mid];
            if (booleanValue) low = mid + 1;
            else high = mid - 1;
        }

        return mid == array.length - 1 ? -(low + 1) : mid;
    }

    public static void main(String[] args) {

        boolean[] booleans = {true, true, true, true, true,
                false, false, false, false, false, false};

        int search = search(booleans);
        System.out.println("search = " + search);
    }
}

It is not working correct, i.e. sometimes it is returning previous element of searched one.
Finding element by searching iteratively is also not good idea, because array's size could be really big, and it will take much time.

Edit: Actually I need to search in MySQL database table. But table size is too big, and finding required row takes to much time, I want to use binary search algorithm for fastening.

Edit: MySQL table size more than 45 million rows. It takes about 30 seconds when searching required row by SQL query, no matter I added index to the column or not. Furthermore adding index in boolean doesn't give any effect.
When I use binary search it takes about 10 milliseconds. So I want the above method to be corrected.
Edit: For instance, I have DB table called "INFORMATION". And it has two columns "INFO" (TEXT) and "CHECKED" (BOOLEAN). Initial values of "INFO"s are false. And I will get first unchecked info, and get N rows starting from unchecked, then I will check them and mark them true. Process will be repeated until there is no unchecked infos.

30
  • Use a debugger. Commented Jan 31, 2017 at 14:06
  • And searching about the bug when the size is an odd number ? I don't see a question so I write this one ;) Commented Jan 31, 2017 at 14:08
  • @AxelH to find first false element Commented Jan 31, 2017 at 14:12
  • 2
    Why would you use Binary search to find the first false element?!! Commented Jan 31, 2017 at 14:14
  • 1
    @Null because it's asymptotically faster than a linear probe...? Commented Jan 31, 2017 at 14:15

2 Answers 2

5

I amended the Xin Huang answer and simplified the code a bit:

public static int search(boolean[] array) {

    int low = 0, mid;
    int high = array.length - 1;
    boolean booleanValue;

    while (low <= high) {
        mid = (low + high) >>> 1;
        booleanValue = array[mid];
        if (booleanValue) low = mid + 1;
        else if (low == mid) return mid;
        else high = mid;
    }
    return -low;
}

So now the method is returning index of first false element in the array if element found, and negative value if element not found.

Sign up to request clarification or add additional context in comments.

Comments

1

During loop, if booleanValue is false

  1. if low = mid, starting point and ending point meets, we found what we need
  2. else high = mid since mid is definitely a potential candidate

Modified as below:

    while (low <= high) {
        mid = (low + high) >>> 1;
        booleanValue = array[mid];
        if (booleanValue) {
            low = mid + 1;
        }
        else {
            if (low == mid) {
                break;
            }
            high = mid;
        }
    }

2 Comments

This code still has a bug when all values == true. You may want to update your return for special case mid == array.length -1.
please change if (low == mid) { break;} to if (low == mid) return mid, and last return to return -(low + 1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.