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 8880829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:13:55+00:00 2026-06-14T20:13:55+00:00

I am reading a quick sort implementation using a stack at the following link.

  • 0

I am reading a quick sort implementation using a stack at the following link.

link

My question is regarding the following paragraph.

The policy of putting the larger of the small subfiles on the stack
ensures that each entry on the stack is no more than one-half of the
size of the one below it, so that the stack needs to contain room for
only about lg N entries. This maximum stack usage occurs when the
partition always falls at the center of the file. For random files,
the actual maximum stack size is much lower; for degenerate files it
is likely to be small.

This technique does not necessarily work in a truly recursive
implementation, because it depends on end- or tail-recursion removal.
If the last action of a procedure is to call another procedure, some
programming environments will arrange things such that local variables
are cleared from the stack before, rather than after, the call.
Without end-recursion removal, we cannot guarantee that the stack size
will be small for quicksort.

  1. What does the author mean by “that each entry on the stack is no more than one-half of the size of the one below it”? Could you please give an example of this.

  2. How did the author came to the conclusion that the stack needs space for only about lg N entries?

  3. What does authore mean by “Without end-recursion removal, we cannot guarantee that the stack size will be small for quicksort” ?

Thanks for your time and help.

  • 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-14T20:13:56+00:00Added an answer on June 14, 2026 at 8:13 pm

    The policy of putting the larger of the small subfiles on the stack ensures that each entry on the stack is no more than one-half of the size of the one below it,

    That is not quite true. Consider you want to sort a 100-element array, and the first pivot goes right in the middle. Then you have a stack

    49
    50
    

    then you pop the 49-element part off the stack, partition, and push the two parts on the stack. Let’s say the choice of pivot was not quite as good this time, there were 20 elements not larger than the pivot. Then you’d get the stack

    20
    28
    50
    

    and each stack entry is more than half of the one below.

    But that cannot continue forever, and we have

    During the entire sorting, if stack level k is occupied, its size is at most total_size / (2^k).

    That is obviously true when the sorting begins, since then there is only one element on the stack, at level 0, which is the entire array of size total_size.

    Now, assume the stated property holds on entering the loop (while(!stack.empty())).

    A subarray of length s is popped from stack level m. If s <= 1, nothing else is done before the next loop iteration, and the invariant continues to hold. Otherwise, if s >= 2, After partitioning that, there are two new subarrays to be pushed on the stack, with s-1 elements together. The smaller of those two then has a size smaller_size <= (s-1)/2, and the larger has a size larger_size <= s-1. Stack level m will be occupied by the larger of the two, and we have

    larger_size  <= s-1 < s <= total_size / (2^m)
    smaller_size <= (s-1)/2 < s/2 <= total_size / (2^(m+1))
    

    for the stack levels m resp. m+1 at the end of the loop body. The invariant holds for the next iteration.

    Since at most one subarray of size 0 is ever on the stack (it is then immediately popped off in the next iteration), there are never more than lg total_size + 1 stack levels occupied.

    Regarding

    What does author mean by “Without end-recursion removal, we cannot guarantee that the stack size will be small for quicksort” ?

    In a recursive implementation, you can have deep recursion, and when the stack frame is not reused for the end-call, you may need linear stack space. Consider a stupid pivot selection, always choosing the first element as pivot, and an already sorted array.

    [0,1,2,3,4]
    

    partition, pivot goes in position 0, the smaller subarray is empty. The recursive call for the larger subarray [1,2,3,4], allocates a new stack frame (so there are now two stack frames). Same principle, the next recursive call with the subarray [2,3,4] allocates a third stack frame, etc.

    If one has end-recursion removal, i.e. the stack frame is reused, one has the same guarantees as with the manual stack above.

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

Sidebar

Related Questions

Quick question regarding the OAuth2 Spec, in particular section 5.1.5 . Reading that spec,
I've been reading through a nice tutorial and have a quick question concerning something
Quick question for an ASP experts out there! I am reading a SQL database
Quick question, I've been reading about some .Net stuff and the way some people
I am reading the Netbeans Platform Quick Start tutorial ( http://platform.netbeans.org/tutorials/nbm-quick-start.html ), and I
Running a quick experiment related to Is double Multiplication Broken in .NET? and reading
Quick, and perhaps silly question, but here it is nevertheless. Writing to a register
When reading some JSON data structures, and then trying to Dump them using YAML::Tiny
OK, this is just a quick question and I might take some slack for
Is anyone aware of a quick way of reading in an xml file over

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.