var x = [1,2,3,4,5,6,7,8,9,10];
// var x = (Array(101).join(0).split('')).map((a,i)=>{return i+1;});
var findIndex =  (target, x, lowIndex, highIndex) => {
	var low = lowIndex || 0;
	var high = highIndex === undefined ? (x.length - 1) : highIndex;
	var mid = Math.ceil((low + high) / 2);
    // var mid = Math.floor((low + high) / 2);
	// console.log('Mid : ', mid);
	if(target === x[mid]){
		return mid;
	} else {
		if(target < x[mid]){
			// console.log('Low : ', x.slice(low, mid));
			return findIndex(target, x, low, mid - 1);
		} else {
			// console.log('High : ', x.slice(mid, high));
			return findIndex(target, x, mid + 1, high);
		}
	}
};
x.forEach((z)=>{
	console.log(findIndex(z, x));
});

没有评论

  • :arrow:
  • :grin:
  • :???:
  • :cool:
  • :cry:
  • :shock:
  • :evil:
  • :!:
  • :idea:
  • :lol:
  • :mad:
  • :mrgreen:
  • :neutral:
  • :?:
  • :razz:
  • :oops:
  • :roll:
  • :sad:
  • :smile:
  • :eek:
  • :twisted:
  • :wink: