Question
Create a C++ program with the algorithm.
Algorithm First Fit Terrance Tao or all elements i 1,2,3,...in do for all bins j = 1,2,3.... do . I objects i fits in bin j t
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program to use first fit after merge sorting in decreasing order:

#include<bits/stdc++.h>
#include<stdlib.h>
using namespace std;
  
// Function to allocate memory to
// blocks as per First fit algorithm
void firstFit(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++)
{
for (int j = 0; j < m; j++)
{
if (blockSize[j] >= processSize[i])
{
// allocate block j to p[i] process
allocation[i] = j;
  
// Reduce available memory in this block.
blockSize[j] -= processSize[i];
  
break;
}
}
}
  
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
{
//blocksize[i]={processSize[i]};
  
cout << allocation[i]+(m++)+2;
}
cout << endl;
}
}
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
  
/* create temp arrays */
int L[n1], R[n2];
  
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
  
/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] >= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
  
/* Copy the remaining elements of L[], if there
are any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
  
/* Copy the remaining elements of R[], if there
are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
  
/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;
  
// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
  
merge(arr, l, m, r);
}
}
void printArray(int A[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
  
// Driver code
int main()
{
int blockSize[] = {100, 500, 200, 300, 600};
int arr[] = {212, 417, 112, 426,700,600,100};
int m = sizeof(blockSize) / sizeof(blockSize[0]);
int n = sizeof(arr) / sizeof(arr[0]);
int arr_size = sizeof(arr)/sizeof(arr[0]);
  
printf("Given array is \n");
printArray(arr, arr_size);
  
mergeSort(arr, 0, arr_size - 1);
  
printf("\nSorted array is \n");
printArray(arr, arr_size);
  
firstFit(blockSize, m, arr, n);
  
return 0 ;
}

**********************************************************************************************************************************************
Note that : Here blockSize is bin and processSize is object according to the algorithm given.

Add a comment
Know the answer?
Add Answer to:
Create a C++ program with the algorithm. Algorithm First Fit Terrance Tao or all elements i...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is all I have 2. Consider the following approximation algorithm for the bin packing problem....

    This is all I have 2. Consider the following approximation algorithm for the bin packing problem. Algorithm Last Fit(1,S) Input: Set I of items and set S of item sizes; item I; E I has size S; Output: A packing of I into unit size bins B {} for each item I; e I do { if I; fits in one of the bins of B then Put I; in the last bin where it fits else { Add a...

  • I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that...

    I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonical zing data and for producing human-readable output. More formally, the output must satisfy...

  • Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below...

    Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below : : Merge Sort algorithm using 2 processes a.) Define an integer array of 100 integers. Populate this array with random numbers. You can use int rand(void); function. Do not forget to initialize this function. You will sort the numbers in the array using merge-sort algorithm. In merge sort algorithm the half of the array will be sorted by one process and second...

  • Please I am in a hurry, I need an answer asap. I have seen an answer previously regarding to this...

    Please I am in a hurry, I need an answer asap. I have seen an answer previously regarding to this question, however I found a lot of mistakes on it, where the guy declared a public class, which is not a thing in C language. Furthermore it is important to divide the question into two C files, one for part a and one for part b. hw2 Merge Sort algorithm using 2 processes a.) Define an integer array of 100...

  • Create the following function in java: ALGORITHM PERMUTATION GENERATOR PermGenerator(integer n 22) li generates in lexicographical...

    Create the following function in java: ALGORITHM PERMUTATION GENERATOR PermGenerator(integer n 22) li generates in lexicographical order all permutations llof the integers in the set {I.....!! Local variables: integers i //indices of permutation elements integer //for loop counter integers d., d..., Mleft to right elements of a permutation Section 4.4 Permutations and Combinations 283 1/create and write out smallest permutation for k=1 to n do end for write d.d..... //create and write out remaining permutations for k = 2 to...

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...

  • iii.print out the arraylist iv.reverse all the elements v.print out this arraylist vi.make a clone of...

    iii.print out the arraylist iv.reverse all the elements v.print out this arraylist vi.make a clone of the arraylist vii.remove all the elements at any odd index of the original arraylist (not the cloned one) viii.print out the original arraylist ix.reverse the cloned arraylist x.print out the cloned arraylist (this arraylist should still contain the original sequence of elements in order) xi.merge the cloned arraylist to the original arraylist (please think about what happens and draw a diagram for yourself to...

  • Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout...

    Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout to add buttons to start each sort and display the System.nanoTime in common TextArea panel. The question is a bit confusing so i will try to simplify it. Using the GUI ( I made a unclick able one so you have to make it clickable), allow a user to sort the text file based on what they click on. example: if i click merge...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT