public int findInsertIndex(long searchKey) {
        int lowBound = 0;
        int upperBound = nElems - 1;
        int curIn;

        while (true) {
            curIn = (lowBound + upperBound) / 2;
            if (a[curIn] == searchKey)
                return curIn;
            else if (lowBound > upperBound) {
                if (curIn == 0)
                    return curIn;
                else
                    return curIn + 1;
            } else {
                if (a[curIn] > searchKey) {
                    if (curIn - 1 < 0 || a[curIn - 1] < searchKey)
                        return curIn;
                    else
                        upperBound = curIn - 1;
                } else {
                    if (curIn + 1 > nElems - 1 || a[curIn + 1] > searchKey)
                        return curIn + 1;
                    else
                        lowBound = curIn + 1;
                }
            }
        }
    }