New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[js] 第24天 如何快速让一个数组乱序,写出来 #84
Comments
直接使用原生的
|
使用array.sort()进行乱序存在一定问题,增大样本进行实验之后可以发现这种乱序方案并不是完全随机的(所有元素会大概率停留在自己的初始位置)(v8处理排序是小于10个是插入排序,大于10个是快排,排序算法复杂度介于O(n)与O(n2)之间,也就是存在两个元素都没有比较的机会,因此不是完全随机),这里可以使用Fisher–Yates shuffle(洗牌算法) |
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; console.log(arr); |
arr.sort(function(a,b){ return Math.random()>.5 ? -1 : 1;}); |
let arr= [1,2,3,4,5,6,7,8,9,10]; arr.map((item,index)=>{ let random =Math.floor(Math.random() * arr.length); [arr[index],arr[random]] = [arr[random],arr[index]]; }); console.log(arr); |
为什么楼上要用三元运算符,直接这样不就完事了。。 arr.sort((a, b) => Math.random() - .5) 不过我们team随机算法也用的洗牌算法,思路就是从后往前遍历,然后随机(0, i+1),交换 function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1))
[array[i], array[j]] = [array[j], array[i]]
}
} |
function mixi(arr){
arr.sort(function(a,b){
(Math,random()>0.5)?a-b>0:a-b<0
})
} |
const swap = (arr, index1, index2) => {
;[arr[index1], arr[index2]] = [arr[index2], arr[index1]]
}
export default function shuffle(arr = []) {
if (!Array.isArray(arr)) return
let index = arr.length - 1
for (; index > 0; index++) {
const randomIndex = Math.floor(Math.random() * (index + 1))
swap(arr, index, randomIndex)
}
} |
const shuffle = arr => arr.sort(() => Math.random() - 0.5); |
//洗牌数组 //随机取0-1中间值(包括0和1) |
大神,想问下,arr.sort(function(a,b){return Math.random() > 0.5 ? 1 : -1}),这种呢随机不是和Math.random的值密切相关吗,和sort有关系么?我的理解是sort是按照Math.random来排序,Math.random是完全随机的吧,那么这个整体应该也是完全随机的吧 |
|
|
function messArr(arr) {
let newIndex = [];
let newArr = [];
while (newIndex.length < arr.length) {
let num = Math.floor(Math.random() * arr.length);
if (!newIndex.includes(num)) {
newIndex.push(num);
}
}
for (let index in newIndex) {
newArr.push(arr[newIndex[index]]);
}
return newArr
}
var arr = ['广西', '上海', '北京', '云南'];
console.log(messArr(arr)); |
function shuffle(array) {
let _array = array.map(v => v)
return array.reduce((acc, cur, curIndex, arr) => {
// 取 [0, 数组长度 - 1] 的随机数
const randomVal = Math.floor(Math.random() * _array.length)
acc.push(
_array[randomVal]
)
// 删除对应的数
_array.splice(randomVal, 1)
return acc
}, [])
}
console.log(randomArray([0,1,2,3,4,5])) |
/**
* @param {number[]} arr
* @return {number[]}
*/
function shuffle(arr) {
arr.sort(() => Math.random() - 0.5);
} |
shuffle的思想· const randomSort = (arr=[]) => {
const length = arr.length;
for(let index = 0; index < length; index++) {
let randomIndex = Math.floor(Math.random()*(length - index)) + index;
[arr[index], arr[randomIndex]] = [arr[randomIndex], arr[index]]
}
return arr;
} |
照着上面的大佬敲了一遍,感觉randomIndex这里一直用数组的长度-1会不会好一点啊 |
function fn(arr) {
var newarr = arr.concat();
for (var i = 0; i < newarr.length; i++) {
var index = Math.floor(Math.random() * newarr.length);
var temp = newarr[i];
newarr[i] = newarr[index];
newarr[index] = temp;
}
return newarr;
} |
|
function outOfOrder(arr) { console.log(outOfOrder([1,2,3,4,5])) |
/**
console.log( randomSortArr([1,2,3,4,5,6,7,8,9,10,11,12,13,34]) ) |
function shuffle(arr){
return arr.sort(() => {
return Math.random() - 0.5
})
}
function shuffle2(arr){
const a = [...arr]
const res = []
while (a.length) {
const idx = Math.floor(Math.random() * a.length)
res.push(...a.splice(idx, 1))
}
return res
}
const arr = [1,2,3,4,5,6,7,8]
console.log(shuffle(arr))
console.log(shuffle2(arr))
// [ 7, 2, 1, 5, 6, 4, 3, 8 ]
// [ 2, 8, 1, 5, 3, 6, 4, 7 ] |
function rd(arrayLength) { const result=[];
if(pArray[k] !==null){ |
sort()// ES5
function randArr(arr) {
return arr.sort(function () {
return Math.random() - 0.5;
});
}
console.log(randArr([1, 2, 3, 4, 5]));
// ES6
let arr = [1, 2, 3, 4, 5];
let newArr = (arr) => arr.sort(() => Math.random() - 0.5);
console.log(newArr(arr));
let arr = [1, 2, 3, 4, 5];
let newArr = arr.sort(() => Math.random() - 0.5);
console.log(newArr); |
const shuffle = arr => arr.sort(() => Math.random() - 0.5); |
var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
let arr6 = [1,2,3,4,5,6,7,8,9] |
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let newArr = []
for (i = 0; i < arr.length; i < 10) {
let index = Math.floor(Math.random() * arr.length)
let item = arr.splice(index, 1)[0];
newArr.push(item);
}
console.log(newArr) |
洗牌算法(shuffle):假设数组长度为n,生成0 ~ n-1之间的随机数random,然后将第random个元素与最后一个元素交换位置,继续生成 0 ~ n-2 之间的随机数random,将第random个元素与数组倒数第二个元素交换位置 function shuffle(arr){ |
在JavaScript中,可以使用洗牌算法(Fisher-Yates算法)来快速将一个数组乱序。以下是一个实现洗牌算法的示例:
在上面的示例中, 请注意,这个算法会修改原始数组,如果不想修改原始数组,可以在函数内部创建一个新的数组并进行操作。 |
第24天 如何快速让一个数组乱序,写出来
The text was updated successfully, but these errors were encountered: