-
Notifications
You must be signed in to change notification settings - Fork 1
/
first-fit-decreasing.js
51 lines (35 loc) · 1.31 KB
/
first-fit-decreasing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function firstFitDecreasing(collection, binSize) {
var collectionObj = [];
for (var id in collection) {
collectionObj.push({
id : id,
statement : collection[id],
size : collection[id].length + 2 // Each SIDs require a ",\n" between them
});
}
var collectionCount = collectionObj.length;
var collectionSorted = collectionObj.sort(function(a, b) { return b.size - a.size; });
var binCount = 0;
var binMemory = [];
for (var i = 0; i < collectionCount; i++) {
var j = 0;
for(j = 0; j < binCount; j++) {
if (binMemory[j].capacity >= collectionSorted[i].size) {
binMemory[j].capacity = binMemory[j].capacity - collectionSorted[i].size;
binMemory[j].collection.push(collectionSorted[i]);
break;
}
}
if(j == binCount) {
binMemory[binCount] = {
capacity : binSize,
collection: []
};
binMemory[binCount].capacity = binMemory[binCount].capacity - collectionSorted[i].size;
binMemory[binCount].collection.push(collectionSorted[i]);
binCount++;
}
}
return binMemory;
}
firstFitDecreasing(INPUT_COLLECTION, INPUT_BIN_SIZE);