二分法简单查找

import java.io.*;
import java.util.*;
public class BinarySearch{
 public static void main(String[] args) {
        System.out.println("Start read file...");
        int[] origin = readArrayFormTxt("array.txt");
        System.out.println("read file completed!");


        Arrays.sort(origin);
        System.out.println("Arrays after sort:");
        for (int number : origin) {
            System.out.println(number);
        }


        System.out.println("Pls input the Integer you want find:");
        Scanner sc = new Scanner(System.in);
        int result = binarySearch(sc.nextInt(),origin);

        System.out.println(result == -1 ? "not found" : "find success,the index is:"+result);
    }

//常规的二分法查找   
 public static int binarySearch(int num, int[] source){
        int lo = 0;
        int hi = source.length - 1;
        while(lo <= hi){
            int mid = lo + (hi - lo) / 2;
            if(num < source[mid]){
                hi = mid - 1;
            }else if(num > source[mid]){
                lo = mid + 1;
            }else{
                return mid;
            }
        }
        return -1;
        
    }

//递归:
//1.方法的第一条语句总是一个包含return的条件语句
//2.递归的调用总是尝试解决一个规模更小的子问题,这样递归才能收敛到最简单的情况
//3.递归调用的父问题和尝试解决的子问题之间不应该有交集,两个子问题各自操作的数组部分是不同的
public static int binarySearch(int num,int[] sourse,int lo, int hi){
  if(lo>hi) return -1;
  int mid = lo + (hi-lo)/2;
  if(num < sourse[mid]) return binarySearch(num,sourse,lo,mid-1);
  else if(num > sourse[mid]) return binarySearch(num,sourse,mid+1,hi);
  else return mid;
}

//从文件中读取数组
public static int[] readArrayFormTxt(String filePath){
        List<Integer> list = new ArrayList<Integer>();
        File file = new File(filePath);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            while ((tempString = reader.readLine()) != null) {
                list.add(Integer.parseInt(tempString));
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        int[] array = new int[list.size()];    
        for ( int i = 0;i<list.size();i++) {
            array[i] = list.get(i);
        }
        return array;
    }

 

public static void readFileByLines(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            System.out.println("");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            while ((tempString = reader.readLine()) != null) {
                System.out.println("line " + line + ": " + tempString);
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }
}


 

posted @ 2015-11-29 20:51  桃源仙居  阅读(127)  评论(0)    收藏  举报