The first adaptation of memory dynamic allocation, the optimal adaptation, the worst adaptation algorithm (Java version) __ algorithm

Source: Internet
Author: 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);
}

First adaptive algorithm

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!");
}

}

}

}

Optimal adaptive algorithm

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
}
}
}

}

}

Worst Fit algorithm

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 ();
&NBSP;&NBSP;&NBSP;&NBSP}
   }
   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
}
}
}

}
}
}

Define a memory Job area

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;

}

}

Define a memory job

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;
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.