# Question about sorting number and placing them in buckets

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?

Thanks.

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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.
0
Commented:
yes you can use Hash table
Hashtable hashtable = new Hashtable();

0
Commented:
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)
0
Commented:
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;
else
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 );
``````

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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:

Thank you!
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.