記憶體動態分配的首先適應、最優適應、最壞適應演算法的實現(java 版)__演算法

來源:互聯網
上載者:User

public class DynamicAssign {

 public static void main(String[] args) {
  Area[] area = new Area[5];
  area[0] = new Area(0, 20);
  area[1] = new Area(1, 30);
  area[2] = new Area(2, 40);
  area[3] = new Area(3, 50);
  area[4] = new Area(4, 150);
  Memory[] memory = new Memory[4];
  memory[0] = new Memory(0, 100);
  memory[1] = new Memory(1, 30);
  memory[2] = new Memory(2, 50);
  memory[3] = new Memory(3, 40);
  FirstAdapt(memory, area);    
  BestAdapt(memory, area);   
  BadAdapt(memory,area);
 }

//首先適應演算法

 public static void FirstAdapt(Memory[] memory, Area[] area) {
  int k;
  for (int i = 0; i < memory.length; i++) {
   k = area.length;
   for (int j = 0; j < area.length; j++) {
    if ((memory[i].getValue()) <= (area[j].getValue())) {
     area[j].setValue(area[j].getValue() - memory[i].getValue());
     System.out.println(memory[i] + "--->" + area[j]);
     break;
    } else
     k--;
    if (k < 1) {
     System.out.println(memory[i] + " Failed !");
    }

   }

  }

 }

//最優適應演算法

 public static void BestAdapt(Memory[] memory, Area[] area) {
  int i, j;
  for (i = 0; i < memory.length; i++) {
   boolean flag = true;

   int min = 0;
   for (j = 0; j < area.length; j++) {
    if (flag) {
     if (memory[i].getValue() <= area[j].getValue()) {
      min = area[j].getValue();
      flag = false;
     }
    } else if ((memory[i].getValue() <= area[j].getValue())
      && (min >= area[j].getValue())) {
     min = area[j].getValue();
    }
   }
   if (min == 0) {
    System.out.println(memory[i] + " Failed !");

   } else {
    for (j = 0; j < area.length; j++) {
     if (area[j].getValue() == min) {
      System.out.println(memory[i] + "--->" + area[j]);
      area[j].setValue(min - memory[i].getValue());
      break;
     }
    }
   }

  }

 }

//最壞適應演算法

 public static void BadAdapt(Memory[] memory, Area[] area) {
  int i, j;
  for (i = 0; i < memory.length; i++) {
   boolean flag = true;

   int max = 0;
   for (j = 0; j < area.length; j++) {
    if (flag) {
     if (memory[i].getValue() <= area[j].getValue()) {
      max = area[j].getValue();
      flag = false;
     }
    } else if ((memory[i].getValue() <= area[j].getValue())
      && (max <= area[j].getValue())) {
     max = area[j].getValue();
    }
   }
   if (max == 0) {
    System.out.println(memory[i] + " Failed !");

   } else {
    for (j = 0; j < area.length; j++) {
     if (area[j].getValue() == max) {
      System.out.println(memory[i] + "--->" + area[j]);
      area[j].setValue(max - memory[i].getValue());
      break;
     }
    }
   }

  }
 }
}

//定義一個記憶體作業區

class Area {

 private int id;

 private int value;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public int getValue() {
  return value;
 }

 public void setValue(int value) {
  this.value = value;
 }

 Area(int id) {
  this.id = id;
 }

 Area(int id, int value) {
  this.id = id;
  this.value = value;
 }

 public String toString() {
  return "Area:" + id;

 }

}

//定義一個記憶體作業

class Memory {

 Area[] area = new Area[5];

 Memory[] memory = new Memory[4];

 private int id;

 private int value;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public int getValue() {
  return value;
 }

 public void setValue(int value) {
  this.value = value;
 }

 Memory(int id) {
  this.id = id;
 }

 Memory(int id, int value) {
  this.id = id;
  this.value = value;
 }

 public String toString() {
  return "memory:" + id;
 }

}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.