public static int seek(int[] array, int arg, int sIndex, int eIndex) { int sIdx = sIndex; int eIdx = eIndex; int tmpIdx = 0; while (true) { tmpIdx = (sIdx + eIdx) / 2; if (sIdx == tmpIdx || eIdx == tmpIdx) break; if (arg > array[tmpIdx]) { sIdx = tmpIdx; } else { eIdx = tmpIdx; } System.out.println(sIdx + " - " + eIdx); } return arg < array[sIdx] ? array[sIdx] : array[eIdx]; } public static void main(String[] args) { int[] arr = { 1, 5, 9, 14, 27, 39, 41, 50, 346, 348, 349, 981, 1207, 8721 }; System.out.println(Search.seek(arr, 345, 0, arr.length - 1)); }这个就找它的索引,当范围缩小到两个时停止.然后第一个如果比arg大则返回第一个,否则返回第二个.
ChDw(米) ( ) 信誉:155 Blog 2006-12-26 17:23:11 得分: 0
如果你认为永远不存在345的情况,你只要认定index一定小于0就行了 int[] iArray ={ 1, 5, 9, 14, 27, 39, 41, 50, 346, 348, 349, 981, 1207, 8721 }; int num = 345; int index = Arrays.binarySearch(iArray, num); System.out.println(index + ":" + iArray[Math.abs(index + 1)]); Arrays.binarySearch会返回 (-(insertion point) - 1),也就是346的位置
高手瞻仰ing
JDK源码, 贴出来, 方便一些 /** * Searches the specified array of ints for the specified value using the * binary search algorithm. The array must be sorted (as * by the {@link #sort(int[])} method) prior to making this call. If it * is not sorted, the results are undefined. If the array contains * multiple elements with the specified value, there is no guarantee which * one will be found. * * @param a the array to be searched * @param key the value to be searched for * @return index of the search key, if it is contained in the array; * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The * <i>insertion point</i> is defined as the point at which the * key would be inserted into the array: the index of the first * element greater than the key, or <tt>a.length</tt> if all * elements in the array are less than the specified key. Note * that this guarantees that the return value will be >= 0 if * and only if the key is found. */ public static int binarySearch(int[] a, int key) { return binarySearch0(a, 0, a.length, key); } // Like public version, but without range checks. private static int binarySearch0(int[] a, int fromIndex, int toIndex, int key) { int low = fromIndex; int high = toIndex - 1; while (low <= high) { int mid = (low + high) >>> 1; int midVal = a[mid]; if (midVal < key) low = mid + 1; else if (midVal > key) high = mid - 1; else return mid; // key found } return -(low + 1); // key not found. }
public static void main(String[] args)
{
int[] iArray ={ 1, 5, 9, 14, 27, 39, 41, 50, 345, 346, 348, 349, 981, 1207, 8721 }; int arg = 345;
for(int i = 0;i < iArray.length;i++)
if((arg-arr[i]) < 0)
{
System.out.println(arr[i]);
break;
}
}
}
虽然不是什么算法,不过挺简单。
你要的值就是 iArray[Math.abs(Arrays.binarySearch(iArray,arg))-1];
二分查找时如果找不到返回的是比它小的那个值的索引+2的负值.
所以只要取正再减1就是比它大的那个值的索引.
谢谢,能否帮我把程序写出来。。
谢谢,我的数组里是没有arg(345)的。还有大家能否不要用JAVA的Array中的方法,其实我是用在javascript里的,javscript的Array功能很少。 在此只是求个算法
然后循环这个数组,用给定的值345飞别和每个元素做差值,判断差值与abs的绝对值大小,如果当前的差值小于abs,则把当前差值赋值给abs,并且将当前数组的索引值赋值给index,这样一次循环下来就可以知道最接近的值在数组的什么位置了,当然也就知道最接近的值是什么了。
int num = 346;
int index = Arrays.binarySearch(iArray, num);
if(index < 0)
System.out.println(index + ":" + iArray[Math.abs(index + 1)]);
else
System.out.println(index + ":" + iArray[index]);
注意。。
注意。。
注意。。 我的数组里没有345,并且数组里面永远不可能有345,请不要用JAVA中的Array里的方法,还有我的记录有上千条,也请不要用把整个记录循环一边的方法,我现在要的是效率,我目前的实现如下:public class ErFenFa {
int[] iArray ={ 1, 5, 9, 14, 27, 39, 41, 50, 346, 348, 349, 981, 1207, 8721 };int iSeek = 345;
int iCount = 0; public int find()
{
int iIndex = 0;
int iStart = 0;
int iEnd = iArray.length - 1;
while (true) {
iCount++;
iIndex = (iStart + iEnd) / 2;
if (iArray[iIndex] < iSeek) {
iStart = iIndex;
} else if (iArray[iIndex] > iSeek) {
iEnd = iIndex;
break;
}
//注:我就写到这里,下面准备这样写。。
我现在是采用二分法的思想,找出一个小于345但离它最近的索引,再找出一个大于345的索引(但可能不是最近的),然后根据小索引大索引把iArray截取小索引与大索引中间的数据,然后再利用大家说的计算差值或者其他方法,不过这样做还是要循环一边,如果我的记录有2000条,通过这个方法可以得出500条,但是循环500条还是很慢啊,所以请各位想一个高效的方法
return iCount;
}
}
if (positionA>=positionB) return positionA; int m = (positionA+positionB)/2
if(int[m] < arg){
return getValue(array,arg,m,positionB);
}esle if(int[m] > arg){
return getValue(array,arg,positionA,m);
}else if(int[m] = arg){
return m+1;
}
}int[] iArray ={ 1, 5, 9, 14, 27, 39, 41, 50, 346, 348, 349, 981, 1207, 8721 };int iSeek = 345;
getValue(iArray ,iSeek ,0,iArray.length);
用下递归,二分查找不知道速度怎么样
int[] iArray ={ 1, 5, 9, 14, 27, 39, 41, 50, 346, 348, 349, 981, 1207, 8721 };
int num = 345;
int index = Arrays.binarySearch(iArray, num);
System.out.println(index + ":" + iArray[Math.abs(index + 1)]);
Arrays.binarySearch会返回 (-(insertion point) - 1),也就是346的位置
带星的基本上不会骗人的
楼主应该好好考虑别人的建议
int eIdx = eIndex;
int tmpIdx = 0;
while (true) {
tmpIdx = (sIdx + eIdx) / 2;
if (sIdx == tmpIdx || eIdx == tmpIdx)
break;
if (arg > array[tmpIdx]) {
sIdx = tmpIdx;
} else {
eIdx = tmpIdx;
}
System.out.println(sIdx + " - " + eIdx);
} return arg < array[sIdx] ? array[sIdx] : array[eIdx]; }
public static void main(String[] args) {
int[] arr = { 1, 5, 9, 14, 27, 39, 41, 50, 346, 348, 349, 981, 1207,
8721 };
System.out.println(Search.seek(arr, 345, 0, arr.length - 1)); }这个就找它的索引,当范围缩小到两个时停止.然后第一个如果比arg大则返回第一个,否则返回第二个.
如果你认为永远不存在345的情况,你只要认定index一定小于0就行了
int[] iArray ={ 1, 5, 9, 14, 27, 39, 41, 50, 346, 348, 349, 981, 1207, 8721 };
int num = 345;
int index = Arrays.binarySearch(iArray, num);
System.out.println(index + ":" + iArray[Math.abs(index + 1)]);
Arrays.binarySearch会返回 (-(insertion point) - 1),也就是346的位置
高手瞻仰ing
/**
* Searches the specified array of ints for the specified value using the
* binary search algorithm. The array must be sorted (as
* by the {@link #sort(int[])} method) prior to making this call. If it
* is not sorted, the results are undefined. If the array contains
* multiple elements with the specified value, there is no guarantee which
* one will be found.
*
* @param a the array to be searched
* @param key the value to be searched for
* @return index of the search key, if it is contained in the array;
* otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
* <i>insertion point</i> is defined as the point at which the
* key would be inserted into the array: the index of the first
* element greater than the key, or <tt>a.length</tt> if all
* elements in the array are less than the specified key. Note
* that this guarantees that the return value will be >= 0 if
* and only if the key is found.
*/
public static int binarySearch(int[] a, int key) {
return binarySearch0(a, 0, a.length, key);
} // Like public version, but without range checks.
private static int binarySearch0(int[] a, int fromIndex, int toIndex,
int key) {
int low = fromIndex;
int high = toIndex - 1; while (low <= high) {
int mid = (low + high) >>> 1;
int midVal = a[mid]; if (midVal < key)
low = mid + 1;
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}
public static void main(String[] args) {
int[] iArray ={1,5,9,14,27,39,41,50,346,348,349,981,1207,8721};
int n = 345;
System.out.println(binarySearch(iArray,n));
}
static int binarySearch(int[] num,int n) {
int startidx = 0;
int endidx = num.length-1;
int index = 0;
boolean found = false;
while(startidx<=endidx && !found) {
index = (startidx+endidx)/2;
if(num[index]<n) {
startidx = index + 1;
} else if(num[index]>n) {
endidx = index -1;
} else {
found = true;
break;
}
}
if(!found){
if(endidx<0){
return 0;
} else if(startidx>=num.length){
return num.length-1;
} else if(num[startidx]-n>n-num[endidx]){
return endidx;
} else {
return startidx;
}
} else {
return index ;
}
}
}