<!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>
2 回答
慕莱坞1975833
TA贡献27条经验 获得超9个赞
function bubbleSort(items) {
var len=items.length;
for(var i=0;i<len;i++){
for(var j =i+1;j<len;j++){ //交换次数
if(items[i]>items[j]){
var num = items[j];
items[j]=items[i];
items[i] = num;
}
}
}
return items;
}
stop变量根本没用
添加回答
举报
0/150
提交
取消