首页 > 代码库 > 45.@冒泡排序

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>
View Code

 

<!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.@冒泡排序