?? jsp.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" /> 線條個數:<input type="text" size="3" value=20 id="lineNum" /> 阻塞時間(毫秒):<input type="text" size="3" id="waitTime" value=100 /> <label for="showDemo" >顯示過程</label><input checked type="checkbox" name="showDemo" id="showDemo"/>');
document.write('<input type="button" id="init" onclick="init();" value="生成隨機數" /> ');
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 + -