-1

Thats my code . I want to use a faster sorting algorithm maybe quick sort or comb sort.
i sorted the list twice first according to arrival then to brust time.
i need help implementing a faster sorting algorithm
My main mwthod

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

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;
    }
6
  • 2
    What is your question here? Commented Dec 20, 2012 at 13:22
  • Are you braging or do you have a question that goes with your code??? or is it simple: i want to use quick sort? Commented Dec 20, 2012 at 13:22
  • 1
    List.Sort uses quick sort witch makes O(n log n) comparisons Commented Dec 20, 2012 at 13:24
  • list.sort wouldn't be efficient here because i need to sort twice first for arrival and second for brust. Commented Dec 20, 2012 at 13:31
  • For reference, this question seems to be in response to my answer to Ayman's previous question on CodeReview.SE: codereview.stackexchange.com/a/19788/8243 . Commented Dec 20, 2012 at 19:08

1 Answer 1

1

I would recommend you to have look at Parallel Sort Algorithm for inspiration.

I am also assuming that you want some kind of help implementing it, which with the above solution would be something like

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

But do please try to state a clear question :)

Adding the definition of the IComparable interface to your process-class;

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

Doing what I've stated so far would leave you with one round of sorting, and it would also make your code a hell of a lot more readable :)

If you have any questions just ask :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.