Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7731635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:31:52+00:00 2026-06-01T06:31:52+00:00

Is there any function in haskell libraries that sorts integers in O(n) time?? [By,

  • 0

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]

Basically I find that the following code takes a lot of time with the sort (as compared to summing the list without sorting) :

import System.Random
import Control.DeepSeq
import Data.List (sort)

genlist gen = id $!! sort $!! take (2^22) ((randoms gen)::[Int])

main = do
    gen <- newStdGen
    putStrLn $ show $ sum $ genlist gen

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.

Time : 6 seconds (without sort); about 35 seconds (with sort)

Memory : about 80 MB (without sort); about 310 MB (with sort)

Note 1 : 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)

I am assuming faster algorithms will provide bettor memory print too, hence looking for O(n) time.

Note 2 : I am looking for fast algorithms for Int64, though fast algorithms for other specific types will also be helpful.


Solution Used : IntroSort with unboxed vectors was good enough for my task:

import qualified Data.Vector.Unboxed as V
import qualified Data.Vector.Algorithms.Intro as I

sort :: [Int] -> [Int]
sort = V.toList . V.modify I.sort . V.fromList
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:31:54+00:00Added an answer on June 1, 2026 at 6:31 am

    The idea to sort the numbers using an array is the right one for reducing the memory usage.

    However, using the maximum and minimum of the list as bounds may cause exceeding memory usage or even a runtime failure when maximum xs - minimum xs > (maxBound :: Int).

    So I suggest writing the list contents to an unboxed mutable array, sorting that inplace (e.g. with quicksort), and then building a list from that again.

    import System.Random
    import Control.DeepSeq
    import Data.Array.Base (unsafeRead, unsafeWrite)
    import Data.Array.ST
    import Control.Monad.ST
    
    myqsort :: STUArray s Int Int -> Int -> Int -> ST s ()
    myqsort a lo hi
       | lo < hi   = do
           let lscan p h i
                   | i < h = do
                       v <- unsafeRead a i
                       if p < v then return i else lscan p h (i+1)
                   | otherwise = return i
               rscan p l i
                   | l < i = do
                       v <- unsafeRead a i
                       if v < p then return i else rscan p l (i-1)
                   | otherwise = return i
               swap i j = do
                   v <- unsafeRead a i
                   unsafeRead a j >>= unsafeWrite a i
                   unsafeWrite a j v
               sloop p l h
                   | l < h = do
                       l1 <- lscan p h l
                       h1 <- rscan p l1 h
                       if (l1 < h1) then (swap l1 h1 >> sloop p l1 h1) else return l1
                   | otherwise = return l
           piv <- unsafeRead a hi
           i <- sloop piv lo hi
           swap i hi
           myqsort a lo (i-1)
           myqsort a (i+1) hi
       | otherwise = return ()
    
    
    genlist gen = runST $ do
        arr <- newListArray (0,2^22-1) $ take (2^22) (randoms gen)
        myqsort arr 0 (2^22-1)
        let collect acc 0 = do
                v <- unsafeRead arr 0
                return (v:acc)
            collect acc i = do
                v <- unsafeRead arr i
                collect (v:acc) (i-1)
        collect [] (2^22-1)
    
    main = do
        gen <- newStdGen
        putStrLn $ show $ sum $ genlist gen
    

    is reasonably fast and uses less memory. It still uses a lot of memory for the list, 222 Ints take 32MB storage raw (with 64-bit Ints), with the list overhead of iirc five words per element, that adds up to ~200MB, but less than half of the original.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any function that converts an escaped Url string to its unescaped form?
is there any function in C++ that calculates a fingerprint or hash of a
As title; is there any function that can achieve this?
Is there any way to compare two functions in Haskell? My thought is that
Is there any function like php's mb_convert_encoding which can convert an encoding to another?
Is there any function in c# equivalent to C's stdio function ungetc() ?
Is there any function available in PHP to check whether an array is empty
Is there any function in C on linux by which we can query MX
Is there any function in PHP to make the first character of the word
Is there any difference between a variable declared as static outside any function between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.