不断提高binarySearch的性能

import java.util.Random;

public class Test {
	static Random rand = new Random(47);
	static int[] a = new int[1000];
	static int len = a.length;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		for (int i = 0; i < 1000; i++)
			a[i] = rand.nextInt(1000);
		radixSort();
		for (int i : a)
			System.out.print(i + " ");
		System.out.println();
		int result = search(a, 7);
		System.out.println(result);
		result = search(7);
		System.out.println(result);
		long start = System.nanoTime();
		result = binarySearch(a, 0, len, 7);
		long end = System.nanoTime() - start;
		System.out.println("binarySearch: " + end);
		System.out.println(result);
	}

	private static int search(int[] a, int t) {
		long start = System.nanoTime();
		int l = -1;
		if (a[511] < t)
			l = 1000 - 512;
		if (a[l + 256] < t)
			l += 256;
		if (a[l + 128] < t)
			l += 128;
		if (a[l + 64] < t)
			l += 64;
		if (a[l + 32] < t)
			l += 32;
		if (a[l + 16] < t)
			l += 16;
		if (a[l + 8] < t)
			l += 8;
		if (a[l + 4] < t)
			l += 4;
		if (a[l + 2] < t)
			l += 2;
		if (a[l + 1] < t)
			l += 1;
		int p = l + 1;
		if (p > 1000 || a[p] != t)
			p = -1;
		long end = System.nanoTime() - start;
		System.out.println("if: " + end);
		return p;
	}

	private static int binarySearch(int[] a, int start, int len, int key) {
		int low = start - 1, high = start + len, guess;
		while (high - low > 1) {
			guess = (high + low) / 2;
			if (a[guess] < key)
				low = guess;
			else if (a[guess] == key)
				return guess;// 提前返回这个索引值
			else
				high = guess;
		}
		// 有一点需要注意的是,必须先判断key不在数组范围内的情况,不然的话,假如先执行return high,也就是判断if(a[high] ==
		// key),此时会导致数组越界异常
		if (high == start + len || low == start - 1)// key不在排好序的这个数组范围内
			return ~high;// 按位非运算,有个好处,当~high=-1时,知道所需查找的数小于这个数组的范围,当~high =
							// -(a.length+1),时,我们可以知道所需查找的key大于这个数组范围
		else
			return high;// 刚好high等于0或者等于a.length - 1的时候,即key等于数组的起始位置或终了位置
	}

	private static int search(int t) {
		long start = System.nanoTime();
		int l = -1;
		int delta = getFirstValue();
		while (delta != 0) {
			if (a[l + delta] < t)
				l += delta;
			delta >>= 1;
		}
		int result = l + 1;
		if (result > a[len - 1] || a[result] != t)
			result = -1;
		long end = System.nanoTime() - start;
		System.out.println("while: " + end);
		return result;
	}

	private static int getFirstValue() {
		int num = 2;
		while (num < a[len - 1])
			num <<= 1;
		return num >> 1;
	}

	private static void radixSort() {
		int[] tmp = new int[a.length];
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < a.length; j++)
				tmp[j] = a[j] / pow(10, i) % 10;
			a = countingSort(tmp, 9);
		}
	}

	public static int pow(int a, int n) {
		int result = 1;
		while (n != 0) {
			if ((n & 1) != 0) {
				result *= a;
			}
			a *= a;
			n = n >> 1;
		}
		return result;
	}

	private static int[] countingSort(int[] tmp, int max) {
		int[] c = new int[max + 1];
		int[] b = new int[tmp.length];
		for (int i = 0; i < c.length; i++)
			c[i] = 0;
		for (int i = 0; i < tmp.length; i++)
			c[tmp[i]] = c[tmp[i]] + 1;
		for (int i = 1; i < c.length; i++)
			c[i] = c[i] + c[i - 1];
		for (int i = tmp.length - 1; i >= 0; i--) {
			b[c[tmp[i]] - 1] = a[i]; // 这里等式右边为a[i]很重要
			c[tmp[i]] = c[tmp[i]] - 1;
		}
		return b;
	}

}

你可能感兴趣的:(c,String,search,Random,Class,import)