# sort排序法（最简单）

arr.sort(compareFunction)

let arr = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];

arr.sort(() => Math.random() - 0.5);
//[2, 8, 3, 15, 6, 5, 14, 1, 9, 0, 11, 10, 16, 12, 13, 4, 7]
//[13, 12, 8, 2, 4, 11, 5, 0, 6, 10, 1, 15, 9, 7, 14, 3, 16]
//[8, 3, 13, 12, 4, 6, 7, 9, 11, 10, 16, 5, 2, 15, 1, 14, 0]
//[12, 0, 2, 7, 11, 10, 1, 16, 5, 6, 9, 3, 14, 8, 13, 4, 15]


let arr = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];

function shuffle(arr) {
let i = arr.length;
while (i) {
let j = Math.floor(Math.random() * i--);
[arr[j], arr[i]] = [arr[i], arr[j]];
}
return arr;
}
shuffle(arr);
//[5, 14, 15, 8, 12, 13, 16, 2, 4, 7, 10, 6, 1, 11, 0, 9, 3]
//[0, 13, 3, 15, 4, 9, 14, 1, 7, 10, 2, 5, 11, 8, 12, 16, 6]
//[9, 13, 7, 11, 15, 5, 1, 6, 3, 16, 0, 10, 2, 12, 4, 8, 14]
//[12, 11, 3, 16, 1, 2, 7, 10, 9, 14, 6, 5, 4, 8, 15, 0, 13]


# 循环随机位交换法（最容易理解）

let arr = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];

for(let i = 0,len = arr.length; i < len; i++){
let currentRandom = parseInt(Math.random() * (len - 1));
let current = arr[i];
arr[i] = arr[currentRandom];
arr[currentRandom] = current;
}
//[15, 3, 1, 9, 13, 8, 10, 5, 7, 11, 12, 0, 16, 4, 6, 2, 14]
//[15, 6, 3, 7, 8, 5, 9, 0, 13, 4, 12, 1, 11, 16, 14, 10, 2]
//[1, 16, 10, 14, 13, 4, 6, 12, 5, 2, 9, 3, 8, 11, 15, 0, 7]
//[2, 9, 10, 13, 12, 15, 16, 8, 1, 11, 14, 0, 3, 5, 4, 7, 6]


# 循环随机位法（循环次数最多）

let arr = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],newArr = [];

for(let i = 0,len = arr.length; i < len;){
i++;
let currentRandom = parseInt(Math.random() * len);
if (!newArr.includes(arr[currentRandom])){
newArr.push(arr[currentRandom]);
}else{
i--;
}
}
//[10, 15, 2, 12, 13, 7, 4, 14, 1, 11, 6, 0, 8, 16, 9, 5, 3]
//[14, 9, 13, 10, 11, 6, 15, 16, 0, 12, 3, 5, 8, 4, 1, 7, 2]
//[4, 13, 11, 7, 8, 3, 16, 9, 14, 5, 2, 10, 12, 6, 15, 0, 1]
//[2, 14, 16, 4, 9, 13, 6, 12, 0, 5, 8, 15, 11, 1, 3, 10, 7]


03-16 1835

10-09 7789

01-27 2147

01-29 1073

06-21 2672

02-24 924

05-03 5216

10-07 3463

05-29 1276

01-05 5481

#### N皇后问题 - 使用随机爬山法实现其快速解法

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。