1. 程式人生 > >Java二分查詢小例子

Java二分查詢小例子

Java二分查詢--迴圈,遞迴


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**測試資料99,231,*/
public class Test {
	private static int wwww;

	@SuppressWarnings("resource")
	public static void main(String[] args) throws IOException {
		List<String> list = addnum();
		for (String string : list) {
			System.out.println("list集合:" + string);
		}
		while (true) {
			System.out.println("輸入:");
			// 方法一:強大的Scanner類
			Scanner sc = new Scanner(System.in);
			int number = sc.nextInt();
			System.out.println("輸入的是:" + number + "\n---------------------");

			long timeStart = System.currentTimeMillis();
			int tototo = modelOne(list, number, 0, list.size() - 1);
			System.out.println("這個數取值在第:" + (tototo + 1) + "行");
			System.out.println("二分遞迴查詢耗時:"
					+ (System.currentTimeMillis() - timeStart) + "毫秒"
					+ "\n共呼叫modelOne()函式" + wwww + "次"
					+ "\n---------------------");

			long timeStart2 = System.currentTimeMillis();
			int tototo2 = modelTwo(list, number, 0, list.size() - 1);
			System.out.println("這個數取值在第:" + (tototo2 + 1) + "行");
			System.out.println("遍歷查詢耗時:"
					+ (System.currentTimeMillis() - timeStart2) + "毫秒"
					+ "\n---------------------");

			long timeStart3 = System.currentTimeMillis();
			int tototo3 = modelThree(list, number, 0, list.size() - 1);
			System.out.println("這個數取值在第:" + (tototo3 + 1) + "行");
			System.out.println("二分迴圈查詢耗時:"
					+ (System.currentTimeMillis() - timeStart3) + "毫秒"
					+ "\n---------------------");
		}
		// 方法二:
		// BufferedReader br = new BufferedReader(new
		// InputStreamReader(System.in));
		// System.out.println(br.readLine());
		// 方法三:
		// char i = (char) System.in.read();
		// System.out.println(i);

	}

	/** 二分查詢--遞迴 --key=需要查詢的值*/
	public static int modelOne(List<String> list, int key, int start, int end) {
		// 二分查詢--遞迴
		wwww += 1;
		 if (key < Integer.valueOf(list.get(start).split("\t")[0])
		 || key > Integer.valueOf(list.get(end).split("\t")[1])
		 || start > end) {
			 System.out.println(start+"---"+end);
		 return 0;
		 }
		int middle = (start + end) / 2;// 中間位置
		System.out.println("middle" + middle);
		if (key > Integer.valueOf(list.get(middle).split("\t")[1])) {
			if (key <= Integer.valueOf(list.get(middle).split("\t")[0])) {
				return middle;
			} else {
				return modelOne(list, key, middle + 1, end);
			}
		} else if (key < Integer.valueOf(list.get(middle).split("\t")[1])) {
			if (key >= Integer.valueOf(list.get(middle).split("\t")[0])) {
				return middle;
			} else {
				return modelOne(list, key, start, middle - 1);
			}
		} else {
			return middle;
		}
	}

	/** 遍歷查詢 --key=需要查詢的值 */
	public static int modelTwo(List<String> list, int key, int start, int end) {
		if (key < Integer.valueOf(list.get(start).split("\t")[1])
				|| key > Integer.valueOf(list.get(end).split("\t")[1])
				|| start > end) {
			return 0;
		}
		for (int i = 0; i < list.size(); i++) {
			if (key <= Integer.valueOf(list.get(i).split("\t")[1])) {
				return i;
			}
		}
		return 0;
	}

	/** 二分查詢---迴圈 --key=需要查詢的值 */
	public static int modelThree(List<String> list, int key, int start, int end) {
		if (key < Integer.valueOf(list.get(start).split("\t")[0])
				|| key > Integer.valueOf(list.get(end).split("\t")[1])
				|| start > end) {
			return 0;
		}
		while (start <= end) {
			int middle = (start + end) / 2;// 中間位置
			if (key > Integer.valueOf(list.get(middle).split("\t")[1])) {
				if (key <= Integer.valueOf(list.get(middle).split("\t")[0])) {
					return middle;
				} else {
					// return modelOne(list, key, middle + 1, end);
					start = middle + 1;
				}
			} else if (key < Integer.valueOf(list.get(middle).split("\t")[1])) {
				if (key >= Integer.valueOf(list.get(middle).split("\t")[0])) {
					return middle;
				} else {
					// return modelOne(list, key, start, middle - 1);
					end = middle - 1;
				}
			} else {
				return middle;
			}
		}
		return 0;
	}

	/** 新增資料到list */
	public static List<String> addnum() {
		List<String> list = new ArrayList<String>();
		int counts = 0;
		for (int i = 0; i < 10000; i++) {
			list.add((counts + 1) + "\t" + (counts + 5));
			counts += 5;
		}
		return list;
	}

	/** 新增資料到string[] */
	public static String[] addnum2() {
		List<String> list = new ArrayList<String>();
		int counts = 0;
		for (int i = 0; i < 10000; i++) {
			list.add((counts + 1) + "\t" + (counts + 5));
			counts += 5;
		}
		String[] allStrings = null;
		for (int i = 0; i < list.size(); i++) {
			allStrings[i] = list.get(i);
		}
		return allStrings;
	}
}