二分法的应用----------利用随机类生成数组,并用二分法对数组的元素进行查找以及插入操作

二分法的应用是分冶策略方法的典型代表

开发语言  java

开发工具  elipse3.2

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

class OrdArray {

    private long[] a;        

    private int nElems;      

    private int lowerBound;  

    private int upperBound;   

    private int curIn;        

    private int iCount;

    public OrdArray(int max) {   

        a = new long[max];      

        nElems = 0; }

    public int size() {

        return nElems; }

    public void prtn(){

           System.out.println("通过 "+iCount +" 次循环找到"); }

    public int find(long searchKey) {

        lowerBound = 0;

        upperBound = nElems - 1;

        while(true) {

               iCount++;

            curIn = (lowerBound + upperBound) / 2;

            if (a[curIn] == searchKey) {

                return curIn;        

            } else if (lowerBound > upperBound) {

                return nElems;

            } else {

                if (a[curIn] < searchKey) {

                    lowerBound = curIn + 1;    

                } else {

                    upperBound = curIn - 1; } } }}

    public void insert(long value) {

        lowerBound = 0;

        upperBound = nElems - 1;

     while(true) {

            curIn = (lowerBound + upperBound) / 2;

            if (lowerBound == upperBound) {

                if (a[curIn] > value) { // 如果要插入的数比所比较的数小

              for (int k=nElems; k>curIn; k--) { // 将该比较数及其以后的数向后移动一位

                        a[k] = a[k-1]; }

                    a[curIn] = value;

                    break;

                } else {  // 如果要插入的数比所比较的数大

                 for (int k=nElems; k>curIn+1; k--) { // 将该比较数以后的数向后移动一位

                        a[k] = a[k-1]; }

                    a[curIn+1] = value;

                    break; }               

            } else {

                if (lowerBound > upperBound) {

                    for (int k=nElems; k>curIn; k--) {

                        a[k] = a[k-1]; }

                    a[curIn] = value;

                    break; }

                if (lowerBound < upperBound) {

                    if (a[curIn] < value) {

                        lowerBound = curIn + 1;   

                    } else {

                        upperBound = curIn - 1;   }}}}  

        nElems++;                     

        System.out.print("数组a[]在未插入数字之前所包含的元素是: ");

        for(int i=1;i<a.length;i++)

               System.out.print(" "+a[i]+" ");

                        System.out.println(" ");

                        System.out.println("往数组a[]插入的元素是: "+value); }

   public void display() {       

        for (int j=0; j<nElems; j++) {

            System.out.print(a[j] + " ");  }

        System.out.println("");}

   public int enterNum(){

         int num=0;;

       try{

              String s="";

            BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

              s=in.readLine();

             num=Integer.parseInt(s);

         }catch(IOException e){}

                 System.out.print(" ");

                  return num; }}

   public class OrderedApp {

    public static void main(String[] args) {

        int maxSize = 10;         

        OrdArray arr = new OrdArray(maxSize);

        for(int i=0;i<maxSize;i++){

        System.out.print("请输入数组a[]要增加的元素: ");

        arr.insert(arr.enterNum());

        }

        System.out.print("请输入你要查找的数字 ");

        int searchKey =arr.enterNum();           

        if ((arr.find(searchKey)) != (arr.size())) {

            System.out.println("找到要查找的数字 " + searchKey);

        } else {

            System.out.println("不能找到该数字:  " + searchKey); }

        arr.display();// display items

        arr.prtn();

        arr.display();} }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值