简单的说就是制作一个网页,, 用户输入若干数字、
然后用javascript来排序、、
怎么用循环来读取这些数字、
<!DOCTYPE HTML PUBLIC "斗扒-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>冒泡排序</title>
<script type="text/javascript">
var bubbleSort = function (array) {//从后向前
if(!(array instanceof Array)) {
return;
}
var n = array.length;
for(var i = 0; i < n-1; i++) {
for(var j = n-1; j > i; j--) {
var a = array[j], b = array[j-1];
if(a < b) {
array[j-1] = a;
array[j] = b;
}
}
}
};
var bubbleSort2 = function(array) { //从前到后
if(!(array instanceof Array)) {
return;
}
var n = array.length;
for(var i = 0; i < n; i++) {
for(var j = 0; j<j-i-1;j++) {
var a = array[j], b= array[j+1];
if (a > b){
array[j] = b;
array[j+1] = a;
}
}
}
};
</script>
</head>
<body>
<p>输入实例:1,2,3,123,3,2,13123,3,6,7,1</p>
<input type="text" autocomplete="on" id="enterArray" style="width:400px;height:40px;line-height:40px;padding:2px;font-size:16px;" />
<button id="submit">排序</button>
<div id="show" style="font-weight:bold;font-size:20px;margin-top:30px;color:#f90;"><雀衡/div>
<script type="text/javascript">顷销做
document.getElementById('submit').onclick = function(event) {
var arr = stringToArray(document.getElementById('enterArray').value);
document.getElementById('show').innerHTML = "排序前:" + arr;
bubbleSort(arr);
document.getElementById('show').innerHTML += "<br />排序后:" + arr;
};
function stringToArray(str) {
var array = [];
var arr = str.split(',');
for(var i = 0; i<arr.length; i++) {
array.push(parseFloat(arr[i]));
}
return array;
}
</script>
</body>
</html>