1. 程式人生 > 遊戲攻略 >《吸血鬼倖存者》各武器塔羅牌搭配推薦

《吸血鬼倖存者》各武器塔羅牌搭配推薦

package search;

import java.util.Arrays;

public class MidSearch {
	public static void main(String[] args) {
		int key = 11;
		int[] target = { 11, 14, 6, 90, 129, 1 };
		String sort = "" ;
		for(int i = 0;i<target.length;i++)
				sort += target[i]+",";
		System.out.println("排序前:" + sort);
		Arrays.sort(target);
		System.out.println();
		sort = "";
		for(int i = 0;i<target.length;i++)
			sort += target[i]+",";
		System.out.println("排序後:" + sort);
		System.out.println("元素在陣列中的位置:" + MidSearch.rank(key, target));
	}

	public static int rank(int key, int[] target) {
		return rank(key, target, 0, target.length - 1);
	}

	private static int rank(int key, int[] target, int lo, int hi) {

		if (lo > hi)
			return -1;
		int mid = lo + (hi - lo) / 2;
		if (key < target[mid])
			return rank(key, target, lo, mid - 1);
		if (key > target[mid])
			return rank(key, target, mid + 1, hi);
		else
			return mid;
	}
}
排序前:11,14,6,90,129,1,

排序後:1,6,11,14,90,129,
元素在陣列中的位置:2