Question about sorting number and placing them in buckets

Posted on 2011-10-05
Last Modified: 2013-12-16
Hello group,

Let's say we have an array of number (not sorted) and we need to place them in different buckets such that

<            5
5      to      10
10      to      15
15      to      20
20      to      25
>            25

what is the best solution? I'm think of hash tables.  Can some experts give me some advice on this?


Question by:akohan
    LVL 31

    Expert Comment

    B0:    <   5
    B1:    5      to      10
    B2:    10      to      15
    B3:    15      to      20
    B4:    20      to      25
    B5:    >            25

    Notice that
    floor( 2 / 5)  = 0, so put  2  into B0
    floor( 5 / 5)  = 1, so put  5  into B1
    floor( 6 / 5)  = 1, so put  6  into B1
    floor(18 / 5) = 3, so put 18 into B3

    floor : Returns the largest integer less than or equal to the specified decimal number.

    Not sure is this is what you had in mind. I'll be back tomorrow.
    LVL 10

    Expert Comment

    yes you can use Hash table
    Hashtable hashtable = new Hashtable();

    LVL 20

    Expert Comment

    It depends on what you want to do with the numbers once they are in the buckets.
    It may just as easily be the case that your single buckets are normal arrays.
    Of course that means that your collectoin of buckets is possibly best implemented as an array-valued associative array (which is internally implemented using hash tables, but shoul dnot worry you)
    LVL 20

    Accepted Solution

    To illustrate my previous pos, cf. the following code. (Sorry I'm a C++ / STL guy, but this should be easily transferred to C#)
    vector <double> input;
    typedef tBucketIdentifier int;
    tBucketIdentifier NumberToBucketId(double x) {
       if (x<0) 
          return -1; 
       else if (x>25)
          return 5;
          return (int) (x/5);
    map <tBucketIdentifier, vector <double> > output;
    for (vector <double>::iterator i = input.begin(); i!=input.end(); i++)
       output[ NumberToBucketId( *i ) ].push_back( *i );

    Open in new window

    Another approach would be:
    Simply sort the input array and determine the indices where the various bucjets begin and end.

    Author Comment


    Thank you!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now