排序
var arr=[5,40,-41,2,6,-5,12,78,79,-1];
var temp;
function er(arr,num){
//冒泡排序
for(var j=0;j<arr.length-1;j++)
{for(var i=0;i<arr.length-1;i++)
{if(arr[i]>arr[i+1])
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
}
//二分查找法,查找数组中某个数字的当前索引值
var left=0;
var right=arr.length-1;
var middle;
while(left<=right){
middle=parseInt((left+right)/2);
if(num==arr[middle]){
return middle;
}else if(num>arr[middle]){
left=middle+1;
}else if(num<arr[middle]){
right=middle-1;
}else{
return -1
}
}
}
var num=5;
var erNum=er(arr,num)
document.write("<b>"+arr+"</b><br/>")
document.write(num+"的索引是"+erNum)