如何处理从Java中的二进制搜索返回的否定索引?

时间:2015-03-25 21:34:04

标签: java arrays binary-search negative-number

所以我在回到负值时似乎遇到了一个小问题(如果数字中没有找到数字。)我被指示基本上采用负数它应该在哪里(如果它属于第7个索引空间则为-7)并使其为正索引数。

以下是具体说明:

// STEP #2 if the return value is negative then you realize that the number was not 
        // found in the array and this negative number is an encoded index of where the 
        // number belongs.  You must "decode" this index back to the real index it represents. 
        // You do this by doing the opposite operations in the reverse order they were done in 
        // when bsearch "encoded" the index. Once you have a non negative index you can go to 
        // the next step.  

0 个答案:

没有答案