patterncsharpMinor
First-come first-serve (FCFS) algorithm
Viewed 0 times
fcfscomealgorithmfirstserve
Problem
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)
{
//----------------------------------------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 processes = new List();
for (int i = 1; i 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 clock)
{
processes[i].start = processes[i].arrivalTime;
```
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)
{
//----------------------------------------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 processes = new List();
for (int i = 1; i 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 clock)
{
processes[i].start = processes[i].arrivalTime;
Solution
//Don't use this code. Bubble Sort is slow.
for (int k = 0; k 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.
Code Snippets
//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;
}
}
}Context
StackExchange Code Review Q#19755, answer score: 2
Revisions (0)
No revisions yet.