Link to home
Start Free TrialLog in
Avatar of akohan
akohan

asked on

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.

Avatar of phoffric
phoffric

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


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)
ASKER CERTIFIED SOLUTION
Avatar of Member_2_4694817
Member_2_4694817

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of akohan

ASKER


Thank you!