这样<script language=javascript> var a = new Array(1, 5, 9, 14, 27, 39, 41,50); var s = a.toString(); var m=8; var newa = s.split(",9,")[0]+","+m+",9,"+s.split(",9,")[1]; alert(newa.split(',')) </script>
才2000左右呀,不是很大的<script> a = new Array(); function init() { for(var i=0;i<2000;i++) { a[i]=Math.floor(Math.random()*1000) } a=a.sort(function(a,b){return (parseInt(a)>parseInt(b))?1:-1}); document.getElementById("div3").innerText="初始化的数组"+a } function go() { var index=Math.floor(Math.random()*1000) document.getElementById("div4").innerText="新插入的值"+index var beginTime=new Date() var flag=0 var str=a.join(",").replace(/\d+/g,function(a){ if(flag==1){ return a; } else { if(parseInt(a)>parseInt(index)){ flag=1;return index+","+a; }else{ return a } } } ) if(flag==0) { str=str+","+index } a=str.split(",") document.getElementById("div2").innerText="最后的数组"+a document.getElementById("div1").innerHTML="用时"+(new Date()-beginTime) } </script> <input name="button" value="初始化数组" type="button" onclick=init()> <input name="button" value="插入一个值" type="button" onclick=go()> <div id="div3"> </div> <div id="div4"> </div> <div id="div1"> </div> <div id="div2"> </div>
如果要追求效率,当然用二分法最快<script> var arr = new Array(1, 3,5, 9,14, 27, 39, 41,50);function insert(arr,value){ var p1=0,p2=arr.length-1; while((p1+1<p2)){ var pos=p1+Math.floor((p2-p1)/2) arr[pos]<=value?p1=pos:p2=pos } return [].concat(arr.slice(0,p1+1),value,arr.slice(p2)) }alert(insert(arr,1)) alert(insert(arr,7)) alert(insert(arr,9)) alert(insert(arr,42)) alert(insert(arr,50))</script>
完善一下 <script language=javascript> var a = new Array(1, 5, 9, 14, 27, 39, 41,50); var s = a.toString(); var m=8;//查找的值 var n=9;//你想插入的值 var index = 2; //9的index var newa = []; if(index==0) { newa = (n+','+a.toString()).split(','); } else if(index==a.length-1) { newa = (a.toString()+","+n).split(','); } else { arrp = s.split(","+n+","); newa = arrp[0]+","+m+","+n+","+arrp[1]; } alert(newa)</script>
你的方法我已经试过,还是慢
var a = new Array(1, 5, 9, 14, 27, 39, 41,50);
a.push(8)
a=a.sort(function(a,b){return parseInt(a)>parseInt(b)})
alert(a)
</script>
这个方法可以。
效率高的话,楼主可以使用折半法
CutBug(这个冬天不太冷) :我已经用折半查找法找到要插入值(如9)的索引,但怎么插入到9前面呢?
如果用排序,这样排序一次还可以接受,但我要插入的值有两千条,比如我在9前面插入了8,但还要在8前面插入7,这样每插入一条都要排序,用了1分多钟,效率太低
var a = new Array(1, 5, 9, 14, 27, 39, 41,50);
var s = a.toString();
var m=8;
var newa = s.split(",9,")[0]+","+m+",9,"+s.split(",9,")[1];
alert(newa.split(','))
</script>
a = new Array();
function init()
{
for(var i=0;i<2000;i++)
{
a[i]=Math.floor(Math.random()*1000)
}
a=a.sort(function(a,b){return (parseInt(a)>parseInt(b))?1:-1});
document.getElementById("div3").innerText="初始化的数组"+a
}
function go()
{
var index=Math.floor(Math.random()*1000)
document.getElementById("div4").innerText="新插入的值"+index
var beginTime=new Date()
var flag=0
var str=a.join(",").replace(/\d+/g,function(a){
if(flag==1){
return a;
}
else {
if(parseInt(a)>parseInt(index)){
flag=1;return index+","+a;
}else{
return a
}
}
}
)
if(flag==0)
{
str=str+","+index
}
a=str.split(",")
document.getElementById("div2").innerText="最后的数组"+a
document.getElementById("div1").innerHTML="用时"+(new Date()-beginTime)
}
</script>
<input name="button" value="初始化数组" type="button" onclick=init()>
<input name="button" value="插入一个值" type="button" onclick=go()>
<div id="div3">
</div>
<div id="div4">
</div>
<div id="div1">
</div>
<div id="div2">
</div>
var arr = new Array(1, 3,5, 9,14, 27, 39, 41,50);function insert(arr,value){
var p1=0,p2=arr.length-1;
while((p1+1<p2)){
var pos=p1+Math.floor((p2-p1)/2)
arr[pos]<=value?p1=pos:p2=pos
}
return [].concat(arr.slice(0,p1+1),value,arr.slice(p2))
}alert(insert(arr,1))
alert(insert(arr,7))
alert(insert(arr,9))
alert(insert(arr,42))
alert(insert(arr,50))</script>
<script language=javascript>
var a = new Array(1, 5, 9, 14, 27, 39, 41,50);
var s = a.toString();
var m=8;//查找的值
var n=9;//你想插入的值
var index = 2; //9的index
var newa = [];
if(index==0)
{
newa = (n+','+a.toString()).split(',');
}
else if(index==a.length-1)
{
newa = (a.toString()+","+n).split(',');
}
else
{
arrp = s.split(","+n+",");
newa = arrp[0]+","+m+","+n+","+arrp[1];
}
alert(newa)</script>
要求
版本 5.5
将指定的元素插入数组开始位置并返回该数组。
为什么要自己另起炉灶呢??
<script language=javascript>
var a=new Array(3,5,8)
a.splice(1,1,5,6)
alert(a)
alert(a.length)
</script>
join 方法 :返回字符串值,其中包含了连接到一起的数组的所有元素,元素由指定的分隔符分隔开来。arrayObj.join(separator)
pop 方法 :移除数组中的最后一个元素并返回该元素。arrayObj.pop()
push 方法 :将新元素添加到一个数组中,并返回数组的新长度值。arrayObj.push([item1 [item2 [. . . [itemN ]]]])
reverse 方法 :返回一个元素顺序被反转的 Array 对象。arrayObj.reverse( )
shift 方法 :移除数组中的第一个元素并返回该元素。arrayObj.shift( )
slice 方法 :返回一个数组的一段。arrayObj.slice(start, [end])
sort 方法 :返回一个元素已经进行了排序的 Array 对象。arrayobj.sort(sortfunction)
splice 方法 :从一个数组中移除一个或多个元素,如果必要,在所移除元素的位置上插入新元素,返回所移除的元素。arrayObj.splice(start, deleteCount, [item1[, item2[, . . . [,itemN]]]])
unshift 方法 :将指定的元素插入数组开始位置并返回该数组。arrayObj.unshift([item1[, item2 [, . . . [, itemN]]]])
将新元素添加到一个数组中,并返回数组的新长度值。arrayObj.push([item1 [item2 [. . . [itemN ]]]])
这个问题无论怎么样都涉及了快速查找(定位),现在的情况还是二分法比较快。
找到位置后具体是怎么来分开再组合新的数组,我觉得不是太影响效率的。都是需要用到CutBug(这个冬天不太冷) 介绍的方法的。
var a = new Array(1, 5, 9, 14, 27, 39, 41,50);
var b = new Array(2,3,6,9,10); // 需要插入的2000个数据,经过排序后的数组
var c = new Array(); // 创建新数组,用于保存插入新数据后的结果var tmpI = 0; // 数组a的下标
var tmpJ = 0; // 数组b的下标
var tmpK = 0; // 数组c的下标
var aLength = a.length;
var bLength = b.length;
while(tmpI<aLength && tmpJ<bLength)
{
if(a[tmpI]<=b[tmpJ])
{
c[tmpK]=a[tmpI];
tmpI++;
tmpK++;
}
else
{
c[tmpK]=a[tmpI];
tmpJ++;
tmpK++;
}
}if(tmpI<aLength)
{
// 将a数组追加到数组c的后面
}
else if(tmpJ<bLength)
{
// 将b数组追加到数组c的后面
}
<!--
var a = [1, 5, 9, 14, 27, 39, 41, 50];alert(a);a.splice(2, 0, 8);alert(a);
//-->
</SCRIPT>
大家为什么都考虑得这么复杂呢,很简单就可以搞定
用splice方法轻松搞定
这应该是最好的,毕竟是微软提供的插入接口。
免费的源码、工具网站,欢迎大家访问!
http://www.j2soft.cn/
═══════════════════