C++ Program to Implement the Bin Packing Algorithm

This is a C++ Program to implement Bin packing algorithm. This is a sample program to illustrate the Bin-Packing algorithm using next fit heuristics. In the bin packing problem, objects of different volumes must be packed into a finite number of bins or containers each of volume V in a way that minimizes the number of bins used. In computational complexity theory, it is a combinatorial NP-hard problem.

There are many variations of this problem, such as 2D packing, linear packing, packing by weight, packing by cost, and so on. They have many applications, such as filling up containers, loading trucks with weight capacity constraints, creating file backups in media and technology mapping in Field-programmable gate array semiconductor chip design.

The bin packing problem can also be seen as a special case of the cutting stock problem. When the number of bins is restricted to 1 and each item is characterised by both a volume and a value, the problem of maximising the value of items that can fit in the bin is known as the knapsack problem.


Here is source code of the C++ Program to Implement the Bin Packing Algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<iostream>
  3. using namespace std;
  5. void binPacking(int *a, int size, int n)
  6. {
  7.     int binCount = 1;
  8.     int s = size;
  9.     for (int i = 0; i < n; i++)
  10.     {
  11.         if (s - *(a + i) > 0)
  12.         {
  13.             s -= *(a + i);
  14.             continue;
  15.         }
  16.         else
  17.         {
  18.             binCount++;
  19.             s = size;
  20.             i--;
  21.         }
  22.     }
  24.     cout << "Number of bins required: " << binCount;
  25. }
  27. int main(int argc, char **argv)
  28. {
  29.     cout << "BIN - PACKING Algorithm\n";
  30.     cout << "Enter the number of items in Set: ";
  31.     int n;
  32.     cin >> n;
  33.     cout << "Enter " << n << " items:";
  34.     int a[n];
  35.     for (int i = 0; i < n; i++)
  36.         cin >> a[i];
  37.     cout << "Enter the bin size: ";
  38.     int size;
  39.     cin >> size;
  40.     binPacking(a, size, n);
  41. }


$ g++ BinPacking.cpp
$ a.out
BIN - PACKING Algorithm
Enter the number of items in Set: 5
Enter 5 items:12 23 34 45 56
Enter the bin size: 70
Number of bins required: 3

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

Here’s the list of Best Reference Books in C++ Programming, Data Structures and Algorithms.


Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter