大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
var data = [33, 86, 12, 9, 66, 5, 10, 45, 11, 3];
function sort(data){
var i=0;
while(idata[j+1]){
var x=data[j];
data[j]=data[j+1];
data[j+1]=x;
}
}
i++;
}
}
sort(data)
BinarySearch(data,33)
function BinarySearch(arr, target) {
let s = 0;
let e = arr.length - 1;
let m = Math.floor((s + e) / 2);
let sortTag = arr[s] <= arr[e];//确定排序顺序
while (s < e && arr[m] !== target) {
if (arr[m] > target) {
sortTag && (e = m - 1);
!sortTag && (s = m + 1);
} else {
!sortTag && (e = m - 1);
sortTag && (s = m + 1);
}
m = Math.floor((s + e) / 2);
}
if (arr[m] == target) {
console.log('找到了,位置%s', m);
return m;
} else {
console.log('没找到');
return -1;
}
}