C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

1次元オブジェクトとMビンのFirstFit減少を実装するC++プログラム


これは、1-DオブジェクトとMビンのFirstFitDecreasingを実装するためのC++プログラムです

必要な機能と擬似コード:

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

サンプルコード

#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);
}

出力

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

  1. バブルソートを実装するC++プログラム

    バブルソートは、比較ベースのソートアルゴリズムです。このアルゴリズムでは、隣接する要素が比較および交換されて、正しいシーケンスが作成されます。このアルゴリズムは他のアルゴリズムよりも単純ですが、いくつかの欠点もあります。このアルゴリズムは、多数のデータセットには適していません。並べ替えタスクの解決には時間がかかります。 バブルソート手法の複雑さ 時間計算量:最良の場合はO(n)、O(n 2 )平均および最悪の場合 スペースの複雑さ:O(1) Input − A list of unsorted data: 56 98 78 12 30 51 Output &mi

  2. 基数ソートを実装するC++プログラム

    基数ソートは、非比較ソートアルゴリズムです。この並べ替えアルゴリズムは、同じ位置と値を共有する数字をグループ化することにより、整数キーで機能します。基数は、記数法のベースです。 10進法では、基数または基数は10であることがわかっているので、いくつかの10進数を並べ替えるには、数値を格納するために10個の位取りボックスが必要です。 基数ソート手法の複雑さ 時間計算量:O(nk) スペースの複雑さ:O(n + k) Input − The unsorted list: 802 630 20 745 52 300 612 932 78 187 Output &minus