开发者

Sorting a Queue

开发者 https://www.devze.com 2023-01-20 00:53 出处:网络
I have to simulate a process scheduler using SRTN algorithm and im having trouble within a certain part.

I have to simulate a process scheduler using SRTN algorithm and im having trouble within a certain part.

I have a queue of a custom class called 'Process' I need to sort it based on a a field called 'last_prediction'. My code works most of the time, but if you look at time:19 of my output, the output in the ready queue is wrong (it should be: 1004(1.5) 1002(2) 1003(2)).

Here is my code:

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

EDIT: ok, im trying to use ICompare, similar to what you gave hughdbrown.Now i get a different error:

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

this is the error i get now:

Unhan开发者_运维问答dled Exception: System.InvalidOperationException: Failed to compare two elements in the array. ---> System.NullReferenceException: Object reference not set to an instance of an object. 


I would use a real sorting routine on this array, not a hand-crafted insertion/bubble sort. Add a comparison function to your object.

I'd also use a templatized data collection, not ArrayList. You might be interested in using this C# PriorityQueue code from my website. That has Queue semantics and maintains items in a sorted order.


Later: Your IComparable code would be something like this:

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

Later still: here is a complete test program that has a sortable Process. Tested in Mono on 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);
        }
    }
}


Have you considered using the ArrayList.Sort method instead of attempting to write your own sort?


Here's how I would sort the Process objects. Let's use a List<Process> instead of an ArrayList so that we don't have to keep casting it back and forth. I haven't done much with queues in C# so I'm afraid I can't help much with those. And please note that this code is untested. :)

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


yea.. use arraylist.sort

If ur array only got numbers, create a new number array coz.. arraylist.sort for string has some problem.

and use arraylist.sort

take the number of the position you want and convert back to string if u want..

0

精彩评论

暂无评论...
验证码 换一张
取 消