2010-10-16 88 views
0

我必须使用SRTN算法模拟进程调度器,并且在某个部分内部遇到问题。排序队列

我有一个名为'Process'的自定义类的队列,我需要根据名为'last_prediction'的字段对它进行排序。我的代码大部分时间都在工作,但是如果您查看我的output的时间:19,就绪队列中的输出错误(应为:1004(1.5)1002(2)1003(2))。

这里是我的代码:

int count = ReadyQueue.Count; 

     // Copy Queue into Vector 
     ArrayList temp = new ArrayList(); 
     for (int i = 0; i < count; i++) 
     { 
      Process p = (Process)ReadyQueue.Dequeue(); 
      temp.Add(p); 
     } 

     // Sort Vector 
     for (int i = 0; i < count; i++) 
     { 
      double min = ((Process)temp[i]).last_prediction; 
      for (int j=i+1; j<count; j++) 
      { 
       if (((Process)temp[j]).last_prediction < min) 
       { 
        min = ((Process)temp[j]).last_prediction; 
        Process dummy = (Process)temp[j]; 
        temp[j] = temp[i]; 
        temp[i] = dummy; 
       } 
      } 
     } 

     // Copy Vector back into Queue 
     for (int i = 0; i < count; i++) 
     { 
      Process p = (Process)temp[i]; 
      ReadyQueue.Enqueue(p); 
     } 

编辑:好吧,我尝试使用ICompare,类似于你所付出的hughdbrown.Now我得到一个不同的错误:

public class Process 
    { 
     public int process_id; 
     public int arrival_time; 
     public int total_time; 
     public int avg_burst; 
     public int actual_burst; 
     public int last_burst; // SRTN 
     public double last_prediction; // SRTN 
     public int io_delay; 
     public int context_switch_delay; 

     public class ProcessSort : IComparer 
     { 
      public int Compare(object x, object y) 
      { 
       var a = x as Process; 
       var b = y as Process; 
       double aNum = a.last_prediction; 
       double bNum = b.last_prediction; 
       return Compare(aNum, bNum); 
      } 
     } 
    } 

这是错误我现在得到:

Unhandled Exception: System.InvalidOperationException: Failed to compare two elements in the array. ---> System.NullReferenceException: Object reference not set to an instance of an object. 
+1

它不会停止成为一个队列,一旦你排序吗?它只是成为另一个列表。 – 2010-10-16 03:17:04

+0

A1(B1)A2(B2)A3(B3)您是否想根据B值对其进行排序,然后根据它们的A值对具有相同B值的那些进行排序? – blizpasta 2010-10-16 03:37:31

+0

不完全。我需要它,所以进程停留在它的位置,如果它前面的值是等值的 – 2010-10-16 04:16:37

回答

2

我会在这个阵列上使用真正的排序程序,而不是手工制作的插入/冒泡排序。为你的对象添加一个比较函数。

我也会使用模板化数据集合,而不是ArrayList。您可能有兴趣使用此C#PriorityQueue代码from my website。它具有队列语义并以排序顺序维护项目。


后来:您IComparable的代码将是这样的:

public class Process : IComparable 
{ 
    int last_prediction; 
    public int CompareTo(object obj) 
    { 
      Process right = obj as Process; 
      return this.last_prediction.CompareTo(right.last_prediction); 
    } 
} 

后来还是:这里是有一个排序过程的完整测试程序。在Ubuntu上进行单声道测试。

using System; 
using System.Collections.Generic; 
using System.Text; 

namespace Comparer 
{ 
    public class Process : IComparable 
    { 
     int last_prediction; 
     public Process(int p) 
     { 
      this.last_prediction = p; 
     } 
     public int CompareTo(object obj) 
     { 
      Process right = obj as Process; 
      return this.last_prediction.CompareTo(right.last_prediction); 
     } 
     public int Prediction { get { return this.last_prediction; } } 
    } 

    class MainClass 
    { 
     public static void Main (string[] args) 
     { 
      List<Process> list = new List<Process>(); 
      for (int i = 0; i < 10; i++) 
       list.Add(new Process(10 - i)); 

      System.Console.WriteLine("Current values:"); 
      foreach (Process p in list) 
       System.Console.WriteLine("Process {0}", p.Prediction); 

      list.Sort(); 

      System.Console.WriteLine("Sorted values:"); 
      foreach (Process p in list) 
       System.Console.WriteLine("Process {0}", p.Prediction); 
     } 
    } 
} 
+0

我想现在使用它,但是我得到一个stackoverflow错误。有没有办法像myArrList.Sort(Process.last_prediction)? – 2010-10-16 02:25:47

+0

看到我上面的编辑 – 2010-10-16 02:44:04

+0

休,非常感谢你的支持。你的排序工作,但它仍然给我像我的类似的输出。如果所比较的两个值是相同的值,我不认为它会交换。这就是为什么我混淆了我原来的代码是交换使用'<'当这应该发生使用'<='作为比较 – 2010-10-16 04:20:11

0

你有没有考虑过使用ArrayList.Sort方法,而不是试图wri你自己的?

-1

啊..使用arraylist.sort

如果乌尔阵列只拿到了号,创建一个新的数字阵列堂妹.. arraylist.sort字符串有一些问题。

,并使用arraylist.sort

把你想和转换回字符串如果u想要的位置的数量..

+3

你从你的手机发送你的原始答案? :) – 2010-10-16 01:22:50

+0

不是真的..为什么呢? – william 2010-10-16 01:59:28

0

这是我会怎么处理对象进行排序。我们使用List<Process>而不是ArrayList,这样我们就不需要继续来回投射它。我并没有在C#中使用过多的队列,所以我害怕我对这些事情无能为力。请注意,此代码未经测试。 :)

int count = ReadyQueue.Count; 

    // Copy Queue into Vector 
    List<Process> listProcesses = new List<Process>(); 

    for(int i = 0; i < count; i++) 
    { 
     Process p = (Process)ReadyQueue.Dequeue(); 
     listProcesses.Add(p); 
    } 

    // Sort Vector 
    listProcesses.Sort(CompareProcessesByPrediction); 

    // Copy Vector back into Queue 
    foreach(Process p in listProcesses) 
     ReadyQueue.Enqueue(p); 


private static int CompareProcessesByPrediction(Process proc1, Process proc2) 
{ 
    //if they're both not-null, figure out which one is greatest/smallest. 
    //otherwise just pick the one that isn't null 
    if(proc1 == null) 
     return proc2 == null ? 0 : -1; 
    else 
     return proc1 == null ? 1 : proc1.last_prediction.CompareTo(proc2.last_prediction); 
}