User Tools

Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
algorithm:fisher-yates_shuffle [2013/04/20 22:38]
will
algorithm:fisher-yates_shuffle [2015/02/02 08:28] (current)
Line 1: Line 1:
-[algorithm ​Fisher-Yates shuffle]+======= λ Fisher-Yates shuffle ​======
  
-====== Algorithm ======+<​nowiki>​ 
 +======= Algorithm ​=======
 <syntax js> <syntax js>
 function shuffle(a) { function shuffle(a) {
     for (var i=a.length-1;​ i>0; i--) {     for (var i=a.length-1;​ i>0; i--) {
-        var j = Math.floor(Math.random()*(i+1)); ​// j random integer [0-i]+        ​// j is a random integer [0-i] 
 +        ​var j = Math.floor(Math.random()*(i+1));​
         swap(a, j, i);         swap(a, j, i);
     }     }
Line 12: Line 14:
 </​syntax>​ </​syntax>​
  
-====== Support ======+======= Support ​=======
 <syntax js> <syntax js>
 function swap(a, i, j) { function swap(a, i, j) {
Line 22: Line 24:
 </​syntax>​ </​syntax>​
  
-====== Visualisation ======+======= Tests ======= 
 +<syntax js> 
 +</​syntax>​ 
 + 
 +======= Options ======= 
 +<syntax js> 
 +
 +    "​title":"​Fisher-Yates shuffle",​ 
 +    "​height":"​270px"​ 
 +
 +</​syntax>​ 
 + 
 +======= Visualisation ​=======
 <syntax html> <syntax html>
 <​html>​ <​html>​
Line 64: Line 78:
 } }
  
-function args() { +function args(forCloning) { 
-    return a;+    ​if (forCloning) { 
 +        var a2 = []; 
 +        for (var i=0, c=a.length; i<c; i++) { 
 +            a2.push(a[i].data);​ 
 +        } 
 +        return a2; 
 +    } 
 +    else { 
 +        ​return a; 
 +    }
 } }
  
-function update(n, x, duration) {+function update(n, x, isRunning, duration) {
     if (duration < 0) {     if (duration < 0) {
         return; // State-less visualisation.         return; // State-less visualisation.
Line 79: Line 102:
     }     }
 } }
- 
-var oldI; 
  
 function drawGraph(a,​ idx, idx2, duration) { function drawGraph(a,​ idx, idx2, duration) {
Line 96: Line 117:
     bars.transition().duration(duration).attr("​x",​ function(d, i) { return barSpacing*i;​ });     bars.transition().duration(duration).attr("​x",​ function(d, i) { return barSpacing*i;​ });
     ​     ​
-    bars.classed("​index2", function(d, i) {return i===idx2 && i!==oldI});​ +    bars.classed("​index", function(d, i) {return i===idx ​|| i===idx2});
-    if (idx!==oldI) { +
-        bars.classed("​index",​ function(d, i) {return ​i===idx}); +
-        oldI = idx; +
-    }+
 } }
 </​script>​ </​script>​
 </​head>​ </​head>​
-<​body ​style="​height:​270px"​>+<​body>​
 <div id="​chart"></​div>​ <div id="​chart"></​div>​
 </​body>​ </​body>​
 </​html>​ </​html>​
 </​syntax>​ </​syntax>​
 +</​nowiki>​
algorithm/fisher-yates_shuffle.1366522716.txt.gz · Last modified: 2015/02/02 08:23 (external edit)