SJF算法排序C#

时间:2012-12-20 13:20:47

标签: c#

这就是我的代码。我想使用更快的排序算法,可能需要快速排序或梳理排序。
我先按照到达时间对列表进行两次排序,然后是明确的时间。
我需要帮助实现更快的排序算法
我的主要方法是

static void Main(string[] args)
        {
            //----------------------------------------Reading I/O File--------------------------------------
            string s = Environment.CurrentDirectory.ToString();   // returns the directory of the exe file
            if (File.Exists(s + @"\input.txt"))  //checking if the input files exists
                Console.WriteLine("File Exists");
            else
            {
                Console.WriteLine("File Not Found");
                Console.WriteLine("-----------------------------------------------------");
                return;
            }
            Console.WriteLine("-----------------------------------------------------");
            //----------------------------------------Data Into List--------------------------------------
            string FileText = File.ReadAllText(s + @"\input.txt"); //reading all the text in the input file
            string[] lines = FileText.Split('\n'); //splitting the lines
            List<Process> processes = new List<Process>();
            for (int i = 1; i < lines.Length; i++)
            {
                string[] tabs = lines[i].Split('\t');//splitting the tabs to get objects' variables
                Process x = new Process(tabs[0], int.Parse(tabs[1]), int.Parse(tabs[2]), int.Parse(tabs[3]));//creating object
                processes.Add(x);//adding object to the list
            }
            //   ----------------------------------------Sorting The List--------------------------------------
            Process temp;
            for (int k = 0; k < processes.Count; k++)
            {
                for (int i = k + 1; i < processes.Count; i++)
                {
                    if (processes[k].arrivalTime > processes[i].arrivalTime)
                    {
                        temp = processes[i];
                        processes[i] = processes[k];
                        processes[k] = temp;
                    }
                }
            }
            int tempClock = 0;
            for (int i = 0; i < processes.Count; i++)
            {
                if (processes[i].arrivalTime > tempClock)
                    tempClock = processes[i].arrivalTime;
                for (int k = i + 1; k < processes.Count; k++)
                {
                    if (processes[k].arrivalTime <= tempClock && processes[k].brust < processes[i].brust)
                    {
                        temp = processes[i];
                        processes[i] = processes[k];
                        processes[k] = temp;
                    }
                }
                tempClock += processes[i].brust;
            }
            Console.WriteLine("Processes After Sorting");
            Console.WriteLine("-----------------------------------------------------");
            Console.WriteLine("Name\tArrival\tBrust\tPriority");
            for (int i = 0; i < processes.Count; i++)
            {
                Console.Write(processes[i].name + "\t" + processes[i].arrivalTime + "\t" + processes[i].brust + "\t" + processes[i].priority);
                Console.WriteLine();
            }
            Console.WriteLine("-----------------------------------------------------");
            //----------------------------------------Gantt Chart--------------------------------------
            Console.WriteLine("Gantt Chart");
            Console.WriteLine("-----------------------------------------------------");
            int counter = 0;
            for (int i = 0; i < processes.Count; i++)
            {
                Console.Write(processes[i].name + "\t");
               if (processes[i].arrivalTime < counter)
                    printSpaces(counter);
               else
               {
                   printSpaces(processes[i].arrivalTime);
                   counter = processes[i].arrivalTime;
               }
                printHashes(processes[i].brust);
                counter += processes[i].brust;
                Console.WriteLine();
            }
            Console.WriteLine("-----------------------------------------------------");
            //-----------------------------------Completing Data And final Table-------------------------
            int clock = 0, totalwait = 0, totalturnAround = 0;
            for (int i = 0; i < processes.Count; i++)
            {
                if (processes[i].arrivalTime > clock)
                {
                    processes[i].start = processes[i].arrivalTime;
                    clock += processes[i].start - processes[i].arrivalTime;
                    clock += processes[i].brust;

                }
                else
                {
                    if (i > 0)
                        processes[i].start = processes[i - 1].end;
                    clock += processes[i].brust;
                }
                if (processes[i].start > processes[i].arrivalTime)
                    processes[i].wait = processes[i].start - processes[i].arrivalTime;
                else processes[i].wait = 0;
                processes[i].end = processes[i].start + processes[i].brust;
                processes[i].turnAround = processes[i].wait + processes[i].brust;
                totalwait += processes[i].wait;
                totalturnAround += processes[i].turnAround;
            }
            Console.WriteLine("Name\tArrival\tBrust\tStart\tEnd\tWait\tturnaround");
            for (int i = 0; i < processes.Count; i++)
            {
                Console.Write(processes[i].name + "\t" + processes[i].arrivalTime + "\t" + processes[i].brust + "\t" + processes[i].start + "\t" + processes[i].end + "\t" + processes[i].wait + "\t" + processes[i].turnAround);
                Console.WriteLine();
            }
            double att = 0, awt = 0;
            awt = (double)totalwait / (double)processes.Count;
            att = (double)totalturnAround / (double)processes.Count;
            Console.WriteLine("A.W.T= {0}", awt + "\t A.T.T= " + att);
            Console.ReadKey();
        }


分类过程

class Process
    {
        public Process(string name, int arrivalTime, int brust, int priority)
        {
            this.name = name;
            this.arrivalTime = arrivalTime;
            this.brust = brust;
            this.priority = priority;
        }
        public Process()
        {

        }
        public string name;
        public int arrivalTime;
        public int brust;
        public int priority;
        public int wait;
        public int end;
        public int start;
        public int turnAround;
    }

1 个答案:

答案 0 :(得分:1)

我建议你看一下Parallel Sort Algorithm的灵感。

我也假设您需要某种帮助来实现它,这与上面的解决方案类似

// The contents of processes must implement IComparable<T>
QuicksortParallelOptimised(processes, 0, processes.Count);

但请尽量说明一个明确的问题:)

将IComparable接口的定义添加到您的流程类;

partial class Process : IComparable<Process>
{
  public override int CompareTo(Process otherProcess) 
  {
    if (this.arrivalTime == otherProcess.arrivalTime)
      return this.brust.CompareTo(otherProcess.brust);
    return this.arrivalTime.CompareTo(otherProcess.brust);
  }
}

执行我到目前为止所说的内容会让您进行一轮排序,这也会让您的代码更具可读性:)

如果您有任何问题,请询问:)