最短的过程下一个算法产生不希望的结果

时间:2015-05-04 23:34:46

标签: java arrays for-loop process shortest

我目前正在编写一个具有突发时间,到达时间和进程号的Shortest-Process-Next算法。我在这里遇到的麻烦是,我的算法产生的输出不同于所需的输出。该算法的工作原理是我得到的最短突发时间B,可以在到达时A

例如,流程3的突发时间为56,这是最短的突发时间,到达时间为0。然后,进程2的突发时间为102,这是下一个没有到达时间的最短突发时间。如果某个流程在特定时间到达,例如流程5,其到达时间为100。然后接下来,因为流程2和流程3的突发时间总和为:56 + 102 = 158158大于流程5 100的到达时间。该算法将继续进行其余的过程。

我的2D数组具有这些值,P B A行除外:

P   B   A
3  56   0  
2  102  0  
1  135  0  
4  148  0  
5  125  100  
6  65   200 

预期产出:

Process executed 3
Process executed 2
Process executed 5
Process executed 6
Process executed 4
Process executed 1

我目前的代码是:

int[][] myArr=new int[6][3];
myArr[0][0]=3;
myArr[1][0]=2;
myArr[2][0]=1;
myArr[3][0]=4;
myArr[4][0]=5;
myArr[5][0]=6;
myArr[0][1]=56;
myArr[1][1]=102;
myArr[2][1]=135;
myArr[3][1]=148;
myArr[4][1]=125;
myArr[5][1]=65;
myArr[0][2]=0;
myArr[1][2]=0;
myArr[2][2]=0;
myArr[3][2]=0;
myArr[4][2]=100;
myArr[5][2]=200;
ArrayList <Integer> m = new ArrayList<Integer>();
ArrayList <Integer> n = new ArrayList<Integer>();
ArrayList <Integer> nPro = new ArrayList<Integer>();
ArrayList <Integer> myArrCloneId = new ArrayList<Integer>();
ArrayList <Integer> myArrCloneBurst = new ArrayList<Integer>();
ArrayList <Integer> myArrCloneArrival = new ArrayList<Integer>();

int y = 0;
int y2 = 0;
int y3 = 0;
int y4 = 0;

for(int g = 0; g < myArr.length; g++) {
    myArrCloneId.add(myArr[g][0]);
    myArrCloneBurst.add(myArr[g][1]);
    myArrCloneArrival.add(myArr[g][2]);
}

for(int j = 0; j < myArr.length; j++) {
    y = myArr[j][1];


    for(int i = 0; i < myArr.length; i++) {
        for(int i1 = 0; i1 < myArr.length; i1++) {

            if( i > 0 && counter >= myArr[i][2] &&
                myArr[i][2] > myArr[i-1][2] &&
                myArr[i][1] < myArr[i1][1] &&
                (nPro.contains(myArr[i1][1]) == false) && 
                (nPro.contains(myArr[i1][0]) == false)
              ) {

                if(m.isEmpty()) {
                    System.out.println("Process executed " + myArr[i][0]);

                    myArrCloneId.remove(Integer.valueOf(myArr[i][0]));
                    myArrCloneBurst.remove(Integer.valueOf(myArr[i][1]));
                    myArrCloneArrival.remove(Integer.valueOf(myArr[i][2]));

                    counter = counter + myArr[i][1];

                    m.add(myArr[i][0]);
                    n.add(myArr[j][1]);
                    nPro.add(myArr[j][0]);
                } else {
                    for(int u = 0; u < m.size(); u++) {

                        if( m.get(u) != myArr[i1][0] &&
                            (m.contains(myArr[i1][0]) == false)
                          ) {

                            for(int u1 = 0; u1 < myArrCloneId.size(); u1++) {
                                if( myArr[i1][1] > myArrCloneBurst.get(u1) && 
                                    myArr[i1][2] == myArrCloneArrival.get(u1)
                                  ) {
                                    y2 = myArrCloneId.get(u1);
                                    y3 = myArrCloneBurst.get(u1);
                                    y4 = myArrCloneArrival.get(u1);

                                    m.add(y2);
                                    System.out.println("Process executed " + y2);

                                    myArrCloneId.remove(Integer.valueOf(y2));
                                    myArrCloneBurst.remove(Integer.valueOf(y3));
                                    myArrCloneArrival.remove(Integer.valueOf(y4));

                                    n.add(y3);
                                    nPro.add(y2);

                                    counter = counter + y3;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    if(m.contains(myArr[j][0]) == false) {
        System.out.println("Process executed " + myArr[j][0]);

        myArrCloneId.remove(Integer.valueOf(myArr[j][0]));
        myArrCloneBurst.remove(Integer.valueOf(myArr[j][1]));
        myArrCloneArrival.remove(Integer.valueOf(myArr[j][2]));

        n.add(myArr[j][1]);
        nPro.add(myArr[j][0]);

        counter = counter + myArr[j][1];
    }

我用这个逻辑得到的输出是:

Process executed 3
Process executed 2
Process executed 5
Process executed 1
Process executed 4
Process executed 6

我的问题是:为什么我的代码没有产生所需的输出?

0 个答案:

没有答案