# First-come first-serve (FCFS) algorithm

I want your suggestions on this algorithm:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FCFS_Console
{
class Program
{
static void Main(string[] args)
{
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("_________________________________________________________________");
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
}
//   ----------------------------------------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 || (processes[k].arrivalTime == processes[i].arrivalTime && processes[k].brust > processes[i].brust))
{
temp = processes[i];
processes[i] = processes[k];
processes[k] = temp;
}
}
}
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= " + awt + "\t A.T.T= " + att);
}
public static void printSpaces(int counter)
{
for (int i = 0; i < counter; i++)
{
Console.Write(" ");
}
}
public static void printHashes(int brust)
{
for (int i = 0; i < brust; i++)
{
Console.Write("#");
}
}

}
}


Process Class

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

namespace FCFS_Console
{
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;
}
}


Procedure:

1. created the Process class
2. received my input from a text file
3. used a list to store my processes
4. created objects from the class
5. sorted the list
6. printed the Gantt Chart
7. completed the rest of the process data like start, end, turn around, wait
8. printed the final table
9. calculated the average wait time and the average turn around time
-

//Don't use this code.  Bubble Sort is slow.
for (int k = 0; k < processes.Count; k++)
{
for (int i = k + 1; i < processes.Count; i++)
{
if (processes[k].arrivalTime > processes[i].arrivalTime ||
(processes[k].arrivalTime == processes[i].arrivalTime &&
processes[k].brust > processes[i].brust))
{
temp = processes[i];
processes[i] = processes[k];
processes[k] = temp;
}
}
}


Bubble sort is very slow when run on medium or large lists. I recommend using a faster sort algorithm (e.g., Quick Sort), or using C#'s built-in sorting functions (e.g., the OrderBy extension method). If you prefer to minimize how much you need to change your existing code, the code for Comb Sort is almost identical to that of bubble sort, while still running significantly faster. That said, Comb sort is a bit less popular (and thus less well-understood) than other algorithms of similar efficiency.

You may want extract each step in your algorithm (Sort, Chart, etc.) into a separate method.

If speed is important to you, a profiler will tell you which step is worth optimizing first.

-
could u show me how to use comb sort or quick sort –  Ayman Sharaf Dec 20 '12 at 13:04
The C implementation of comb sort on wikipedia should be pretty easy to convert to C# code: en.wikipedia.org/w/… . The difference between comb sort and bubble sort is that the initial iterations of comb sort swaps elements that are far apart whereas in bubble sort every iteration swaps only adjacent elements. –  Brian Dec 20 '12 at 14:02

Here is a sample solution you can use to improve you code,. I have created a simple processor class to do processing logic in which input string will be passed from you console application and processor will generate the according to configured writer. (This code can be re factored too).let me know if you have doubts.

    public interface IWriter
{
void WriteMessage(string msg);
}

public class ConsoleWriter : IWriter
{
#region Implementation of IWriter

public void WriteMessage(string msg)
{
Console.WriteLine(msg);
}

#endregion
}

public class Processor
{
private IEnumerable<string> _lines;
private IEnumerable<Process> _processes;
private IWriter _writer;

public Processor(string inputText)
{
_processes=new List<Process>();
_inputText = inputText;
_lines = inputText.Split('\n');
InitializeComponent();
}

private void InitializeComponent()
{
_processes= _lines.Select(line => line.Split('\t')).
ToList().
Select(elem => new Process(elem[0], int.Parse(elem[1]),
int.Parse(elem[2]),
int.Parse(elem[3])));

}

public void Process(IWriter writer)
{
_writer = writer;
Sort();
PrepareGantt();
CompletingDataAndfinalTable();
}

private void CompletingDataAndfinalTable()
{

}

private void PrepareGantt()
{
}

private void Sort()
{
}
}


Actually what i understood from your code is that you want two thing :

1. Processing the string in different format
2. Displaying the result.

For this reason I have abstracted out the processor and logger code.

-
looks good but thats not very different i want to improve the algorithm if possible if u could help me i'd be thankful –  Ayman Sharaf Dec 19 '12 at 18:42
what you want to improve in algo improvement? cleaner code, timing issues, give me more insights on the same –  paritosh Dec 19 '12 at 18:51
I Want it To Be Cleaner And Faster.especially faster is more important to me –  Ayman Sharaf Dec 19 '12 at 21:00

s + @"\input.txt"

use Path.Combine.

According to StyleCop (and I agree), using blocks should be on the inside of the namespace declaration.

FileText should be camelCase.

You should not Split('\n'). Use File.ReadLines.

Wherever appropriate, you should be using foreach instead of for with a counter.

You should not implement your own sorting algorithm. The built-in .NET sorting methods should be used.

printSpaces should be replaced by printing new String(' ', counter) (similar for printHashes).

-