best fit algorithm
C++
// C++ implementation of Best - Fit algorithm
#include<bits/stdc++.h>
using namespace std;
// Function to allocate memory to blocks as per Best fit
// algorithm
void bestFit(int blockSize[], int m, int processSize[], int n)
{
// Stores block id of the block allocated to a
// process
int allocation[n];
// Initially no block is assigned to any process
memset(allocation, -1, sizeof(allocation));
// pick each process and find suitable blocks
// according to its size ad assign to it
for (int i=0; i<n; i++)
{
// Find the best fit block for current process
int bestIdx = -1;
for (int j=0; j<m; j++)
{
if (blockSize[j] >= processSize[i])
{
if (bestIdx == -1)
bestIdx = j;
else if (blockSize[bestIdx] > blockSize[j])
bestIdx = j;
}
}
// If we could find a block for current process
if (bestIdx != -1)
{
// allocate block j to p[i] process
allocation[i] = bestIdx;
// Reduce available memory in this block.
blockSize[bestIdx] -= processSize[i];
}
}
cout << "\nProcess No.\tProcess Size\tBlock no.\n";
for (int i = 0; i < n; i++)
{
cout << " " << i+1 << "\t\t" << processSize[i] << "\t\t";
if (allocation[i] != -1)
cout << allocation[i] + 1;
else
cout << "Not Allocated";
cout << endl;
}
}
Also in C++:
- Title
- cpp pi from acos
- Category
- C++
- Title
- how to print to the serial monitor arduino
- Category
- C++
- Title
- write to file in C++
- Category
- C++
- Title
- 2d vector
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- cs1955 unity vector3
- Category
- C++
- Title
- C++ cin cout
- Category
- C++
- Title
- change int to string cpp
- Category
- C++
- Title
- constant variables in c++
- Category
- C++
- Title
- screen record ios simulator
- Category
- C++
- Title
- how to sort an array c++
- Category
- C++
- Title
- iterar un map c++
- Category
- C++
- Title
- c++ remove space from string
- Category
- C++
- Title
- maximum possible number atmost k swaps
- Category
- C++
- Title
- c++ declare variable
- Category
- C++
- Title
- sort function in cpp
- Category
- C++
- Title
- C++ pointer arithmetic
- Category
- C++
- Title
- C++ w3schools
- Category
- C++
- Title
- unordered_set c++
- Category
- C++
- Title
- error: redefinition of ‘class Customer’
- Category
- C++
- Title
- new class * [] c++
- Category
- C++
- Title
- C++ sfinae
- Category
- C++
- Title
- check an stack is empty c++
- Category
- C++
- Title
- how to make a n*n 2d dynamic array in c++
- Category
- C++
- Title
- c++ smart pointer 2d array
- Category
- C++
- Title
- char to string c++
- Category
- C++
- Title
- RLE Encoding/Compression c++
- Category
- C++
- Title
- what is atoi in strinf
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++
- Title
- c++ class method example
- Category
- C++
- Title
- restting a queue stl
- Category
- C++
- Title
- setbits
- Category
- C++
- Title
- clear qlayout
- Category
- C++
- Title
- std::substring
- Category
- C++
- Title
- declaration vs. definition cpp
- Category
- C++
- Title
- c++ reading string
- Category
- C++
- Title
- Get handle in C++
- Category
- C++
- Title
- initialization list c++
- Category
- C++
- Title
- string substr c++
- Category
- C++
- Title
- how to take input in C++ in coding
- Category
- C++
- Title
- initialize 2d array c++
- Category
- C++
- Title
- formal parameter c++
- Category
- C++
- Title
- index string c++
- Category
- C++
- Title
- singleton c++
- Category
- C++
- Title
- c++ throw exception
- Category
- C++
- Title
- c++ yes no question
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- c++ create array
- Category
- C++
- Title
- how to sort a vector in c++
- Category
- C++
- Title
- c++ vector pop_back
- Category
- C++
- Title
- properties of a set c++
- Category
- C++
- Title
- calculate sum in c++
- Category
- C++
- Title
- euler's totient function c++
- Category
- C++
- Title
- how to print a 2d array in c++
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- c++ for loop syntax
- Category
- C++
- Title
- intersection between vector c++
- Category
- C++
- Title
- bitset c++
- Category
- C++
- Title
- c++ vector size
- Category
- C++
- Title
- accumulate in cpp
- Category
- C++
- Title
- fast input output in c++
- Category
- C++
- Title
- what is time complexity of insertion sort
- Category
- C++
- Title
- if not defined c++
- Category
- C++
- Title
- get index of value c++
- Category
- C++
- Title
- c++ wait for user input
- Category
- C++
- Title
- including cpp header file in c++
- Category
- C++
- Title
- first prime numbers less than
- Category
- C++
- Title
- how to append an element to an array in cpp
- Category
- C++
- Title
- how to get a letter from the users string in c++
- Category
- C++
- Title
- static_cast c++
- Category
- C++
- Title
- shuffle vector c++
- Category
- C++
- Title
- how to check datatype of a variable in c++
- Category
- C++
- Title
- sum of two numbers c++
- Category
- C++
- Title
- delay millis arduino
- Category
- C++
- Title
- eigenvalue of matrix c++ using Eigen
- Category
- C++
- Title
- time conversion hackerrank solution in c++
- Category
- C++
- Title
- c++ char to string
- Category
- C++
- Title
- c++ reverse vector
- Category
- C++
- Title
- c++ functions
- Category
- C++
- Title
- how to find hcf in c++
- Category
- C++
- Title
- c++ client service ros
- Category
- C++
- Title
- find number of 1s in a binary cv::mat image
- Category
- C++
- Title
- merge sort in c++
- Category
- C++
- Title
- basic data types in c++ hackerrank solution
- Category
- C++
- Title
- prefix sum array
- Category
- C++
- Title
- how to calculate inverse trigonometric values in c++
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- c++ random
- Category
- C++
- Title
- modular exponentiation c++
- Category
- C++
- Title
- c++ modulo make it give only positive numbers
- Category
- C++
- Title
- map arduino
- Category
- C++
- Title
- substitution failure is not an error
- Category
- C++
- Title
- how to format decimal palces in c++
- Category
- C++
- Title
- c++ declare char
- Category
- C++
- Title
- c++ loop through array
- Category
- C++
- Title
- c++ iterate over vector
- Category
- C++
- Title
- inconsequential meaning
- Category
- C++
- Title
- how to execute c++ program in cmd
- Category
- C++
- Title
- shortest path with bfs in c++
- Category
- C++
- Title
- variant hold type
- Category
- C++