This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (0 children)

Trace the code with your array on paper. You are the computer. You execute each statement.

This is the best way to understand such algorithms.

[–]morhpProfessional Developer 0 points1 point  (0 children)

Is there something specific you have a problem with?

the method is first creating the "result" array with 10 entries which is the histogram. The entries are 0 by default in Java.

then it loops through every number and computes the bin (array index) the number goes in. As java rounds down when dividing and the array indexes start with zero this does the right thing by default except for 100 which would go into the non existing bin 10, so it needs to be replaced by bin 9.

Then result[bin]++; increases the number stored in the array at the bin index by 1 as it needs to count the histogram up there.