问题
I have a sorted array and want to recursively find a position for inserting an element. E.g. an array 2,4,5,6,7,8,12,35 with an insertion value of 3 and the algorithm should return the index of the position where the 3 has to be inserted (index 1). The following code works sometimes, and other times it gets stuck in an infinite loop. Eventually my brain feels like jelly and I ask for your help. This is the code:
private static int insertRec(int[] a, int e, int l, int r) {
int mid = l + r / 2;
if (l == r || a[mid] == e) return mid;
if (e > a[mid]) insertRec(a, e, mid + 1, r);
if (e < a[mid]) insertRec(a, e, l, mid - 1);
return -1;
}
Edit with assumed working code:
private static int insertRec(int[] a, int e, int l, int r) {
int mid = (l + r) / 2;
if (l == r || a[mid] == e) return mid;
else if (e > a[mid]) return insertRec(a, e, mid + 1, r);
else if (e < a[mid]) return insertRec(a, e, l, mid - 1);
return -1;
}
回答1:
int mid = l + r / 2;
It should be
int mid = (l + r) / 2;
EDIT: moreover, you can check the description of the STL algorithm lower_bound which makes exactly what you want, as I understand. A implementation based on this would be like:
int lb(int a[], int last, int val) { // array, len of the array, element to insert
int it, count, step;
int first = 0;
count = (last-first);
while (count > 0) {
it = first;
step = count/2;
it += step;
if (a[it] < val) {
first = ++it;
count -= step+1;
} else
count = step;
}
return first;
}
EDIT2: your code has a few mistakes to work properly, among them:
in the second line you cannot check if
a[mid] == e
because the element you want to insert, may not exist in the array. This will cause to return -1 in several cases.the infinite loop arises from the way of computing
mid
and later assigningmid+1
ormid-1
.because the element you want to insert may be equal to some elements in the array, you will be returning incorrectly, since you compare for both
e > a[mid]
ande < a[mid]
.
I advice you to take a look at this fantastic post about binary search. In any case, here's the algorithm trying to follow the more possible your style and using the info of the post. Hope it helps.
private static int insertRec(int[] a, int e, int l, int r) {
int mid = l + (r - l) / 2;
if (l == r) return l;
else if (a[mid] < e) return insertRec(a, e, mid+1, r);
else return insertRec(a, e, l, mid);
}
来源:https://stackoverflow.com/questions/26950649/recursive-insert-via-binary-search