C#: first-come-first-served versus shortest-job-first

A CPU scheduler chooses which ready job runs next. In first-come, first-served (FCFS) scheduling, jobs run in the order they arrive. In shortest-job-first (SJF) scheduling, the job with the smallest CPU burst runs next. A job's waiting time is the time it spends in the ready queue before it first runs.

C#
using System;
using System.Globalization;
using System.Linq;

class Program {
  static double AverageWait(int[] bursts) {
    int clock = 0, totalWait = 0;
    foreach (int burst in bursts) {
      totalWait += clock;
      clock += burst;
    }
    return (double)totalWait / bursts.Length;
  }

  static void Main() {
    int[] bursts = { 8, 4, 9, 5 };
    Console.WriteLine("FCFS average wait: " + AverageWait(bursts).ToString("F2", CultureInfo.InvariantCulture));
    int[] shortestFirst = bursts.OrderBy(b => b).ToArray();
    Console.WriteLine("SJF  average wait: " + AverageWait(shortestFirst).ToString("F2", CultureInfo.InvariantCulture));
  }
}
Read the full lesson