diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..d478271 --- /dev/null +++ b/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/src/Bins.java b/src/Bins.java index b313a3d..ed09a15 100644 --- a/src/Bins.java +++ b/src/Bins.java @@ -34,40 +34,39 @@ public static void main (String args[]) { Scanner input = new Scanner(Bins.class.getClassLoader().getResourceAsStream(DATA_FILE)); List data = b.readData(input); - PriorityQueue pq = new PriorityQueue(); - 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 pq = new PriorityQueue(); + 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 pq1 = new PriorityQueue(); + 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 data){ + int total=0; + for (Integer size : data) { + total +=size; + } + return total; + + } + private static PriorityQueue worstFit(List data){ + int diskId = 1; + PriorityQueue pq = new PriorityQueue(); + pq.add(new Disk(0)); + for (Integer size : data) { Disk d = pq.peek(); if (d.freeSpace() >= size) { pq.poll(); @@ -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 pq){ + System.out.println("number of pq used: " + pq.size()); while (!pq.isEmpty()) { System.out.println(pq.poll()); }