Prerequisite : Partition allocation methods
Worst Fit allocates a process to the partition which is largest sufficient among the freely available partitions available in the main memory. If a large process comes at a later stage, then memory will not have space to accommodate it.
Example:
Input : blockSize[] = {100, 500, 200, 300, 600};
processSize[] = {212, 417, 112, 426};
Output:
Process No. Process Size Block no.
1 212 5
2 417 2
3 112 5
4 426 Not Allocated

Implementation:
1- Input memory blocks and processes with sizes.
2- Initialize all memory blocks as free.
3- Start by picking each process and find the
maximum block size that can be assigned to
current process i.e., find max(bockSize[1],
blockSize[2],.....blockSize[n]) >
processSize[current], if found then assign
it to the current process.
5- If not then leave that process and keep checking
the further processes.
Below is implementation of above steps.
C++
#include<bits/stdc++.h>
using namespace std;
void worstFit( int blockSize[], int m, int processSize[],
int n)
{
int allocation[n];
memset (allocation, -1, sizeof (allocation));
for ( int i=0; i<n; i++)
{
int wstIdx = -1;
for ( int j=0; j<m; j++)
{
if (blockSize[j] >= processSize[i])
{
if (wstIdx == -1)
wstIdx = j;
else if (blockSize[wstIdx] < blockSize[j])
wstIdx = j;
}
}
if (wstIdx != -1)
{
allocation[i] = wstIdx;
blockSize[wstIdx] -= 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;
}
}
int main()
{
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = sizeof (blockSize)/ sizeof (blockSize[0]);
int n = sizeof (processSize)/ sizeof (processSize[0]);
worstFit(blockSize, m, processSize, n);
return 0 ;
}
|
Java
public class GFG
{
static void worstFit( int blockSize[], int m, int processSize[],
int n)
{
int allocation[] = new int [n];
for ( int i = 0 ; i < allocation.length; i++)
allocation[i] = - 1 ;
for ( int i= 0 ; i<n; i++)
{
int wstIdx = - 1 ;
for ( int j= 0 ; j<m; j++)
{
if (blockSize[j] >= processSize[i])
{
if (wstIdx == - 1 )
wstIdx = j;
else if (blockSize[wstIdx] < blockSize[j])
wstIdx = j;
}
}
if (wstIdx != - 1 )
{
allocation[i] = wstIdx;
blockSize[wstIdx] -= processSize[i];
}
}
System.out.println( "\nProcess No.\tProcess Size\tBlock no." );
for ( int i = 0 ; i < n; i++)
{
System.out.print( " " + (i+ 1 ) + "\t\t" + processSize[i] + "\t\t" );
if (allocation[i] != - 1 )
System.out.print(allocation[i] + 1 );
else
System.out.print( "Not Allocated" );
System.out.println();
}
}
public static void main(String[] args)
{
int blockSize[] = { 100 , 500 , 200 , 300 , 600 };
int processSize[] = { 212 , 417 , 112 , 426 };
int m = blockSize.length;
int n = processSize.length;
worstFit(blockSize, m, processSize, n);
}
}
|
Python3
def worstFit(blockSize, m, processSize, n):
allocation = [ - 1 ] * n
for i in range (n):
wstIdx = - 1
for j in range (m):
if blockSize[j] > = processSize[i]:
if wstIdx = = - 1 :
wstIdx = j
elif blockSize[wstIdx] < blockSize[j]:
wstIdx = j
if wstIdx ! = - 1 :
allocation[i] = wstIdx
blockSize[wstIdx] - = processSize[i]
print ( "Process No. Process Size Block no." )
for i in range (n):
print (i + 1 , " " ,
processSize[i], end = " " )
if allocation[i] ! = - 1 :
print (allocation[i] + 1 )
else :
print ( "Not Allocated" )
if __name__ = = '__main__' :
blockSize = [ 100 , 500 , 200 , 300 , 600 ]
processSize = [ 212 , 417 , 112 , 426 ]
m = len (blockSize)
n = len (processSize)
worstFit(blockSize, m, processSize, n)
|
C#
using System;
class GFG
{
static void worstFit( int []blockSize, int m,
int []processSize, int n)
{
int []allocation = new int [n];
for ( int i = 0; i < allocation.Length; i++)
allocation[i] = -1;
for ( int i = 0; i < n; i++)
{
int wstIdx = -1;
for ( int j = 0; j < m; j++)
{
if (blockSize[j] >= processSize[i])
{
if (wstIdx == -1)
wstIdx = j;
else if (blockSize[wstIdx] < blockSize[j])
wstIdx = j;
}
}
if (wstIdx != -1)
{
allocation[i] = wstIdx;
blockSize[wstIdx] -= processSize[i];
}
}
Console.WriteLine( "\nProcess No.\tProcess Size\tBlock no." );
for ( int i = 0; i < n; i++)
{
Console.Write( " " + (i+1) + "\t\t\t" + processSize[i] + "\t\t\t" );
if (allocation[i] != -1)
Console.Write(allocation[i] + 1);
else
Console.Write( "Not Allocated" );
Console.WriteLine();
}
}
public static void Main(String[] args)
{
int []blockSize = {100, 500, 200, 300, 600};
int []processSize = {212, 417, 112, 426};
int m = blockSize.Length;
int n = processSize.Length;
worstFit(blockSize, m, processSize, n);
}
}
|
Javascript
<script>
function worstFit(blockSize, m,
processSize, n)
{
let allocation = new Array(n);
for (let i = 0; i < allocation.length; i++)
allocation[i] = -1;
for (let i = 0; i < n; i++)
{
let wstIdx = -1;
for (let j = 0; j < m; j++)
{
if (blockSize[j] >= processSize[i])
{
if (wstIdx == -1)
wstIdx = j;
else if (blockSize[wstIdx] <
blockSize[j])
wstIdx = j;
}
}
if (wstIdx != -1)
{
allocation[i] = wstIdx;
blockSize[wstIdx] -= processSize[i];
}
}
document.write( "<br>Process No.  " +
" Process Size  " +
" Block no.<br>" );
for (let i = 0; i < n; i++)
{
document.write( " " + (i + 1) +
"     " +
"    " +
processSize[i] +
"      " );
if (allocation[i] != -1)
document.write(allocation[i] + 1);
else
document.write( "Not Allocated" );
document.write( "<br>" );
}
}
let blockSize = [ 100, 500, 200, 300, 600 ];
let processSize = [ 212, 417, 112, 426 ];
let m = blockSize.length;
let n = processSize.length;
worstFit(blockSize, m, processSize, n);
</script>
|
Output
Process No. Process Size Block no.
1 212 5
2 417 2
3 112 5
4 426 Not Allocated
Time Complexity: O(N*M) where N is processSize length and M is blockSize length.
Auxiliary Space: O(N)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
13 Sep, 2023
Like Article
Save Article