<html>
<head>
<title>JS冒泡排序算法</title>
</head>
<body>
<script type="text/javascript">
var objArray = new Array();
objArray[0] = 14;
objArray[1] = 3;
objArray[2] = 8;
objArray[3] = 2;
objArray[4] = 7;
document.write("<p>冒泡排序之前:" );
for(var n=0;n<objArray.length;n++){
document.write(objArray[n] + " ");
}
document.write("</p>");
//开始冒泡
var i = 1;
var temp;
while (i<=objArray.length - 1) {
var arr = "";
var j = objArray.length - 1;
while (j>=i) {
if(objArray[j]<objArray[j-1]) {
//change
temp = objArray[j];
objArray[j] = objArray[j-1];
objArray[j-1] = temp;
}
j--;
}
//print
for (var m=0;m<objArray.length;m++) {
arr = arr + objArray[m] + " ";
}
document.write("<p>第" + i + "次冒泡排序之后:" + arr + "</p>");
i++;
}
</script>
</body>
</html>