45.@冒泡排序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了45.@冒泡排序相关的知识,希望对你有一定的参考价值。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>冒泡</title> 6 </head> 7 <body> 8 <script> 9 function bubble(str) { 10 for (var l = str.length; l > 0; l--) { 11 for (var i = 0; i < l - 1; i++) { 12 if (str[i] > str[i + 1]) { 13 var temp = str[i]; 14 str[i] = str[i + 1]; 15 str[i + 1] = temp; 16 } 17 } 18 19 } 20 return str; 21 } 22 23 var strs = [9, 2, 0, 1, 8]; 24 var newStr = bubble(strs); 25 console.log(newStr); 26 </script> 27 </body> 28 </html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>冒泡</title>
</head>
<body>
<script>
function bubble(str) {
for (var l = str.length; l > 0; l--) {
for (var i = 0; i < l - 1; i++) {
if (str[i] > str[i + 1]) {
var temp = str[i];
str[i] = str[i + 1];
str[i + 1] = temp;
}
}
}
return str;
}
var strs = [9, 2, 0, 1, 8];
var newStr = bubble(strs);
console.log(newStr);
</script>
</body>
</html>
以上是关于45.@冒泡排序的主要内容,如果未能解决你的问题,请参考以下文章