Solved

# Question about sorting number and placing them in buckets

Posted on 2011-10-05
Medium Priority
228 Views
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.

0
Question by:akohan

LVL 32

Expert Comment

ID: 36915657
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

LVL 10

Expert Comment

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

0

LVL 20

Expert Comment

ID: 36938361
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

LVL 20

Accepted Solution

thehagman earned 500 total points
ID: 36938370
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

Author Comment

ID: 36957323

Thank you!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
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!
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
###### Suggested Courses
Course of the Month14 days, 22 hours left to enroll