二分查找实验代码

新代码
package test;

/**
 * http://www.cn-cuckoo.com/2010/04/20/are-you-one-of-the-10-percent-of-programmers-who-can-write-a-binary-search-1530.html
 * 
 * 二分查找
 * 2010-04-21 12:00 至 2010-04-21 12:36 
 *
 */
public class BinarySearch {
	
	/**
	 * 会出现死循环
	 * @param data
	 * @param value
	 * @param min
	 * @param max
	 */
	private static void doSearch_bak(int[] data, int value, int min, int max)
	{
		int mid = (min+max)/2;
		System.out.print("min:" + min);
		System.out.print(" max:" + max);
		System.out.println(" mid:" + mid);
		if(data[mid]==value)
		{
			System.out.println("找到数据:" + value);
			System.out.println("********************");
			System.out.println();
		}
		else if(value<data[mid])
		{
			System.out.println("数据在前半区");
			doSearch_bak(data, value, min, mid);
		}
		else if(value>data[mid])
		{
			System.out.println("数据在后半区");
			doSearch_bak(data, value, mid, max);
		}
	}

	private static void doSearch(int[] data, int value, int min, int max)
	{
		if(max-min==1)
		{
			System.out.println("###没有找到数据:" + value);
			System.out.println("********************");
			System.out.println();
			return;
		}
		
		int mid = (min+max)/2;
		System.out.print("min:" + data[min]);
		System.out.print(" max:" + data[max]);
		System.out.println(" mid:" + data[mid]);
		if(data[mid]==value)
		{
			System.out.println("找到数据mid:" + value);
			System.out.println("********************");
			System.out.println();
		}
		else if(value<data[mid])
		{
			System.out.println("数据在前半区");
			doSearch(data, value, min, mid);
		}
		else if(value>data[mid])
		{
			System.out.println("数据在后半区");
			doSearch(data, value, mid, max);
		}
	}
	
	private static void search(int[] data, int value, int min, int max)
	{
		if(data[min]==value || data[max]==value)
		{
			System.out.println("找到数据min max:" + value);
			System.out.println("********************");
			System.out.println();
			return;
		}
		doSearch(data, value, min, max);
	}
	
	
	private static void test01()
	{
		int[] data = new int[100];
		for(int i=0; i<100; i++)
		{
			data[i] = i;
		}
		
		for(int i=0; i<100; i++)
		{
			System.out.println("********************");
			System.out.println("search:" + i);
			search(data, i, 0, 99);
		}
		
	}

	private static void test02()
	{
		int[] data = new int[100];
		for(int i=0; i<100; i++)
		{
			data[i] = 2*i;
		}
		
		for(int i=0; i<200; i++)
		{
			System.out.println("********************");
			System.out.println("search:" + i);
			search(data, i, 0, 99);
		}
		
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//test01();
		test02();
	}

}




旧代码
package test;

/**
 * http://www.cn-cuckoo.com/2010/04/20/are-you-one-of-the-10-percent-of-programmers-who-can-write-a-binary-search-1530.html
 * 
 * 二分查找
 * 2010-04-21 12:00 至 2010-04-21 12:36 
 *
 */
public class BinarySearch {
	
	/**
	 * 会出现死循环
	 * @param data
	 * @param value
	 * @param min
	 * @param max
	 */
	private static void doSearch_bak(int[] data, int value, int min, int max)
	{
		int mid = (min+max)/2;
		System.out.print("min:" + min);
		System.out.print(" max:" + max);
		System.out.println(" mid:" + mid);
		if(data[mid]==value)
		{
			System.out.println("找到数据:" + value);
			System.out.println("********************");
			System.out.println();
		}
		else if(value<data[mid])
		{
			System.out.println("数据在前半区");
			doSearch(data, value, min, mid);
		}
		else if(value>data[mid])
		{
			System.out.println("数据在后半区");
			doSearch(data, value, mid, max);
		}
	}

	private static void doSearch(int[] data, int value, int min, int max)
	{
		int mid = (min+max)/2;
		System.out.print("min:" + min);
		System.out.print(" max:" + max);
		System.out.println(" mid:" + mid);
		if(max-min==1)
		{
			System.out.println("找到数据:" + value);
			System.out.println("********************");
			System.out.println();
		}
		else if(data[mid]==value)
		{
			System.out.println("找到数据:" + value);
			System.out.println("********************");
			System.out.println();
		}
		else if(value<data[mid])
		{
			System.out.println("数据在前半区");
			doSearch(data, value, min, mid);
		}
		else if(value>data[mid])
		{
			System.out.println("数据在后半区");
			doSearch(data, value, mid, max);
		}
	}
	
	private static void test01()
	{
		int[] data = new int[100];
		for(int i=0; i<100; i++)
		{
			data[i] = i;
		}
		
		for(int i=0; i<100; i++)
		{
			System.out.println("********************");
			System.out.println("doSearch:" + i);
			doSearch(data, i, 0, 99);
		}
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		test01();
	}

}

你可能感兴趣的:(html)