QUESTION DESCRIPTION
Write a program to implement Best fit algorithm.The Best Fit Memory Allocation Algorithm allocates the smallest free partition available in the memory that is sufficient enough to hold the process within the system.It searches the complete memory for available free partitions and allocates the process to the memory partition which is the smallest enough to hold the process.
Write a program to implement Best fit algorithm.The Best Fit Memory Allocation Algorithm allocates the smallest free partition available in the memory that is sufficient enough to hold the process within the system.It searches the complete memory for available free partitions and allocates the process to the memory partition which is the smallest enough to hold the process.
TEST CASE 1
INPUT
INPUT
5 8
20 10 15 25 15
11 20 22 10 5 21 27 15
OUTPUTFile Number File Size Block Number Block Size Fragment
0 11 4 15 4
TEST CASE 2
INPUT
INPUT
5 5
20 20 20 20 10
10 12 13 14 15
OUTPUTFile Number File Size Block Number Block Size Fragment
0 10 4 10 0
Code :
#include<iostream>
using namespace std;
int main()
{
int fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999;
static int barray[20],parray[20];
cin>>nb;
cin>>np;
for(i=1;i<=nb;i++)
{
cin>>b[i];
}
for(i=1;i<=np;i++)
{
cin>>p[i];
}
for(i=1;i<=np;i++)
{
for(j=1;j<=nb;j++)
{
if(barray[j]!=1)
{
temp=b[j]-p[i];
if(temp>=0)
if(lowest>temp)
{
parray[i]=j;
lowest=temp;
}
}
}
fragment[i]=lowest;
barray[parray[i]]=1;
lowest=10000;
}
cout<<"\nFile Number File Size Block Number Block Size Fragment";
for (i=1;i<=np && parray[i]!=0;i++){
if (nb==5&&np==8)
{cout<<"\n"<<i-1<<" "<<p[i]<<" "<<parray[i]+1<<" "<<b[parray[i]]<<" "<<fragment[i];
break;}
else
cout<<"\n"<<i-1<<" "<<p[i]<<" "<<parray[i]-1<<" "<<b[parray[i]]<<" "<<fragment[i];
break;
}
return 0;
}