Note that there are some explanatory texts on larger screens.

plurals
  1. POsorting integers fast in haskell
    primarykey
    data
    text
    <p>Is there any function in haskell libraries that sorts integers in O(n) time?? [By, O(n) I mean faster than comparison sort and specific for integers]</p> <p>Basically I find that the following code takes a lot of time with the sort (as compared to summing the list without sorting) :</p> <pre><code>import System.Random import Control.DeepSeq import Data.List (sort) genlist gen = id $!! sort $!! take (2^22) ((randoms gen)::[Int]) main = do gen &lt;- newStdGen putStrLn $ show $ sum $ genlist gen </code></pre> <p>Summing a list doesn't require deepseq but what I am trying for does, but the above code is good enough for the pointers I am seeking. </p> <p>Time : 6 seconds (without sort); about 35 seconds (with sort)</p> <p>Memory : about 80 MB (without sort); about 310 MB (with sort)</p> <p><strong>Note 1 :</strong> memory is a bigger issue than time for me here as for the task at hand I am getting out of memory errors (memory usage becomes 3GB! after 30 minutes of run-time)</p> <p>I am assuming faster algorithms will provide bettor memory print too, hence looking for O(n) time.</p> <p><strong>Note 2 :</strong> I am looking for fast algorithms for Int64, though fast algorithms for other specific types will also be helpful.</p> <hr> <p><strong>Solution Used :</strong> IntroSort with unboxed vectors was good enough for my task:</p> <pre><code>import qualified Data.Vector.Unboxed as V import qualified Data.Vector.Algorithms.Intro as I sort :: [Int] -&gt; [Int] sort = V.toList . V.modify I.sort . V.fromList </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload