# Bin Packing

## Best Fit Algorithm

And now we come to our last algorithm, the Best Fit Algorithm.  The only difference between this and the Worst Fit Algorithm is that instead of picking a Bin with the most amount of free space, this algorithm picks the Bin with the least amount of free space in it that can still hold the current Element.  The results you obtain by using this algorithm are not always the same as the Worst Fit, sometimes it is slightly better, other times it is not.  It depends on the nature of the data supplied.

`   Private Sub BestFit()      'Checks to make sure everything is initialized      If Elements Is Nothing Then Exit Sub      Dim ElementsCopy(Elements.GetUpperBound(0)) As Integer      ReDim Bins(0)      'Bin Number we are on, Bin Element we are on, Amount placed in the current Bin      Dim BinNumber, BinElement, BinCount As Integer      Dim BestBin, BestBinAmount As Integer      Dim i, j, k As Integer      'Make a copy of the array incase we need to sort it      DeepCopyArray(Elements, ElementsCopy)      'Sort in descending order if needed      If Me.Decreasing = True Then         Array.Sort(ElementsCopy)         Array.Reverse(ElementsCopy)      End If      'Declare the first element in the first Bin      ReDim Bins(0)(0)      'Loop through each Element and place in a Bin      For i = 0 To ElementsCopy.GetUpperBound(0)         BestBin = -1         BestBinAmount = -1         For j = 0 To BinNumber            BinElement = Bins(j).GetUpperBound(0)            'Count the amount placed in this Bin            BinCount = 0            For k = 0 To BinElement               BinCount += Bins(j)(k)            Next            'Find the most full Bin that can hold this Element            If BestBinAmount < BinCount AndAlso BinCount + ElementsCopy(i) <= Me.BinHeight Then               BestBinAmount = BinCount               BestBin = j            End If         Next         If BestBin = -1 Then            'There wasn't room for the Element in any existing Bin            'Create a new Bin            ReDim Preserve Bins(BinNumber + 1)            BinNumber += 1            'Initialize first element of new bin            ReDim Bins(BinNumber)(1)            BinElement = 0            Bins(BinNumber)(BinElement) = ElementsCopy(i)         Else            'There's room for this Element in an existing Bin            'Place Element in "Best Bin"            BinElement = Bins(BestBin).GetUpperBound(0)            ReDim Preserve Bins(BestBin)(BinElement + 1)            Bins(BestBin)(BinElement) = ElementsCopy(i)         End If      Next      'All Elements have been place, now we go back and remove unused Elements      For i = 0 To BinNumber         For j = 0 To Bins(i).GetUpperBound(0)            If Bins(i)(j) = 0 Then               ReDim Preserve Bins(i)(j - 1)            End If         Next      Next      GC.Collect()   End Sub`

With the same set of data as the last example, this algorithm will lay out something like this in memory:

## You might also like...

### Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

### Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.”