For the real techies ...

Look at the following code. Its a simple binary search algorithm that has been tought during the first semester of any Computer Science degree. Its the same code which is available in the classic Programming Pearls book for the past 20 years and in the Java library for the past 10 years. Now, can you find a bug in this code?


    public static int binarySearch(int[] a, int key) {

        int low = 0;
        int high = a.length - 1;
        while (low <= high) {
            int mid = (low + high) / 2;
            int midVal = a[mid];

            if (midVal < key)
                low = mid + 1;
            else if (midVal > key)
                high = mid - 1;
            else
                return mid; // key found
        }
        return -(low + 1); // key not found.
    }


Don't worry. I'm not here to prove that I'm technically superior to you, because neither did I find the bug. But someone else did! Check this!

ஹூம். பாட்டு எழுதி பேர் வாங்கறவங்க சில பேர். அந்த பாட்டுல குத்தம் சொல்லி பேர் வாங்கறவங்க சில பேர்.

Posted in |

0 comments: