You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionshuffle(array){varm=array.length,t,i;// While there remain elements to shuffle…while(m){// Pick a remaining element…i=Math.floor(Math.random()*m--);// And swap it with the current element.t=array[m];// 缓存当前的第 m 位array[m]=array[i];// 随机 m 之前某位放进 marray[i]=t;// 第随机位换成 m 位的值(后续还会被取到)}returnarray;}
感慨,算法总是逼死我们这些文科生。
The text was updated successfully, but these errors were encountered:
昨天写了前天的任务「如何分组」。大家都会需要用到「洗牌」,而关于如何洗有多种算法,其中有一个简洁高效的算法就是 Fisher-Yates Shuffle https://bost.ocks.org/mike/shuffle/ ( 文章和演示都非常棒 )。
在「如何分组」里我写了一个思路,里面有一种重要的点是不断从原来的数组踢除数值,这会导致数组本身重排,而这是 Fisher-Yates 要避免的问题,他采用的是交换的方法来避免重排,如下中文注释。
感慨,算法总是逼死我们这些文科生。
The text was updated successfully, but these errors were encountered: