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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:16:07+00:00 2026-05-30T20:16:07+00:00

I’m currently learning algorithms in my spare time but have the following question while

  • 0

I’m currently learning algorithms in my spare time but have the following question while studying chapter 3 select() algorithms.

I understand that I can use the select() algorithm to find the median number (n/2 th smallest number) if I was using a array from A to n numbers.

1) but this is the bit I’m struggling to understand. A = [3, 7, 5, 1, 4, 2, 6, 2]. suppose that is the array. what is contents of the array after each call to Partition(), and the parameters in each recursive call of Select().

can some one explain how they are working this out please?

below is the pseudo-code for the 2 algorithms.

Select(A, p, r, k) {
    /* return k-th smallest number in A[p..r] */
    if (p==r) return A[p] /* base case */
    q := Partition(A,p,r)
    len := q – p + 1
    if (k == len) return A[q]
    else if (k<len) return Select(A,p,q-1,k)
    else return Select(A,q+1,r,k-len)
}

and the second code is

Partition(A, p, r) { /* partition A[p..r] */
    x := A[r] /* pivot */
    i := p-1
    for j := p to r-1 {
        if (A[j] <= x) {
            i++
            swap(A[i], A[j])
        }
    }
    swap(A[i+1], A[r])
    return i+1
}

The book I am using is called The Derivation of Algorithms by Anne Kaldewaij.

  • 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-05-30T20:16:08+00:00Added an answer on May 30, 2026 at 8:16 pm

    This algorithm works in two steps. The partitioning step works by picking some pivot element, then rearranging the elements of the array such that everything less than the pivot is to one side, everything greater than the pivot is to the other side, and the pivot is in the correct place. For example, given the array

    3  2  5  1  4
    

    If we pick a pivot of 3, then we might partition the array like this:

    2  1  3  5  4
    +--+  ^  +--+
     ^    |    ^
     |    |    +--- Elements greater than 3
     |    +-------- 3, in the right place
     +------------- Elements less than 3
    

    Notice that we haven’t sorted the array; we’ve just made it closer to being sorted. This is, incidentally, the first step in quicksort.

    The algorithm then uses the following logic. Suppose that we want to find the element that belongs at index k in sorted order (the kth smallest element). Then, in relation to the pivot we picked, there are three options:

    1. The pivot is at position k. Then, since the pivot is in the right place, the value we’re looking for must be the pivot. We’re done.
    2. The pivot is at position greater than k. Then the kth smallest element must be in the portion of the array before the pivot, so we can recursively search that portion of the array for the kth smallest element.
    3. The pivot is at position smaller than k. Then the kth smallest element must be somewhere in the upper region of the array, and we can recurse there.

    In our case, suppose that we want the second-smallest element (the one at position 2). Since the pivot ended up at position 3, this means that the second-smallest element must be somewhere in the first half of the array, so we would recurse on the subarray

    2  1
    

    If we wanted the actual median element, since the pivot ended up smack in the middle of the array, we would just output that the median is 3 and be done.

    Finally, if we wanted something like the fourth-smallest element, then since the pivot is before position 4, we would recurse on the upper half of the array, namely

    5  4
    

    and would look for the first smallest element here, since there are three elements before this region.

    The rest of the algorithm are the details of how to do the partitioning step (which is probably the most involved part of the algorithm) and how to do the three-way choice about whether to recurse or not (a bit less difficult). Hopefully, though, this high-level structure helps the algorithm make more sense.

    Hope this helps!

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I want use html5's new tag to play a wav file (currently only supported

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.