Java数据结构——二分查找

import java.lang.reflect.Array;
import java.nio.Buffer;
import java.util.Arrays;
import java.util.Random;

//=================================================
// File Name       :	Binary_Search
//------------------------------------------------------------------------------
// Author          :	Common

//类名:BinarySearch_Find
//属性:
//方法:
class BinarySearch_Find{
	private int[] temp;
	private int searchKey;
	private int lowerBound = 0;			//下界
	private int upperBound ;				//上界
	private int curNum;

	public int[] getTemp() {
		return temp;
	}

	public void setTemp(int[] temp) {
		this.temp = temp;
	}

	public BinarySearch_Find(int[] temp) {//构造函数
		this.temp = temp;
		this.upperBound = temp.length-1;
	}

	public int find(int searchKey){
		this.searchKey = searchKey;
		while(true){
			curNum = (lowerBound+upperBound)/2;
			if(temp[curNum]==this.searchKey){
				return curNum;							//find
			}
			else if(lowerBound>upperBound){
				return -1;										//没有find
			}
			else{
				if(temp[curNum]<this.searchKey){
					lowerBound = curNum+1;
				}
				else{
					upperBound = curNum-1;
				}
			}
		}
	}

}

//类名:RandomArrays
//属性:
//方法:
class RandomArrays{					//生成随机数组,有Num个

	public int[] getArrays(int Num){
		int[] Arrays = new int[Num];
		Random r = new Random();

		for(int i=0;i<Num;i++){
			Arrays[i] = r.nextInt(1000);
//			System.out.print(Arrays[i]+"、");
		}
		return Arrays;
	}
}

//类名:OrderedArrays
//属性:
//方法:
class OrderedArrays{					//生成有序数组,从0开始到Num

	public int[] getArrays(int Num){
		int[] Arrays = new int[Num];

		for(int i=0;i<Num;i++){
			Arrays[i] = i;
//			System.out.print(Arrays[i]+"、");
		}
		return Arrays;
	}
}

//主类
//Function        : 	Binary_Search
public class Binary_Search {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根

//		RandomArrays array_demo = new RandomArrays();
//		BinarySearch_Find arrays = new BinarySearch_Find(array_demo.getArrays(100));

		OrderedArrays array_demo = new OrderedArrays();
		BinarySearch_Find arrays = new BinarySearch_Find(array_demo.getArrays(100));
		System.out.println(Arrays.toString(arrays.getTemp()));
		System.out.println(arrays.find(1000));

	}

}

时间: 2024-08-12 12:15:17

Java数据结构——二分查找的相关文章

Java 实现二分查找\折半查找

二分查找又称折半查找,优点是比较次数少,查找速度快:其缺点是要求待查表为有序表,且插入删除困难.因此,折半查找方法适用于不经常变动而查找频繁的有序列表. 该算法要求: 1.  必须采用顺序存储结构. 2.  必须按关键字大小有序排列. 该算法时间复杂度最坏为:O(logn) 注意点有mid.low.high 其Java实现代码如下(该代码有缺陷,只是基本实现,有待完善): public class BinarySearch { /** * @param args */ public static

数据结构——二分查找【转】

转自: http://www.lishiyu.cn/post/45.html 二分法(折半查找) -----------效率高,但要求序列必须有序-->使用范围小了 15/2取7不是8 /* * 二分查找算法 --- 递归算法 * */ int binSearch( int array[], int low ,int high, int key) { if(low<=high) { int mid =(low + high)/2; if(key == array[mid]) return mi

Java数组二分查找

二分查找法一般指二分查找 二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好:其缺点是要求待查表为有序表,且插入删除困难.因此,折半查找方法适用于不经常变动而查找频繁的有序列表.首先,假设表中元素是按升序排列,将表中间位置记录的关键字与查找关键字比较,如果两者相等,则查找成功:否则利用中间位置记录将表分成前.后两个子表,如果中间位置记录的关键字大于查找关键字,则进一步查找前一子表,否则进一步查找后一子表.重复以上过程,直到找到满足条件的记录,使查找成功,或直到子表不存在为止,此时查找

java 实现二分查找法

/** * 二分查找又称折半查找,它是一种效率较高的查找方法. [二分查找要求]:1.必须采用顺序存储结构 2.必须按关键字大小有序排列. * @author Administrator * */ public class BinarySearch { public static void main(String[] args) { int[] src = new int[] {1, 3, 5, 7, 8, 9}; System.out.println(binarySearch(src, 3))

java,二分查找法,网上查阅

二分查找又称折半查找,它是一种效率较高的查找方法. 折半查找的算法思想是将数列按有序化(递增或递减)排列,查找过程中采用跳跃式方式查找,即先以有序数列的中点位置为比较对象,如果要找的元素值小于该中点元素,则将待查序列缩小为左半部分,否则为右半部分.通过一次比较,将查找区间缩小一半. 折半查找是一种高效的查找方法.它可以明显减少比较次数,提高查找效率.但是,折半查找的先决条件是查找表中的数据元素必须有序. 折半查找法的优点是比较次数少,查找速度快,平均性能好;其缺点是要求待查表为有序表,且插入删除

[数据结构]二分查找

1,二分查找: 点击打开链接 [cpp] view plaincopy int Find(int arr[], int key,int length) { assert(arr!=NULL&&length>0); int low=0,high=length,mid; while(low<=high) { mid=(low+high)/2; if(arr[mid]==key) return mid; else { if(arr[mid]>key) high=mid-1; e

Java实现二分查找

public class BinarySearch { /** * 二分查找算法 * * @param srcArray 有序数组 * @param key 查找元素 * @return key的数组下标,没找到返回-1 */ public static void main(String[] args) { int srcArray[] = {3,5,11,17,21,23,28,30,32,50,64,78,81,95,101}; System.out.println(binSearch(sr

[数据结构] 二分查找与变种二分查找

1.二分查找 二分搜索(binary search),也称折半搜索(half-interval search).对数搜索(logarithmic search),是一种在有序数组中查找某一特定元素的搜索算法.搜索过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜索过程结束:如果某一特定元素大于或者小于中间元素,则在数组大于或小于中间元素的那一半中查找,而且跟开始一样从中间元素开始比较.如果在某一步骤数组为空,则代表找不到.这种搜索算法每一次比较都使搜索范围缩小一半. 除直接在一个数组

java 冒泡排序 二分查找

下面这个程序是先定义一个整型数组,然后将其中的元素反序赋值,再用冒泡排序进行排序以后用二分查找来查找其中是否有某个数,返回值为-1时表示这个数可能小于这个数组的最小值或大小这个数组的最大值,-2表示这个数比这个数组的最小值大,最大值小,但是数组中不包含这个数,代码如下: package com.jll.sort; public class ErFenSort {    static int[] unsorted;    public static void main(String[] args)