⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jsp.htm

📁 该文件是用JAVASCRIPT动态演示排序算法
💻 HTM
字号:
<html>   
  <head>   
  <title>排序算法演示</title>   
  </head>   
  <body>   
  <script   language="javascript">   
  var   showFlag=true;   //   是否需要中断显示   
  var   selfClose   =   true;   //   是否自动关闭对话框   
  var   stopSort   =   false;   //   是否执行排序   
  var   IsInit   =   false;   //初始化标志   
    
  var   N   =   20;//   排序的数组大小   
  var   Num;   //   排序的数组   
  var   intWaitTime;   //   排序阻塞时间   
    
  //   判断是否是用modal显示自己的,是的话就关闭自己   
  var   oMyObject   =   window.dialogArguments;   
  if(oMyObject!=null)   
  {   
  document.write("演示<br>");   
    
  if(oMyObject.selfClose){   
  document.write('<input   type="button"   onclick="javascript:oMyObject.selfClose=false;"   value="暂停"   />');   
  document.write('<input   type="button"   value="中止"   onclick="javascript:oMyObject.stopSort=true;oMyObject.IsInit=false;window.opener=null;window.close();"   />');   
  window.setTimeout(function(){window.opener=null;window.close();},oMyObject.intWaitTime);   
  }   
  else   
  {   
  document.write('<input   type="button"   value="继续"   onclick="javascript:if(!confirm(\'暂停当前?\'))oMyObject.selfClose=true;window.opener=null;window.close();"   onclick=""   />');   
  document.write('<input   type="button"   value="中止"   onclick="javascript:oMyObject.stopSort=true;oMyObject.IsInit=false;window.opener=null;window.close();"   />');   
  }   
  }   
  else   
  {   
  document.write('<style>iframe{margin:0px;padding:   0px;border:   1px   solid   #000000;}</style>');   
  document.write('<iframe   width="100%"   id="inner"   height="500"   name="inner"></iframe>');   
  document.write('<div   align="center">所用时间:');   
  document.write('<input   type="text"   size="8"   readonly="readonly"id="time"   />&nbsp;线条个数:<input   type="text"   size="3"     value=20   id="lineNum"   />&nbsp;阻塞时间(毫秒):<input   type="text"   size="3"   id="waitTime"   value=100   />&nbsp;<label   for="showDemo"   >显示过程</label><input   checked   type="checkbox"   name="showDemo"   id="showDemo"/>');   
  document.write('<input   type="button"   id="init"   onclick="init();"   value="生成随机数"   />&nbsp;');   
  document.write('<input   type="button"   onclick="BubbleSort();getLastMill();"   value="冒泡排序"   />');   
  document.write('<input   type="button"   onclick="SelectSort();getLastMill();"   value="直接选择排序"   />');   
  document.write('<input   type="button"   onclick=""   value="希尔排序"   />');   
  document.write('<input   type="button"   onclick="QuickSort(1,10);getLastMill();"   value="快速排序"   /></div>');   
  }   
  /////////////////////   排序开始   
  var   ischange;   
  var   temp;//   
  /*   定时器来控制   时的   堆栈数组   
  var   global   =   new   Array();//临时数组,存放过程变量   
  var   global_count=0;   
  */   
    
  var   innerObj;   
  var   startMill,endMill;   
    
  function   SleepByModal()   
  {   
  window.showModalDialog(   window.location.href,window,"dialogHeight:1px;dialogWidth:1px;status:no;help:no;center:1")   
  }   
  function   init_array()   
  {//初始化函数,随机生成N个数,存于数组中   
  selfClose=true;   
  stopSort   =   false   
  N   =   parseInt(document.getElementById("lineNum").value);   
  intWaitTime   =   parseInt(document.getElementById("waitTime").value);   
  showFlag   =   document.getElementById("showDemo").checked;   
  if(N>200)   
  {   
  if(!confirm("数组大小太大,可能运行时间很长,确定要运行?"))return;   
  }   
  if(intWaitTime<100)   
  {   
  if(!confirm("间隔时间过小,可能无法控制暂停,确定要运行?"))return;   
  }   
  Num   =   new   Array([N]);   
  var   rand;   
  for(var   i=0;i<N;i++)   
  {   
  rand   =   Math.round(Math.random()*800);   
  Num[i]   =   rand;   
  }   
  IsInit   =   true;   //已初始化   
  }   
    
  function   init_show()   
  {//初始化显示   
  innerObj=window.frames["inner"];   
  innerObj.document.body.innerHTML="";   
  var   innerTextString="";   
  if(IsInit)   
  {   
  for(var   i=0;i<N;i++)   
  {   
  innerTextString+="<div   id='img"+i+"'   style='background-color:   #000;width:   "+Num[i]+"px;height:2px;font-size:   2px;margin:1px;'></div>";   
  }   
  innerObj.document.body.innerHTML=innerTextString;   
  //document.getElementById("init").disabled   =   true;   
  }   
  else   
  {   
  alert("没有初始化!");   
  }   
  }   
    
  function   init()   
  {//初始化   
  /*   定时器来控制   时的   堆栈数组   
  global   =   new   Array();   
  */   
  selfClose=   true;   
  init_array();   
  init_show();   
  }   
  /*   本来是准备做一个定时器来控制,后来发现通用实现比较困难   
  function   Recycle()   
  {   
  if(global.length   >   global_count)   
  {   
  showIt(global[global_count]);   
  global_count++;   
  }   
  else   
  {   
  window.clearInterval(oInterval);   
  document.getElementById("init").disabled   =   false;   
  }   
  }   
  */   
  function   BubbleSort()   
  {//冒泡排序   
  var   start=new   Date()   
    startMill=start.getTime();   
    //////////////////////   
  if(IsInit)   
  {   
  var   i,j;   
  for(i=0;i<N;i++)   
  {   
  ischange   =   false;   
  for(j=N-1;j>=0;j--)   
  {   
  //global[global.length]=j;   
  if(Num[j+1]>Num[j])   
  {   
  if(stopSort)return;   
  temp   =   Num[j+1];   
  Num[j+1]   =   Num[j];   
  Num[j]   =   temp;   
  ischange   =   true;   
  showIt(j,j+1);   
  }   
    
  }//endfor   
  if(!ischange)   
  {   
  break;   
  }//endfi   
  }//endfor   
  /*   定时器来控制   
  global_count=0;   
  //oInterval   =   window.setInterval("Recycle()",10);   
  */   
  }   
  else   
  {   
  alert("未初始化!");   
  }   
  /////////////////////////   
  var   end=new   Date()   
  endMill=end.getTime();   
  }//BubbleSort   
    
  function   SelectSort()   
  {//直接选择排序   
  if(!IsInit)   
  {   
  alert("未初始化!");   
  return;   
  }   
  var   start=new   Date()   
    startMill=start.getTime();   
    /////////////////////   
  var   i,j;   
  for(i=0;i<N;i++)   
  {   
  for(j=N;j>i;j--)   
  {   
  if(Num[j]<Num[i])   
  {   
  if(stopSort)return;   
  temp   =   Num[i];   
  Num[i]   =   Num[j];   
  Num[j]   =   temp;   
  showIt(i,j);   
  }//endif   
  }//endfor   
  }//endfor   
  /////////////////   
  var   end=new   Date()   
  endMill=end.getTime();   
  }//SelectSort   
    
  function   QuickSort(low,high)   
  {//快速排序   
  if(stopSort)return;   
  var   pivotpos;   
  if(low<high)   
  {   
  pivotpos   =   Partition(low,high);   
  QuickSort(low,pivotpos-1);   
  QuickSort(pivotpos+1,high);   
  }//endif   
  }//QuickSort   
    
  function   Partition(i,j)   
  {//快速排序划分算法   
  var   pivot   =   Num[i];   
  while(i<j)   
  {   
  while(i<j&&Num[i]>=pivot)   
  {   
  j--;   
  }//endwhile   
  if(i<j)   
  {   
  Num[i++]   =   Num[j];   
  showIt(i,j);   
  }//endif   
  while(i<j&&Num[i]<=pivot)   
  {   
  i++;   
  }//endwhile   
  if(i<j)   
  {   
  Num[j--]=Num[i];   
  showIt(j,i);   
  }//endif   
  }//endwhile   
  Num[i]=pivot;   
  return   i;   
  }//Partition   
    
  function   HeapSort()   
  {//堆排序   
  var   i;   
  }   
    
  function   showIt(jj,ii)   
  {   
  innerObj.document.getElementById("img"+jj).style.width=Num[jj];   
  innerObj.document.getElementById("img"+ii).style.width   =   Num[ii];   
  if(showFlag)   
  {   
  SleepByModal();   
  }   
  }   
    
  function   getLastMill()   
  {   
  laserTime   =endMill   -   startMill;   
  if(laserTime==null||isNaN(laserTime)||laserTime<0)   
  laserTime=0;   
  document.getElementById("time").value=laserTime;   
  }   
  /*   
  function   showResult()   
  {   
  for(var   k=0;k<100;k++)   
  {   
  document.getElementById("img"+k).width=Num[k];   
  }   
  }   
  */   
  </script>   
  </body>   
  </html>   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -