Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 1.24 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <script>
  6.         function sleepSort(array, callback, f){
  7.             var l = array.length, result = [];
  8.             var i = l;
  9.  
  10.             if(f == null)
  11.                 f = Number;
  12.  
  13.             while(i--) each(array[i]);
  14.  
  15.             function each(value){
  16.                 setTimeout(function(){
  17.                     result.push(value);
  18.                     if(--l === 0)
  19.                         callback(result);
  20.                 }, f(value));
  21.             }
  22.         }
  23.  
  24.         window.onload = function(){
  25.             var array = [], i = 100;
  26.  
  27.             while(i--)
  28.                 array.push(randint());
  29.  
  30.             Q('#array').textContent = JSON.stringify(array, null, 4);
  31.             sleepSort(array, function(result){
  32.                 Q('#result').textContent = JSON.stringify(result, null, 4);
  33.             });
  34.         };
  35.  
  36.         function randint(){ return Math.random() * 256 | 0; }
  37.         function Q(selector){ return document.querySelector(selector); }
  38.     </script>
  39.     <style>
  40.         div { margin: 1em 0 }
  41.         #array::before { content: 'before :' }
  42.         #result::before { content: 'after :' }
  43.     </style>
  44. </head>
  45. <body>
  46.     <div id="array"></div>
  47.     <div id="result"></div>
  48. </body>
  49. </html>