//冒泡排序 var temp;//临时变量 var attr = [8,4,6,3,12,5,7]; for (var j = attr.length;j > 0; j--) ////外层,控制有几个数值比较 { for (var i =0; i < j-1;i++) //内层, { if (attr[i]<attr[i+1]) //从大到小排序 { temp = attr[i+1] attr[i+1]= attr[i] attr[i] = temp } } } document.write(attr); /*for (var j = 0;j < attr.length; j++) //外层,控制有几个数值比较 { for (var i =0; i < attr.length-1;i++) //内层,取得最值 { if (attr[i]>attr[i+1]) { temp = attr[i+1] attr[i+1]= attr[i] attr[i] = temp } } }console.log(attr);*/