<! DOCTYPEhtml>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Generate 10 random numbers between 20-50 bubble sort array out of order</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); // Generate 10 random numbers between 20 and 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)); // Bubble sort
arr.sort(function(a,b){
return 0.5 - Math.random();
});
console.log(arr); // The array is out of order
</script>
</body>
</html>
Copy the code
Random number does not contain 50, if you want to include 50 can refer to: blog.csdn.net/xutongbao/a…