Skip to content

Commit 4ce406f

Browse files
committed
Add NonPreemptivePriorityScheduling new algorithm with unit tests
1 parent 842ff52 commit 4ce406f

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
public class NonPreemptivePrioritySchedulingTest {
7+
8+
@Test
9+
public void testScheduleProcesses() {
10+
NonPreemptivePriorityScheduling.Process[] processes = {
11+
new NonPreemptivePriorityScheduling.Process(1, 10, 2),
12+
new NonPreemptivePriorityScheduling.Process(2, 5, 1),
13+
new NonPreemptivePriorityScheduling.Process(3, 8, 3)
14+
};
15+
16+
NonPreemptivePriorityScheduling.Process[] expectedOrder = {
17+
new NonPreemptivePriorityScheduling.Process(2, 5, 1),
18+
new NonPreemptivePriorityScheduling.Process(1, 10, 2),
19+
new NonPreemptivePriorityScheduling.Process(3, 8, 3)
20+
};
21+
22+
NonPreemptivePriorityScheduling.Process[] actualOrder = NonPreemptivePriorityScheduling
23+
.scheduleProcesses(processes);
24+
25+
assertArrayEquals(expectedOrder, actualOrder, "Processes should be scheduled by priority.");
26+
}
27+
28+
@Test
29+
public void testCalculateAverageWaitingTime() {
30+
NonPreemptivePriorityScheduling.Process[] processes = {
31+
new NonPreemptivePriorityScheduling.Process(1, 10, 2),
32+
new NonPreemptivePriorityScheduling.Process(2, 5, 1),
33+
new NonPreemptivePriorityScheduling.Process(3, 8, 3)
34+
};
35+
36+
NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling
37+
.scheduleProcesses(processes);
38+
39+
double expectedAvgWaitingTime = (0 + 10 + 15) / 3.0; // (0 + 10 + (10 + 8)) / 3
40+
double actualAvgWaitingTime = NonPreemptivePriorityScheduling.calculateAverageWaitingTime(processes,
41+
executionOrder);
42+
43+
assertEquals(expectedAvgWaitingTime, actualAvgWaitingTime, 0.01,
44+
"Average waiting time should be calculated correctly.");
45+
}
46+
47+
@Test
48+
public void testCalculateAverageTurnaroundTime() {
49+
NonPreemptivePriorityScheduling.Process[] processes = {
50+
new NonPreemptivePriorityScheduling.Process(1, 10, 2),
51+
new NonPreemptivePriorityScheduling.Process(2, 5, 1),
52+
new NonPreemptivePriorityScheduling.Process(3, 8, 3)
53+
};
54+
55+
NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling
56+
.scheduleProcesses(processes);
57+
58+
double expectedAvgTurnaroundTime = (5 + 15 + 23) / 3.0; // (5 + (5 + 10) + (5 + 10 + 8)) / 3
59+
double actualAvgTurnaroundTime = NonPreemptivePriorityScheduling.calculateAverageTurnaroundTime(processes,
60+
executionOrder);
61+
62+
assertEquals(expectedAvgTurnaroundTime, actualAvgTurnaroundTime, 0.01,
63+
"Average turnaround time should be calculated correctly.");
64+
}
65+
}

0 commit comments

Comments
 (0)