<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>生成10个20-50之间的随机数 冒泡排序 数组乱序</title>
<style type="text/css">
</style>
</head>
<body>
<script type="text/javascript">
var arr = [];
for (var i = 0; i<10; i++) {
var num = Math.random() * 30 + 20;
num = parseInt(num, 10);
arr.push(num);
}
console.log(arr); //生成10个20-50之间的随机数
function bubbleSort(arr) {
var len = arr.length;
for (var i = 0; i < len; i++) {
for (var j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j+1]) {
var temp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
console.log(bubbleSort(arr)); //冒泡排序
arr.sort(function(a,b){
return 0.5 - Math.random();
});
console.log(arr); //数组乱序
</script>
</body>
</html>