Im trying to sort a list by their arrival time this is my co

I\'m trying to sort a list by their arrival time, this is my code . I believe the process should arrive in this order. I\'m trying to implement FCFS I provided the correct results for it and please try to give code in c#

P1 > P5 > P3 > P6 >P8 >P4 >P2 >P7

So please help me fix my sortlist function to do the following sort the list by the arrival time from earliest to latest

heres my code

public partial class Form1 : Form
{
void initialize_process()
{
List<Process> processes = new List<Process>();
Process p1 = new Process();
p1.arrival = 4;
p1.name = \"P1\";
p1.cpuburst = 38;
p1.ioburst = 314;
processes.Add(p1);

Process p2 = new Process();
p2.arrival = 18;
p2.name = \"P2\";
p2.cpuburst = 149;
p2.ioburst = 324;
processes.Add(p2);

Process p3 = new Process();
p3.arrival = 6;
p3.name = \"P3\";
p3.cpuburst = 52;
p3.ioburst = 170;
processes.Add(p3);

Process p4 = new Process();
p4.arrival = 17;
p3.name = \"P4\";
p3.cpuburst = 129;
p3.ioburst = 408;
processes.Add(p4);

Process p5 = new Process();
p4.arrival = 5;
p3.name = \"P5\";
p3.cpuburst = 41;
p3.ioburst = 588;
processes.Add(p5);

Process p6 = new Process();
p4.arrival = 10;
p3.name = \"P6\";
p3.cpuburst = 86;
p3.ioburst = 211;
processes.Add(p6);

Process p7 = new Process();
p4.arrival = 21;
p3.name = \"P7\";
p3.cpuburst = 131;
p3.ioburst = 239;
processes.Add(p7);

Process p8 = new Process();
p4.arrival = 11;
p3.name = \"P8\";
p3.cpuburst = 113;
p3.ioburst = 252;
processes.Add(p8);

sortlist(processes);


  


// FCFS(p1, p2, p3, p4, p5, p6, p7, p8);


}
void sortlist(List<Process> processes)
{
// SORTS LIST
Process temp;
for (int k = 0; k < processes.Count; k++)
{
for (int i = k + 1; i < processes.Count; i++)
{
if (processes[k].arrival > processes[i].arrival || (processes[k].arrival == processes[i].arrival && processes[k].cpuburst > processes[i].cpuburst))
{
temp = processes[i];
processes[i] = processes[k];
processes[k] = temp;
}
}
}

outputlist(processes);
}
void outputlist(List<Process> processes)
{
// Outputs to the listbox
for (int i = 0; i < processes.Count; i++)
{
output.Items.Add(processes[i].name + \"\\t\" + processes[i].arrival + \"\\t\" + processes[i].cpuburst);

}

}
void FCFS(Process p1, Process p2, Process p3, Process p4, Process p5, Process p6, Process p7, Process p8)
{
  
  
}
public Form1()
{
InitializeComponent();
}

private void calculate_Click(object sender, EventArgs e)
{
initialize_process();

}
}
}

----------------- class code

class Process
{
public Process(string name, int arrival, int cpuburst,int ioburst, int priority)
{
this.name = name;
this.arrival = arrival;
this.cpuburst = cpuburst;
this.ioburst = ioburst;
this.priority = priority;
}
public Process()
{

}
public string name;
public int arrival;
public int cpuburst;
public int ioburst;
public int priority;
public int wait;
// public int end;
//public int start;
public int turnAround;
  
}
}

Solution

void sortlist(List<Process> processes)
{
// SORTS LIST
int temp;

for (int i = 0; i < processes.Count; i++)
{
for (int j = 0; j < processes.Count-1; j++)
{
if (processes[j].arrival > processes[j+1].arrival)
{
temp = processes[j];
processes[j] = processes[j+1];
processes[j+1] = temp;
}
}
}

// This will sort the lost in ascending order on the basis of arrival time

I\'m trying to sort a list by their arrival time, this is my code . I believe the process should arrive in this order. I\'m trying to implement FCFS I provided
I\'m trying to sort a list by their arrival time, this is my code . I believe the process should arrive in this order. I\'m trying to implement FCFS I provided
I\'m trying to sort a list by their arrival time, this is my code . I believe the process should arrive in this order. I\'m trying to implement FCFS I provided

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site