Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zap3 hl182 #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="data"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
59 changes: 29 additions & 30 deletions src/Bins.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,39 @@ public static void main (String args[]) {
Scanner input = new Scanner(Bins.class.getClassLoader().getResourceAsStream(DATA_FILE));
List<Integer> data = b.readData(input);

PriorityQueue<Disk> pq = new PriorityQueue<Disk>();
pq.add(new Disk(0));


int diskId = 1;
int total = 0;
for (Integer size : data) {
Disk d = pq.peek();
if (d.freeSpace() > size) {
pq.poll();
d.add(size);
pq.add(d);
} else {
Disk d2 = new Disk(diskId);
diskId++;
d2.add(size);
pq.add(d2);
}
total += size;
}
int total = checkTotal(data);
PriorityQueue <Disk> pq = new PriorityQueue<Disk>();
pq= worstFit(data);

System.out.println("total size = " + total / 1000000.0 + "GB");
System.out.println();
System.out.println("worst-fit method");
System.out.println("number of pq used: " + pq.size());
while (!pq.isEmpty()) {
System.out.println(pq.poll());
}
System.out.println();
printMessages(pq);


Collections.sort(data, Collections.reverseOrder());
pq.add(new Disk(0));
PriorityQueue <Disk> pq1 = new PriorityQueue<Disk>();
pq1= worstFit(data);

diskId = 1;
for (Integer size : data) {
System.out.println();
System.out.println("worst-fit decreasing method");
printMessages(pq1);
}
private static int checkTotal(List<Integer> data){
int total=0;
for (Integer size : data) {
total +=size;
}
return total;

}
private static PriorityQueue<Disk> worstFit(List<Integer> data){
int diskId = 1;
PriorityQueue<Disk> pq = new PriorityQueue<Disk>();
pq.add(new Disk(0));
for (Integer size : data) {
Disk d = pq.peek();
if (d.freeSpace() >= size) {
pq.poll();
Expand All @@ -80,10 +79,10 @@ public static void main (String args[]) {
pq.add(d2);
}
}

System.out.println();
System.out.println("worst-fit decreasing method");
System.out.println("number of pq used: " + pq.size());
return pq;
}
private static void printMessages(PriorityQueue<Disk> pq){
System.out.println("number of pq used: " + pq.size());
while (!pq.isEmpty()) {
System.out.println(pq.poll());
}
Expand Down