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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:05:35+00:00 2026-06-05T05:05:35+00:00

I’ve been given the basic code for an algorithm, which selects the kth smallest

  • 0

I’ve been given the basic code for an algorithm, which selects the kth smallest element in an unsorted array (or sorted, I’m not sure). Usually, we’d use quickselect for this, but we’ve been given another choice which has been labelled ‘countingselect’ as the function name.

“Counting select uses a similar approach to counting sort. Items in the list are used as indexes into an array of counts. Then, starting at the low value end of the array, item counts are accumulated until the total exceeds the desired value.”

// return the kth smallest item
int countingSelect(int items[], int first, int last, int k) {
    int counts[cap];
    for (int c = 0; c < cap; c++) {
        counts[c] = 0;
    }
    for (int i = first; i < last; i++) {
        counts[items[i]] += 1;
    }
    int c = 0;
    while (k >= 0) {
        k -= counts[c++];
    }
    return c-1;
}

I’m having enormous trouble breaking this down into pseudo-code, so that I can understand exactly how the function works. With the code we’ve been given, my first confusion is what the value ‘cap’ is, and what it’s function is. What value is cap usually? I haven’t been given this information.

Breaking down the algorithm into pseudo code is a good way to understand it, and I request some aid in breaking it down and stepping through the code.

Thankyou in advance.

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

    If you have understood count sort, this should be quite simple. If not, then let me briefly say how count sort works.

    Say you have 10 numbers, in the range [0, 15]. Since you know the bound of data, you can go over your input, and mark how many times you see each number. Then you iterate over the counts and retrieve the numbers in order:

    input numbers
    counts[0..15] = 0
    for i in numbers
        ++counts[numbers[i]]
    for i in counts
        counts[i] times
            print i
    

    Let’s see an example:

    numbers: 1 5 3 13 5 2 0 1 14 2
    

    The first loop creates the counts:

    (index) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    counts: 1 2 2 1 0 2 0 0 0 0 0  0  0  1  1  0
    

    And the second loop gives you:

    1 times 0
    2 times 1
    2 times 2
    1 time 3
    0 times 4
    2 times 5
    0 times 6
    ...
    

    that is:

    0 1 1 2 2 3 5 5 13 14
    

    Your algorithm is basically the same, except instead of outputting a sorted list, it find the kth smallest item.

    In the example above, you can see that:

    0th smallest numbers is 0
    1st and 2nd smallest numbers are 1
    3rd and 4th smallest numbers are 2
    5th smallest number is 3
    6th and 7th smallest numbers are 5
    ...
    

    If you look closely, you will see this pattern:

    if      k <= 0 => 0
    else if k <= 2 => 1
    else if k <= 4 => 2
    else if k <= 5 => 3
    else if k <= 7 => 5
    else if k <= 8 => 13
    else if k <= 9 => 14
    

    However, the numbers

    0 2 4 5 7 8 9
    

    are in fact the running sum of counts itself!

    So, what the bottom of your algorithm does is a running sum, except it checks when the sum gets bigger than k. When it does, the previous number had been your answer. Note that the indices to counts are the numbers themselves.

    int c = 0;
    int sum = 0;
    while (k >= sum) {
        sum += counts[c++];
    }
    return c-1;
    

    Your algorithm has tried to avoid the sum variable and instead subtracts the running sum from k itself which has the same effect.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

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.