|
<html>
|
| <head>
|
| <meta http-equiv="Content-Type" content="text/html"; charset="big5" />
|
| <title>Selection_Sort</title>
|
| <script language="javascript">
|
| function loadEvent(){
|
| document.getElementById("inputID").focus();
|
| }
|
| function sort(){
|
| var num = parseInt(form.input.value);
|
| if (isNaN(num)){alert("重新輸入");form.input.focus();return;}
|
| var iArray = new Array(num);//產生陣列
|
| var sResult = "";
|
| //--------------Random-----------------//
|
| for (var i = 0;i < num;i++){
|
| iArray[i] = Math.floor(Math.random()*num);//Random
|
| sResult += "[" + iArray[i] + "]";
|
| }
|
| form.out_old.value = sResult;
|
| sResult = "";
|
| var start = 0;
|
| var end = 0;
|
| start = new Date().getTime();
|
| for(var i = 0;i < iArray.length-1;i++){
|
| var m = i;
|
| for (var j = i+1; j < iArray.length;j++){
|
| if (iArray[j] < iArray[m]) m = j;
|
| }
|
| //if (i != m){
|
| var tmp = iArray[m];
|
| iArray[m] = iArray[i];
|
| iArray[i] = tmp;
|
| //}
|
| }
|
| end = new Date().getTime();
|
| for (var x = 0;x < iArray.length;x++){sResult += "{" + iArray[x] + "}";}
|
| form.out_new.value=sResult;
|
| form.getTime.value = ((end-start)/1000 + "sec");
|
| form.input.focus();
|
| }
|
| </script>
|
| </head>
|
| <body onLoad="loadEvent()">
|
| <form name="form">
|
| <p>請輸入要產生幾個亂數並排序
|
| <input type="text" name="input" id="inputID"></br>
|
| </p>
|
| <p>
|
| <input type="button" value="排序" onClick="sort()">
|
| </br>
|
| </p>
|
| <p>
|
| 原始資料
|
| <input type="text" name="out_old"></br>
|
| </p>
|
| <p>
|
| 排序後資料
|
| <input type="text" name="out_new"></br>
|
| </p>
|
| <p>程式執行時間
|
| <input type="text" name="getTime">
|
| </p>
|
| </form>
|
| </body>
|
| </html> |
留言
張貼留言