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

  • Home
  • SEARCH
  • 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 8195727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:14:09+00:00 2026-06-07T05:14:09+00:00

I know that quicksort has O(n log n) average time complexity. A pseudo-quicksort (which

  • 0

I know that quicksort has O(n log n) average time complexity. A pseudo-quicksort (which is only a quicksort when you look at it from far enough away, with a suitably high level of abstraction) that is often used to demonstrate the conciseness of functional languages is as follows (given in Haskell):

quicksort :: Ord a => [a] -> [a]
quicksort []     = []
quicksort (p:xs) = quicksort [y | y<-xs, y<p] ++ [p] ++ quicksort [y | y<-xs, y>=p]

Okay, so I know this thing has problems. The biggest problem with this is that it does not sort in place, which is normally a big advantage of quicksort. Even if that didn’t matter, it would still take longer than a typical quicksort because it has to do two passes of the list when it partitions it, and it does costly append operations to splice it back together afterwards. Further, the choice of the first element as the pivot is not the best choice.

But even considering all of that, isn’t the average time complexity of this quicksort the same as the standard quicksort? Namely, O(n log n)? Because the appends and the partition still have linear time complexity, even if they are inefficient.

  • 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-07T05:14:11+00:00Added an answer on June 7, 2026 at 5:14 am

    This “quicksort” is actually deforested tree sort:
    http://www.reddit.com/r/programming/comments/2h0j2/real_quicksort_in_haskell

    data Tree a = Leaf | Node a (Tree a) (Tree a)
    
    mkTree [] = Leaf
    mkTree (x:xs) = Node x (mkTree (filter (<= x) xs)) (mkTree (filter (x <) xs))
    

    Binary tree is unbalanced, so O(N^2) worst-case and O(N*Log N) average-case complexity for building search tree.

    foldTree f g Leaf = g
    foldTree f g (Node x l r) = f x (foldTree f g l) (foldTree f g r)
    
    treeSort l = foldTree (\x lft rht -> lft++[x]++rht) [] (mkTree l)
    

    Retrieval algorithm have O(N^2) worst-case and O(N*Log N) average-case complexity.

    Well-balanced:

    Prelude> let rnds = iterate step where step x = (75*x) `mod` 65537
    Prelude> length . quicksort . take 4000 . rnds $ 1
    4000
    (0.08 secs, 10859016 bytes)
    Prelude> length . quicksort . take 8000 . rnds $ 1
    8000
    (0.12 secs, 21183208 bytes)
    Prelude> length . quicksort . take 16000 . rnds $ 1
    16000
    (0.25 secs, 42322744 bytes)
    

    Not-so-well-balanced:

    Prelude> length . quicksort . map (`mod` 10) $ [1..4000]
    4000
    (0.62 secs, 65024528 bytes)
    Prelude> length . quicksort . map (`mod` 10) $ [1..8000]
    8000
    (2.45 secs, 241906856 bytes)
    Prelude> length . quicksort . map (`mod` 10) $ [1..16000]
    16000
    (9.52 secs, 941667704 bytes)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that Phonegap has an event for back button, but it's only available
I know that this sort of question has been asked here before, but still
I know that immutable objects always have the same state, the state in which
I know that many threads has been created here & on the internet about
I know that quicksort is not stable method, namely for equal elements, maybe member
We know that IPhone has a push notification function. But I have different kinds
I know that I can do something like $int = (int)99; //(int) has a
I know that a fragment's view hierarchy has to be inflated in onCreateView, but
I know that there has been one question about this but it is not
I know that if port 443 is open that means the remote host supports

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.