asfman
android developer
posts - 90,  comments - 213,  trackbacks - 0
<script>
var t=[];
Array.prototype.px=function() {
 var max=this[0],l=this.length;
 for(var i=1;i<l;i++) {
 if(max<this[i])
   max=this[i];
   }
 t[t.length]=max;
 for(var i=0;i<l;i++) {
 if(max==this[i])
  this.splice(i,1);
  }
 if(this.length>0)
   this.px();
 return t;
 }
 var a=[1,3,2,5,6,7,8,3,9];
 document.write(a.px()) 
</script>
从小到大排序的话,只要把max<this[i]改为max>this[i]就可以了。
posted on 2006-07-27 23:34 汪杰 阅读(730) 评论(2)  编辑 收藏 引用

FeedBack:
# re: 数字从大到小排序(原)
2006-07-28 16:55 | 汪杰
快速排序法
<script>
function Half(sAry)
{
this.sortArray = sAry;
this.count = 0;

this.swap = function(i,j)
{
var tmp = this.sortArray[i];this.sortArray[i] = this.sortArray[j];this.sortArray[j] = tmp;
this.count++;
}
this.partition = function(low,high)
{
var pivot,pos,tmp,i,j;
pos = low;
pivot = this.sortArray[pos];
for(i=low+1;i<=high;i++)
{
if (this.sortArray[i]<pivot)
{
pos++;
this.swap(pos,i);
}
}
this.swap(low,pos);
return pos;
}
this.QuickSort = function(low,high)
{
var pivot;
if (low<high)
{
pivot = this.partition(low,high);
this.QuickSort(low,pivot-1);
this.QuickSort(pivot+1,high);
}
}
this.HalfFind = function(sArray,fNum)
{
var low,high,i,t;
low = 0;
i = 1;
high = sArray.length-1;
t = Math.floor((high+low) / 2);
document.getElementById("findProcess").innerHTML="";
while ((sArray[t]!=fNum) && (sArray[low]!=fNum) && (sArray[high]!=fNum) && (low < high))
{
document.getElementById("findProcess").innerHTML+="第"+i+"次查找 -> "+t+"的位置:"+sArray[t]+"&nbsp;&nbsp;&nbsp;&nbsp;low="+low+"&nbsp;high="+high+"<br>";

if (fNum>sArray[t])
{
low = t+1;
t = Math.floor((high+low) / 2);
}
else
{
high = t-1;
t = Math.floor((high+low) / 2);
}
i++
}
if ((sArray[t]!=fNum) && (sArray[low]!=fNum) && (sArray[high]!=fNum) && low >= high)
{
document.getElementById("findProcess").innerHTML+="第"+i+"次查找 -> "+t+"的位置:失败 此队列没有"+fNum+"该数&nbsp;&nbsp;&nbsp;&nbsp;low="+low+"&nbsp;high="+high+"<br>";
}
else
{
if(sArray[t]==fNum)
tt = t;
else if(sArray[low]==fNum)
tt = low;
else
tt = high;

document.getElementById("findProcess").innerHTML+="第"+i+"次查找 -> "+tt+"的位置:"+sArray[tt]+" 找到了!&nbsp;&nbsp;&nbsp;&nbsp;low="+low+"&nbsp;high="+high+"<br>";
}
return t;
}
}
</script>
  回复  更多评论
  
# re: 数字从大到小排序(原)
2006-07-28 16:55 | 汪杰
二分法查找 确定数组中值为t的元素位置

要求输入的是已经排序的数组,一个数值,如果数组长度为0,则返回-1,如果没找到,也返回-1,如果找到了,就返回该元素在数组中的位置,下面代码包含二分查找的核心代码(16行)和一个测试用例。

<html>
<head>
<script language="javascript">
function binaryserach(x,t)
{
var l,u,m;//l为下界,u为上界,m为中间判断位
l = 0;
u = x.length - 1;
while (l<=u){
m = Math.floor((l+u)/2);
//document.write(l+"|"+m+"|"+u+"<br>");
if (x[m]<t)
l=m+1;
else if (x[m]==t)
return m;
else //x[m]>t
u=m-1;
}
return -1;
}



function test(){

var my_array = new Array();
for (i = 0; i < 100; i++)
{
my_array = i;
}
target=34;

document.write(binaryserach(my_array,target));

}
</script>
</head>
<body onLoad="test()">
</body>
</html>
  回复  更多评论
  
只有注册用户登录后才能发表评论。

<2006年7月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

常用链接

留言簿(15)

随笔分类(1)

随笔档案(90)

文章分类(727)

文章档案(712)

相册

收藏夹

http://blog.csdn.net/prodigynonsense

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 456695
  • 排名 - 6

最新随笔

最新评论

阅读排行榜

评论排行榜