javascript 冒泡排序算法
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>javascript 冒泡排序算法</title>
</head>
<body>
<script type="text/javascript">
function bubbleArr(arr) {
var i, j, len = arr.length;
for(i = 0; i < len - 1; i++) {
for(j = 0; j < len - 1 - i; j++) {
if(arr[j] > arr[j + 1]) {
var temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
var arr = [11, 2, 34, 34, 2, 546, 4];
bubbleArr(arr);
console.log(arr)
</script>
</body>
</html>
作者:孟繁贵 Email:meng010387@126.com 期待共同进步!