
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Implement First Fit Decreasing for 1-D Objects and M Bins
Here is a C++ Program to implement First Fit Decreasing for 1-D objects and M bins
Required functions and pseudocode:
Begin function binPack() returns number of bins required. Initialize binC = 0 Initialize an array to store binVal. Place items one by one. function sort() to perform bubble sort in the descending order. End
Example Code
#include <iostream> using namespace std; void binPack(int *a, int s, int n) { int binC = 0; int binVal[n]; for (int i = 0; i < n; i++) binVal[i] = s; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (binVal[j] - a[i] >= 0) { binVal[j] -= a[i]; break; } } for (int i = 0; i < n; i++) if (binVal[i] != s) binC++; cout << "Number of bins required using first fit decreasing algorithm is: " << binC; } int* sort(int *seq, int n) { for (int i = 0; i < n; i++) for (int j = 0; j < n - 1; j++) if (seq[j] < seq[j + 1]) { seq[j] = seq[j] + seq[j + 1]; seq[j + 1] = seq[j] - seq[j + 1]; seq[j] = seq[j] - seq[j + 1]; } return seq; } int main(int argc, char **argv) { cout << "Enter the number of items in Set: "; int n; cin >> n; cout << "Enter " << n << " items:"; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; cout << "Enter the bin size: "; int s; cin >> s; int *seq = sort(a, n); binPack(seq, s, n); }
Output
Enter the number of items in Set: 7 Enter 7 items:4 6 7 5 3 2 1 Enter the bin size: 5 Number of bins required using first fit decreasing algorithm is: 3
- Related Articles
- C++ Program for First Fit algorithm in Memory Management
- Golang program to implement depth first search
- C++ Program for Best Fit algorithm in Memory Management
- C++ Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements From 1 to N
- C++ Program to Implement Bitap Algorithm for String Matching
- C++ Program to Implement Wagner and Fisher Algorithm for online String Matching
- For some integer $m$, every even integer is of the form,b>(A) $m$(B) $m + 1$(C) $2m$(D) $2m +1$
- 8085 program to implement the following function (a*b) + (c*d)
- Program to find first fit room from a list of rooms in Python
- C++ Program to Implement Fisher-Yates Algorithm for Array Shuffling
- Count permutations that are first decreasing then increasing in C++
- Potable water isnot fit for drinkingfit for drinkingonly fit for watering plantsonly fit for bathing
- C++ Program to Implement Vector
- C++ Program to Implement Treap
- C++ Program to Implement Trie

Advertisements