in reply to Re^2: Stuck with manipulating an array
in thread Stuck with manipulating an array

My approach to binning would be simple. You look at the first element of the array @split_entries and the index of the potential candidates, and increase that index until the potential candidate is larger than your distance. All elements between the first element and the index of the potential candidate then belong into one bin.

An example, for a distance of 5:

11 12 16 17 22 30

First you look at the first position in your array (11). The next candidate is at the second position, and its value is 12. abs(12-11) < 5, so you increase the index of your candidate. The next candidate is at the third position, and its value is 16. abs(16-11) >= 5, so your first bin are the first and second entries in the array, 11 and 12.

Now, you start the same thing over, as there are still elements in your array after removing 11 and 12 from it.

You look at the first position in your array (16). The next candidate is at the second position, and its value is 17. abs(16-17) < 5, so you increase the index of your candidate. The next candidate is at the third position, and its value is 22. abs(22-16) >= 5, so your first bin are the first and second entries in the array, 16 and 17.

... and so on.

Replies are listed 'Best First'.
Re^4: Stuck with manipulating an array
by Anonymous Monk on Aug 28, 2017 at 13:31 UTC
    Fair enough, but what kind of data structures will I need? This I cannot seem to figure out...

      With the approach I outlined, you won't need any additional data structures beyond what you already have. You will be modifying your current list of items as you output bins though, as I already described.

      If you want to keep your unbinned array, make a copy before binning or start with the last item considered as candidate instead of the first position in the array instead.

      I highly recommend working through any algorithm on paper until you feel confident with how it works and what kind of data it accesses.