# 二分法查找算法的java又一具体实现；欢迎朋友来检查；

suiyuefuchen 2010-04-23 10:51:05
``````
package TestErfenCha;

public class TestErfenCha {
public static void search(double shu, double[] arr){
int len = arr.length-1;
int min = 0, max = len, mid = (max-min)/2;
int count = 1;
System.out.println("第"+count+"次的最小值是："+arr[min]);
System.out.println("第"+count+"次的中间是："+arr[mid]);
System.out.println("第"+count+"次的最大值是："+arr[max]);
count++;
while(shu!=arr[mid]){
if(shu>arr[max]){
System.out.println("对不起，数不存在");
break;
}
if(shu<arr[mid]){
min = min;
max = mid-1;
mid = min+(max-min)/2;
System.out.println("第"+count+"次的最小值是："+arr[min]);
System.out.println("第"+count+"次的中间是："+arr[mid]);
System.out.println("第"+count+"次的最大值是："+arr[max]);
if(mid<min){
System.out.println("对不起，没有找到");
break;
}
}
else if(shu>arr[mid]){
min = mid+1;
max = max;
mid = min+(max-min)/2;
System.out.println("第"+count+"次的最小值是："+arr[min]);
System.out.println("第"+count+"次的中间是："+arr[mid]);
System.out.println("第"+count+"次的最大值是："+arr[max]);
if(mid<min){
System.out.println("对不起，没有找到");
break;
}
}

if(shu==arr[mid]){
System.out.println("恭喜你找到元素");
break;

}
count++;

}
}

public static void main(String[] args){
double arr[] = new double[15];
for(int i=0;i<15;i++){
arr[i] = i+3;
System.out.print(arr[i]+" ");
}
System.out.println();
search(7.69,arr);
}
}

``````

3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0

...全文
262 11 打赏 收藏 转发到动态 举报

11 条回复

zfq642773391 2010-04-24
• 打赏
• 举报

``````
public class BinarySearch
{
public static final int NOT_FOUND = -1;

/**
* Performs the standard binary search
* using two comparisons per level.
* @return index where item is found, or NOT_FOUND.
*/
public static <AnyType extends Comparable<? super AnyType>>
int binarySearch( AnyType [ ] a, AnyType x )
{
int low = 0;
int high = a.length - 1;
int mid;

while( low <= high )
{
mid = ( low + high ) / 2;

if( a[ mid ].compareTo( x ) < 0 )
low = mid + 1;
else if( a[ mid ].compareTo( x ) > 0 )
high = mid - 1;
else
return mid;
}

return NOT_FOUND;     // NOT_FOUND = -1
}

// Test program
public static void main( String [ ] args )
{
int SIZE = 8;
Integer [ ] a = new Integer [ SIZE ];
for( int i = 0; i < SIZE; i++ )
a[ i ] = i * 2;

for( int i = 0; i < SIZE * 2; i++ )
System.out.println( "Found " + i + " at " +
binarySearch( a, i ) );

}
}
``````
li445970924 2010-04-24
• 打赏
• 举报

public static void main(String args[]) {
HashSet<Integer> hs = new HashSet<Integer>();
while (hs.size() < 100) {
}
// 转换为int
int al[] = new int[100];
int p = 0;
for (Integer i : hs) {
al[p] = i.intValue();
p++;
}
// 冒泡排序
boolean b = true;
while (b) {
b = false;
for (int i = 0; i < 99; i++) {
if (al[i] > al[i + 1]) {
int tmp = al[i];
al[i] = al[i + 1];
al[i + 1] = tmp;
b = true;
}
}
}

// 查找 ；二分查找法 ;
int sss = 234; // 查找的数234
int maxIndex = 99;
int minIndex = 0;
int index = (maxIndex - minIndex) / 2;//center
b = false;
while (minIndex < (maxIndex - 1)) {
if (sss > al[index]) {
minIndex = index;
index = minIndex + (maxIndex - minIndex) / 2;
}
if (sss < al[index]) {
maxIndex = index;
index = maxIndex - (maxIndex - minIndex) / 2;
}
if (sss == al[index]) {
b = true;
break;
}
}
for (int i = 0; i < al.length; i++) {
System.out.print(al[i] + "\t");
}
if (b) {
System.out.println("找到了" + (index + 1) + "对应的值" + al[index]);
} else {
System.out.println("没找到");
}

}
• 打赏
• 举报

abc130314 2010-04-24
• 打赏
• 举报

double[] arr={1,2,3,4};
search(0, arr); //数组越界
search(2, arr); //无任何提示
jaldd 2010-04-24
• 打赏
• 举报

charles361 2010-04-24
• 打赏
• 举报

liujm1182 2010-04-24
• 打赏
• 举报

zqfddqr 2010-04-24
• 打赏
• 举报

Mars_Ma_OK 2010-04-23
• 打赏
• 举报

suiyuefuchen 2010-04-23
• 打赏
• 举报

michaellufhl 2010-04-23
• 打赏
• 举报

``````  System.out.println("恭喜您做对了");
``````

62,568

• 近7日
• 近30日
• 至今