【问题标题】:Insertion sort with binary search not working properly使用二进制搜索的插入排序无法正常工作
【发布时间】:2015-09-28 22:31:12
【问题描述】:

这是代码

for(out=1; out< dictSize; out++){
   String temp = dict[out];
   in=out;
   int lowerBound = 0;
   int upperBound = in-1;
   int curIn;  

   while (lowerBound <= upperBound){
      curIn = (lowerBound+upperBound)/2;
      if(dict[curIn].compareTo(temp) > 0){
           dict[in] = dict[curIn]; 
           dict[curIn]=temp;
           upperBound = curIn-1;
       } else{
           if(dict[curIn].compareTo(temp) < 0){
                 lowerBound=curIn + 1;
            }  
       }         
    }
 }

 //status = "sorted";
 for(String v: dict){
      System.out.println(v);
 }

这是没有正确排列的输出:

animal

animal

barter

cases

code

dictionary

file

simon

this

crazy

怎么了,怎么解决?

【问题讨论】:

    标签: java binary-search insertion-sort


    【解决方案1】:

    由于您处理的是数组而不是链表,因此将最后一个元素与中间元素交换是不够的。您必须移动数组的整个部分(例如使用 System.arraycopy)。所以这是我的版本:

    public static void main(String[] args) {
        String[] dict = {"this", "cases", "animal", "barter", "animal",
                "file", "code", "dictionary", "simon", "crazy"};
        for (int out = 1; out < dict.length; out++) {
            String temp = dict[out];
            int indexToInsert = binarySearch(dict, 0, out - 1, temp);
            if (indexToInsert < out) {
                // Next call copies elements [indexToInsert, out-1]
                // to elements [indexToInsert+1, out] (shift to the right)
                System.arraycopy(dict, indexToInsert, dict, 
                        indexToInsert + 1, out - indexToInsert);
                dict[indexToInsert] = temp;
            }
        }
    
        //status = "sorted";
        for(String v: dict){
            System.out.println(v);
        }
    }
    
    public static int binarySearch(String[] dict, int lowerBound, 
            int upperBound, String value) {
        while (lowerBound <= upperBound){
            int curIn = (lowerBound + upperBound)/2;
            int compRes = dict[curIn].compareTo(value);
            // compRes reflects relation between dict[curIn] and value from
            // input parameters. compRes is {-1,0,+1} for cases when
            // dict[curIn] {<,=,>} value correspondingly. For details see: 
            // http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo(java.lang.String) .
            if (compRes == 0) {
                return curIn;
            } else if (compRes > 0){
                upperBound = curIn - 1;
            } else {
                lowerBound = curIn + 1;
            }
        }
        return lowerBound;
    }
    

    另一种方法是使用二进制搜索的标准java实现:

    public static int binarySearch(String[] dict, int lowerBound, 
            int upperBound, String value) {
        int ret = Arrays.binarySearch(dict, lowerBound, upperBound + 1, value);
        return (ret < 0) ? -ret - 1 : ret;
    }
    

    【讨论】:

    • 谢谢!你能再解释一下你的版本吗,因为我在 compRes && System.arraycopy 迷路了。
    • 我在代码中添加了 cmets。您需要更多详细信息吗?
    猜你喜欢
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 2013-11-22
    相关资源
    最近更新 更多