• 二分法


    1、核心算法

    public class OrdArray {
    
    	private long[] a;
    	private int nElems;
    	
    	public OrdArray(int max){
    		a = new long[max];
    		nElems = 0;
    	}
    	
    	public int size(){
    		return nElems;
    	}
    	
    	/**
    	 * 寻找值
    	 * @param searchKey
    	 * @return
    	 */
    	public int find(long searchKey){
    		return recFind(searchKey, 0, nElems-1);
    	}
    	
    	/**
    	 * 递归调用,二分法寻找值
    	 * @param searchKey
    	 * @param lowerBound
    	 * @param upperBound
    	 * @return
    	 */
    	private int recFind(long searchKey, int lowerBound, int upperBound){
    		int curIn;
    		
    		curIn = (lowerBound + upperBound)/2;//获取中间值
    		if(a[curIn] == searchKey){//寻找到值,返回
    			return curIn;
    		}else if(lowerBound > upperBound){//寻找不导致,自身大小
    			return nElems;
    		}else{//寻找不到值
    			if(a[curIn] < searchKey){//小于,再到小的区间二分寻找
    				return recFind(searchKey, curIn+1, upperBound);
    			}else{//大于,到大于的空间二分寻找
    				return recFind(searchKey, lowerBound, curIn-1);
    			}
    		}
    	}
    	
    	/**
    	 * 插入
    	 * @param value
    	 */
    	public void insert(long value){
    		int j;
    		//寻找值比插入之大的位置
    		for(j = 0; j< nElems; j++){
    			if(a[j]> value){
    				break;
    			}
    		}
    		//比插入值大的值向右移位
    		for(int k = nElems; k>j ; k--){
    			a[k] = a[k-1];
    		}
    		a[j] = value;
    		nElems ++;
    	}
    	
    	/**
    	 * 展示
    	 */
    	public void display(){
    		for(int i = 0; i< nElems; i++){
    			System.out.print(a[i] + " ");
    		}
    		System.out.println(" ");
    	}
    }



    2、测试代码:

    public class BinarySearchApp {
    
    	public static void main(String[] args){
    		//初始化
    		int maxSize = 100;
    		OrdArray arr;
    		arr = new OrdArray(maxSize);
    		//插入
    		arr.insert(72);
    		arr.insert(90);
    		arr.insert(45);
    		arr.insert(126);
    		arr.insert(54);
    		arr.insert(99);
    		arr.insert(144);
    		arr.insert(27);
    		arr.insert(135);
    		arr.insert(81);
    		arr.insert(18);
    		arr.insert(108);
    		arr.insert(9);
    		arr.insert(117);
    		arr.insert(63);
    		arr.insert(36);
    		//展示列表
    		arr.display();
    		//查找
    		int searchKey = 666;//指定查找值
    		if(arr.find(searchKey) != arr.size()){
    			System.out.println("Found " + searchKey);
    		}else{
    			System.out.println("Can't find "+ searchKey);
    		}
    		
    	}
    }

    3、结果:

    9 18 27 36 45 54 63 72 81 90 99 108 117 126 135 144  
    Can't find 666
    ===========================
    9 18 27 36 45 54 63 72 81 90 99 108 117 126 135 144  
    Found 99



    Reference:

    [1]  Robert Lalore(著) 计晓云,赵研,曾希,狄小菡(译), Java数据结构和算法(第二版),中国电力出版社,2004 :200-204

  • 相关阅读:
    PTA(Advanced Level)1009.Product of Polynomials
    PTA(Advanced Level)1002.A+B for Polynomials
    PTA(Advanced Level)1065.A+B and C
    PTA(Advanced Level)1046.Shortest Distance
    PTA(Advanced Level)1042.Shuffling Machine
    PTA(Basic Level)1046.划拳
    PTA(Basic Level)1060.爱丁顿数
    PTA(Basic Level)1053.住房空置率
    PTA(Basic Level)1023.组个最小数
    DOM4J熟知
  • 原文地址:https://www.cnblogs.com/ryelqy/p/10104126.html
Copyright © 2020-2023  润新知