二分法查找算法的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
第1次的最小值是:3.0
第1次的中间是:10.0
第1次的最大值是:17.0
第2次的最小值是:3.0
第2次的中间是:6.0
第2次的最大值是:9.0
第3次的最小值是:7.0
第3次的中间是:8.0
第3次的最大值是:9.0
第4次的最小值是:7.0
第4次的中间是:7.0
第4次的最大值是:7.0
对不起,数不存在

经过修改,已经避免了bug;不过如果还有其他bug,欢迎广大朋友检举,谢谢了;
...全文
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) {
hs.add((int) (Math.random() * 500));
}
// 转换为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("没找到");
}

}
beibuyangguang 2010-04-24
  • 打赏
  • 举报
回复
改成给里面输入数,比一开始指定好,呵呵,不过也比较简单,只是我这么想,呵呵!
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
  • 打赏
  • 举报
回复
顶起先,然后在帮你找bug..
suiyuefuchen 2010-04-23
  • 打赏
  • 举报
回复
欢迎有其他方法的朋友一起来分享代码;如果本例有bug,希望朋友们提出好建议;
michaellufhl 2010-04-23
  • 打赏
  • 举报
回复
  System.out.println("恭喜您做对了");
相关推荐

62,568

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告