现在的位置: 首页 > 综合 > 正文

用js做冒泡排序

2014年02月13日 ⁄ 综合 ⁄ 共 1376字 ⁄ 字号 评论关闭
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>

<body>
<h3>//Javascript DubbleSort Demo</h3>
<hr>
请输入排序序列:<input type=text id=source><input type=button value=排序 id=order>(输入序列请用半角逗号隔开)
<script language="javascript">
//code by douguoqiang1980@163.com
    document.getElementById("order").onclick=function(){
    var xPoint = screen.width/4;
    var yPoint = screen.height/4;
 var i,j,k,t
    var str = document.getElementById("source").value;
    arr = str.split(",");
 document.write("<div id=/"Layer1/" style=/"position:absolute;width:36px;height:191px;left: "+xPoint+"px;top: "+yPoint+"px;/">")
 document.write("<font color=red>原<br>序<br>列<br>:<br></font>")
    for(i=0;i<arr.length;i++){
      document.write(arr[i]+"<br>");
    }
 document.write("</div>");
 for(j=1;j<arr.length;j++){
  for(k=arr.length-1;k>0;k--){
   if(Number(arr[k])<Number(arr[k-1])){
    t = arr[k];
    arr[k] = arr[k-1];
    arr[k-1] = t;
   }
  }
  alert("开始第"+j+"趟排序:");
  document.write("<div id=/"Layer1/" style=/"position:absolute;width:36px;height:191px;left: "+(xPoint+(j*50))+"px;top: "+yPoint+"px;/">")
  document.write("<font color=red>第<br>"+j+"<br>趟<br>:<br></font>")
  for(i=0;i<arr.length;i++){
    document.write(arr[i]+"<br>");
  }
  document.write("</div>");
 }
  }
</script>
</body>
</html>

抱歉!评论已关闭.