<html>
|
| <head>
|
| <meta http-equiv="Content-Type" content="text/html"; charset="big5" />
|
| <title>Sort</title>
|
| <script language="javascript">
|
| function loadfocus(){
|
| 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();
|
| var falg = true;
|
| for (var i = 0;i < num-1;i++){
|
| flag = false;
|
| for(var j = 0;j < num -i-1;j++){
|
| if (iArray[j+1] < iArray[j]){
|
| var tmp = iArray[j+1];
|
| iArray[j+1] = iArray[j];
|
| iArray[j] = tmp;
|
| flag=true;
|
| }
|
| }
|
| if (flag == false) break;
|
| }
|
| end = new Date().getTime();
|
| for (var x = 0;x < num;x++){sResult += "{" + iArray[x] + "}";}
|
| form.out_new.value=sResult;
|
| form.getTime.value = ((end-start)/1000 + "sec");
|
| form.input.focus();
|
| }
|
| </script>
|
| </head>
|
| <body onload="loadfocus()">
|
| <form name="form">
|
| 請輸入要產生幾個亂數並排序<input type="text" name="input" id="inputId"></br>
|
| <input type="button" value="排序" onClick="sort()"></br>
|
| 原始資料<input type="text" name="out_old"></br>
|
| 排序後資料<input type="text" name="out_new"></br>
|
| 程式執行時間<input type="text" name="getTime">
|
| </form>
|
| </body>
|
| </html> |
留言
張貼留言