二分查找(java)

/**
 * TODO
 */
package com.xeezee.array;

/**
 * 二分查找法
 * 
 * @author luoqinglong
 * @date 2012-7-30
 */
public class Half {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HighArray highArray = new HighArray(10);

		highArray.insertData(3);
		highArray.insertData(7);
		highArray.insertData(14);
		highArray.insertData(20);
		highArray.insertData(27);
		highArray.insertData(29);

		try {
			System.out.println(highArray.findKey(19));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

class HighArray {
	private final long[] a;
	private int currentIndex = 0;

	public HighArray(long[] orig) {
		this.a = orig;

	}

	public HighArray(int caption) {
		this.a = new long[caption];

	}

	public void insertData(long value) {
		if (this.currentIndex < this.a.length) {
			this.a[this.currentIndex] = value;
			this.currentIndex++;
		}

	}

	public boolean deleteData(long value) {
		int j;// delete index
		for (j = 0; j < this.currentIndex; j++) {
			if (value == this.a[j]) {
				break;
			}
		}
		if (j == this.currentIndex) {
			return false;
		} else {
			for (int k = j; k < this.currentIndex; k++) {
				this.a[k] = this.a[k + 1];
			}
			this.currentIndex--;
			return true;
		}

	}

	/**
	 * 返回查找数组的index
	 * 
	 * @param searchData
	 * @return 数组的index
	 * @throws InterruptedException
	 */

	public int findKey(int searchData) throws InterruptedException {
		int lowerBound = 0;
		int upperBound = this.currentIndex - 1;
		int destIndex;
		while (true) {
			destIndex = (lowerBound + upperBound) / 2;

			if (this.a[destIndex] == searchData) {
				return destIndex;

			} else if (this.a[destIndex] > searchData) {
				upperBound = destIndex - 1;
			} else {
				lowerBound = destIndex + 1;
			}
			System.out.println(upperBound + "--" + lowerBound);
			// 没有找到数据
			if (upperBound < lowerBound) {
				break;
			}

		}
		return -1;

	}

	public long[] getA() {
		return this.a;
	}
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值