<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="wrap"></div>
<button type="button" id="btnBubble">冒泡</button>
</body>
<script>
var wrap = document.getElementById("wrap");
var bubble = document.getElementById("btnBubble");
var myArray= [4,5,3,2,1];
wrap.innerHTML=myArray;
bubble.addEventListener("click",function(){
bubbleSort(myArray);
});
function bubbleSort(items) {
var len=items.length;
var stop;
for(var i=0;i<len;i++){
for(var j =0,j=stop-i;j<stop;j++){ //交换次数
if(items[j]>items[j+1]){
var num = items[j];
items[j]=items[j+1];
items[j+1] = num;
}
}
}
return items;
}
</script>
</html>
慕莱坞1975833
相关分类