|
| 1 | +package com.thealgorithms.scheduling; |
| 2 | + |
| 3 | +import java.util.PriorityQueue; |
| 4 | + |
| 5 | +/** |
| 6 | + * This class implements the Non-Preemptive Priority Scheduling algorithm. |
| 7 | + * Processes are executed in order of their priority. The process with the |
| 8 | + * highest priority (lower priority number) is executed first, and once a process starts |
| 9 | + * executing, |
| 10 | + * it cannot be preempted. |
| 11 | + */ |
| 12 | +public class NonPreemptivePriorityScheduling { |
| 13 | + |
| 14 | + static class Process implements Comparable<Process> { |
| 15 | + int id; // Process ID |
| 16 | + int burstTime; // Time required by the process for execution |
| 17 | + int priority; // Priority of the process (lower value indicates higher priority) |
| 18 | + |
| 19 | + public Process(int id, int burstTime, int priority) { |
| 20 | + this.id = id; |
| 21 | + this.burstTime = burstTime; |
| 22 | + this.priority = priority; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Compare based on priority for scheduling. The process with the lowest |
| 27 | + * priority is selected first. |
| 28 | + */ |
| 29 | + @Override |
| 30 | + public int compareTo(Process other) { |
| 31 | + return Integer.compare(this.priority, other.priority); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Schedules processes based on their priority in a non-preemptive manner. |
| 37 | + * |
| 38 | + * @param processes Array of processes to be scheduled. |
| 39 | + * @return Array of processes in the order they are executed. |
| 40 | + */ |
| 41 | + public static Process[] scheduleProcesses(Process[] processes) { |
| 42 | + PriorityQueue<Process> pq = new PriorityQueue<>(); |
| 43 | + for (Process process : processes) { |
| 44 | + pq.add(process); |
| 45 | + } |
| 46 | + |
| 47 | + Process[] executionOrder = new Process[processes.length]; |
| 48 | + int index = 0; |
| 49 | + |
| 50 | + // Execute processes based on their priority |
| 51 | + while (!pq.isEmpty()) { |
| 52 | + executionOrder[index++] = pq.poll(); // Poll the process with the highest priority |
| 53 | + } |
| 54 | + |
| 55 | + return executionOrder; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Calculates the average waiting time of the processes. |
| 60 | + * |
| 61 | + * @param processes Array of processes. |
| 62 | + * @param executionOrder Array of processes in execution order. |
| 63 | + * @return Average waiting time. |
| 64 | + */ |
| 65 | + public static double calculateAverageWaitingTime(Process[] processes, Process[] executionOrder) { |
| 66 | + int totalWaitingTime = 0; |
| 67 | + int currentTime = 0; |
| 68 | + |
| 69 | + for (Process process : executionOrder) { |
| 70 | + totalWaitingTime += currentTime; |
| 71 | + currentTime += process.burstTime; |
| 72 | + } |
| 73 | + |
| 74 | + return (double) totalWaitingTime / processes.length; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Calculates the average turn-around time of the processes. |
| 79 | + * |
| 80 | + * @param processes Array of processes. |
| 81 | + * @param executionOrder Array of processes in execution order. |
| 82 | + * @return Average turn-around time. |
| 83 | + */ |
| 84 | + public static double calculateAverageTurnaroundTime(Process[] processes, Process[] executionOrder) { |
| 85 | + int totalTurnaroundTime = 0; |
| 86 | + int currentTime = 0; |
| 87 | + |
| 88 | + for (Process process : executionOrder) { |
| 89 | + currentTime += process.burstTime; |
| 90 | + totalTurnaroundTime += currentTime; |
| 91 | + } |
| 92 | + |
| 93 | + return (double) totalTurnaroundTime / processes.length; |
| 94 | + } |
| 95 | + |
| 96 | + public static void main(String[] args) { |
| 97 | + Process[] processes = { |
| 98 | + new Process(1, 10, 2), |
| 99 | + new Process(2, 5, 1), |
| 100 | + new Process(3, 8, 3) |
| 101 | + }; |
| 102 | + |
| 103 | + Process[] executionOrder = scheduleProcesses(processes); |
| 104 | + |
| 105 | + System.out.println("Process ID | Burst Time | Priority"); |
| 106 | + for (Process process : executionOrder) { |
| 107 | + System.out.printf("%d %d %d%n", process.id, process.burstTime, process.priority); |
| 108 | + } |
| 109 | + |
| 110 | + System.out.printf("Average Waiting Time: %.2f%n", calculateAverageWaitingTime(processes, executionOrder)); |
| 111 | + System.out.printf("Average Turnaround Time: %.2f%n", calculateAverageTurnaroundTime(processes, executionOrder)); |
| 112 | + } |
| 113 | +} |
0 commit comments