简体   繁体   English

在InsertInOrder中使用递归二进制搜索

[英]Using a recursive binary search in an InsertInOrder

I'm reading in a text file of unsorted numbers 我正在读取未排序数字的文本文件

static void insertInOrder( int[] arr, int cnt, int newVal )
    {
        int belongs = -( bSearch( arr, 0, arr.length-1, newVal)) - 1;
        {
            for ( int i = cnt; i >= belongs+1 ; --i)
            {
                arr[i] = arr[i-1];
            }

        arr[belongs] = newVal;
    }

    }

    // We do not need to pass in count. The incoming lo and hi define the range
    public static int bSearch(int[] a, int lo, int hi, int key)
    {
    int mid = (lo+hi)/2;
    if(lo>hi)
        return -1;
    else if (a[mid]==key)
        return mid;
    else if (a[mid]<key)
        return bSearch(a, mid+1, hi, key);
    else
        return bSearch(a, lo, mid-1, key);
    }

The binary search is working, but my insertinorder isn't putting the numbers in ascending order and I can't figure out why. 二进制搜索有效,但是我的insertinorder并没有将数字升序排列,我也不知道为什么。 It's just printing the list out backwards. 它只是将列表向后打印。 And this has to be done using a recursive binary search in an insertinorder. 这必须使用insertinorder中的递归二进制搜索来完成。

Have you copied the latest code in to the question ???. 您是否已将最新代码复制到问题???中。

One obvious problem is: 一个明显的问题是:

int belongs = -( bSearch( arr, 0, arr.length-1, newVal)) - 1;

the method bSearch returns -1 when a value is not found in the array. 如果在数组中找不到值,则bSearch方法返回-1。 This will give an insertion point of -2 when newValue is not found. 当找不到newValue时,它将提供-2的插入点。 You need to change bSearch to return both wether the item was found and the position. 您需要更改bSearch以返回找到的项目和位置。 Two options 两种选择

  1. Create (and return) a new class 创建(并返回)一个新类

     private class FoundResult { boolean found = true; int position; } 
  2. When not found; 找不到时; return -hi-1 ie in bSearch some thing like: 返回-hi-1即在bSearch中类似:

     if(lo>hi) return -hi-1; 

For option 1, the code will be roughly (i have not tested this): 对于选项1,代码大致(我尚未测试过):

static void insertInOrder( int[] arr, int cnt, int newVal )     {

    int belongs = bSearch( arr, 0, arr.length-1, newVal)).position ;
    for ( int i = cnt; i >= belongs+1 ; --i) {
       arr[i] = arr[i-1];
    }

    arr[belongs] = newVal;
}

// We do not need to pass in count. The incoming lo and hi define the range
public static FoundResult bSearch(int[] a, int lo, int hi, int key)
{
int mid = (lo+hi)/2;
FoundResult ret;
if(lo>hi) {
    ret = new FoundResult();
    ret.found = false;
    ret.position = hi;
    return ret;
} else if (a[mid]==key) {
    ret = new FoundResult();
    ret.position = hi;
    return ret;
} else if (a[mid]<key) {
    return bSearch(a, mid+1, hi, key);
} else {
    return bSearch(a, lo, mid-1, key);
}

For option 2 the code is roughly: 对于选项2,代码大致为:

static void insertInOrder( int[] arr, int cnt, int newVal )     {

    int belongs = bSearch( arr, 0, arr.length-1, newVal)) ;
    if (belongs < 0) {
        belongs  = -1 - belongs ;
    }
    for ( int i = cnt; i >= belongs+1 ; --i) {
       arr[i] = arr[i-1];
    }

    arr[belongs] = newVal;
}

// We do not need to pass in count. The incoming lo and hi define the range
public static int bSearch(int[] a, int lo, int hi, int key)
{
int mid = (lo+hi)/2;
FoundResult ret;
if(lo>hi) {
    return - hi - 1;
} else if (a[mid]==key) {
    return hi;
} else if (a[mid]<key) {
    return bSearch(a, mid+1, hi, key);
} else {
    return bSearch(a, lo, mid-1, key);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM