编程语言
首页 > 编程语言> > 最佳适应算法与最坏适应算法

最佳适应算法与最坏适应算法

作者:互联网

一、实验内容

编程实现最佳适应算法与最坏适应算法

二、实验要求

1.任选一种高级语言实现;

三、实验过程

1、 设计思想

最佳适应算法(BF):将所有空闲分区按照容量大小从小到大排序,在申请内存分配时,从链首开始查找,将满足需求的第一个空闲分区分配给作业。

最坏适应算法(WF):将所有空闲分区按照容量大小从大到小排序,在申请内存分配时,从链首开始查找,将满足需求的第一个空闲分区分配给作业。

2、 数据结构

package com.hu;

public class FreeArea {
    private int address;//内存地址
    private int size;//空闲区大小
    boolean flag = true;//空闲区是否未被分配

    public FreeArea() {
    }

    public FreeArea(int address, int size) {
        this.address = address;
        this.size = size;
    }

    public int getAddress() {
        return address;
    }

    public void setAddress(int address) {
        this.address = address;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
}

运行结果

四、实验代码

RR.H

package com.hu;

import java.util.Scanner;

public class BFWF {

    public static void main(String[] args) {

        System.out.println("======最佳适应算法======");
        FreeArea freeArea[]= BFinit();
        BF(freeArea);
        System.out.println("=====最坏适应算法=====");
        FreeArea freeArea1[]= WFinit();
        WF(freeArea1);

    }

    public static void BF(FreeArea freeArea[]){//最佳适应算法

        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要分配的内存大小:");
        int size = scanner.nextInt();

        for (int i =0;i<freearea.length;i++){ if="" (size<="freeArea[i].getSize()&&freeArea[i].flag){//若分配内存大小小于空闲分区大小并且空闲分区未被占用则分配成功" system.out.println("分配内存成功");="" freearea[i].flag="false;//表示该空闲分区已分配" freearea[i].setsize(freearea[i].getsize()-size);="" break;="" }if="" (i="=" freearea.length-1&&size="">freeArea[i].getSize()) System.out.println("分配失败");
        }

    }

    public static void WF(FreeArea freeArea[]){//最坏适应算法

        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要分配的内存大小:");
        int size = scanner.nextInt();

        for (int i =0;i<freearea.length;i++){ if="" (size<="freeArea[i].getSize()&&freeArea[i].flag){//若分配内存大小小于空闲分区大小并且空闲分区未被占用则分配成功" system.out.println("分配内存成功");="" freearea[i].flag="false;//表示该空闲分区已分配" freearea[i].setsize(freearea[i].getsize()-size);="" break;="" }if="" (i="=" freearea.length-1&&size="">freeArea[i].getSize()) System.out.println("分配失败");
        }

    }

    public static FreeArea[] BFinit(){//空闲区初始化并按空闲区从小到大排序
        System.out.println("初始化空闲区并分配内存地址与大小");
        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);

        FreeArea[] freeAreas = new FreeArea[5];

        for (int i=0;i<freeareas.length;i++){ system.out.println("请输入内存地址与大小:");="" freeareas[i]="new" freearea(scanner.nextint(),scanner.nextint());="" }="" freearea="" t;="" for="" (int="" i="0;i<freeAreas.length;i++){" j="0;j<freeAreas.length-1;j++){" if(freeareas[j].getsize()="">freeAreas[j+1].getSize()){
                    t = freeAreas[j+1];
                    freeAreas[j+1]=freeAreas[j];
                    freeAreas[j]=t;
                }
            }
        }
        return freeAreas;
    }

    public static FreeArea[] WFinit(){//空闲区初始化并按空闲区从大到小排序
        System.out.println("初始化空闲区并分配内存地址与大小");
        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);

        FreeArea[] freeAreas = new FreeArea[5];

        for (int i=0;i</freeareas.length;i++){></freearea.length;i++){></freearea.length;i++){>

标签:int,最坏,System,适应,FreeArea,算法,public,空闲,size
来源: https://www.cnblogs.com/Hukai/p/14853419.html