Skip to content

Commit 69a503e

Browse files
committed
Fix
1 parent 5daeb23 commit 69a503e

File tree

2 files changed

+46
-25
lines changed

2 files changed

+46
-25
lines changed

src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,26 @@ private NonPreemptivePriorityScheduling() {
1414
}
1515

1616
/**
17-
* Represents a process with an ID, burst time, priority, and start time.
17+
* Represents a process with an ID, burst time, priority, arrival time, and start time.
1818
*/
1919
static class Process implements Comparable<Process> {
2020
int id;
21+
int arrivalTime;
2122
int startTime;
2223
int burstTime;
2324
int priority;
2425

2526
/**
2627
* Constructs a Process instance with the specified parameters.
2728
*
28-
* @param id Unique identifier for the process
29-
* @param burstTime Time required for the process execution
30-
* @param priority Priority of the process
29+
* @param id Unique identifier for the process
30+
* @param arrivalTime Time when the process arrives in the system
31+
* @param burstTime Time required for the process execution
32+
* @param priority Priority of the process
3133
*/
32-
Process(int id, int burstTime, int priority) {
34+
Process(int id, int arrivalTime, int burstTime, int priority) {
3335
this.id = id;
36+
this.arrivalTime = arrivalTime;
3437
this.startTime = -1;
3538
this.burstTime = burstTime;
3639
this.priority = priority;
@@ -39,35 +42,44 @@ static class Process implements Comparable<Process> {
3942
/**
4043
* Compare based on priority for scheduling. The process with the lowest
4144
* priority is selected first.
45+
* If two processes have the same priority, the one that arrives earlier is selected.
4246
*
4347
* @param other The other process to compare against
4448
* @return A negative integer, zero, or a positive integer as this process
4549
* is less than, equal to, or greater than the specified process.
4650
*/
4751
@Override
4852
public int compareTo(Process other) {
53+
if (this.priority == other.priority) {
54+
return Integer.compare(this.arrivalTime, other.arrivalTime);
55+
}
4956
return Integer.compare(this.priority, other.priority);
5057
}
5158
}
5259

5360
/**
54-
* Schedules processes based on their priority in a non-preemptive manner.
61+
* Schedules processes based on their priority in a non-preemptive manner, considering their arrival times.
5562
*
5663
* @param processes Array of processes to be scheduled.
5764
* @return Array of processes in the order they are executed.
5865
*/
5966
public static Process[] scheduleProcesses(Process[] processes) {
6067
PriorityQueue<Process> pq = new PriorityQueue<>();
68+
int currentTime = 0;
69+
int index = 0;
70+
Process[] executionOrder = new Process[processes.length];
71+
6172
for (Process process : processes) {
6273
pq.add(process);
6374
}
6475

65-
Process[] executionOrder = new Process[processes.length];
66-
int index = 0;
67-
int currentTime = 0;
68-
6976
while (!pq.isEmpty()) {
7077
Process currentProcess = pq.poll();
78+
79+
if (currentTime < currentProcess.arrivalTime) {
80+
currentTime = currentProcess.arrivalTime;
81+
}
82+
7183
currentProcess.startTime = currentTime;
7284
executionOrder[index++] = currentProcess;
7385
currentTime += currentProcess.burstTime;
@@ -85,11 +97,10 @@ public static Process[] scheduleProcesses(Process[] processes) {
8597
*/
8698
public static double calculateAverageWaitingTime(Process[] processes, Process[] executionOrder) {
8799
int totalWaitingTime = 0;
88-
int currentTime = 0;
89100

90101
for (Process process : executionOrder) {
91-
totalWaitingTime += currentTime;
92-
currentTime += process.burstTime;
102+
int waitingTime = process.startTime - process.arrivalTime;
103+
totalWaitingTime += waitingTime;
93104
}
94105

95106
return (double) totalWaitingTime / processes.length;
@@ -104,11 +115,10 @@ public static double calculateAverageWaitingTime(Process[] processes, Process[]
104115
*/
105116
public static double calculateAverageTurnaroundTime(Process[] processes, Process[] executionOrder) {
106117
int totalTurnaroundTime = 0;
107-
int currentTime = 0;
108118

109119
for (Process process : executionOrder) {
110-
currentTime += process.burstTime;
111-
totalTurnaroundTime += currentTime;
120+
int turnaroundTime = (process.startTime + process.burstTime) - process.arrivalTime;
121+
totalTurnaroundTime += turnaroundTime;
112122
}
113123

114124
return (double) totalTurnaroundTime / processes.length;
Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,53 @@
11
package com.thealgorithms.scheduling;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.assertNotEquals;
54

65
import org.junit.jupiter.api.Test;
76

87
public class NonPreemptivePrioritySchedulingTest {
98

109
@Test
1110
public void testCalculateAverageWaitingTime() {
12-
NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 10, 2), new NonPreemptivePriorityScheduling.Process(2, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 8, 3)};
11+
NonPreemptivePriorityScheduling.Process[] processes = {
12+
new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority
13+
new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1),
14+
new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)
15+
};
1316
NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes);
1417

15-
double expectedAvgWaitingTime = 6.666666666666667; // (0 + 10 + 5) / 3
18+
double expectedAvgWaitingTime = (0 + 5 + 15) / 3.0; // Waiting times: 0 for P2, 5 for P1, 15 for P3
1619
double actualAvgWaitingTime = NonPreemptivePriorityScheduling.calculateAverageWaitingTime(processes, executionOrder);
1720

1821
assertEquals(expectedAvgWaitingTime, actualAvgWaitingTime, 0.01, "Average waiting time should be calculated correctly.");
1922
}
2023

2124
@Test
2225
public void testCalculateAverageTurnaroundTime() {
23-
NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 10, 2), new NonPreemptivePriorityScheduling.Process(2, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 8, 3)};
26+
NonPreemptivePriorityScheduling.Process[] processes = {
27+
new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority
28+
new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1),
29+
new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)
30+
};
2431
NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes);
2532

26-
double expectedAvgTurnaroundTime = (5 + 15 + 23) / 3.0; // (5 + (5 + 10) + (5 + 10 + 8)) / 3
33+
double expectedAvgTurnaroundTime = (5 + 15 + 23) / 3.0; // Turnaround times: 5 for P2, 15 for P1, 23 for P3
2734
double actualAvgTurnaroundTime = NonPreemptivePriorityScheduling.calculateAverageTurnaroundTime(processes, executionOrder);
2835

2936
assertEquals(expectedAvgTurnaroundTime, actualAvgTurnaroundTime, 0.01, "Average turnaround time should be calculated correctly.");
3037
}
3138

3239
@Test
3340
public void testStartTimeIsCorrect() {
34-
NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 10, 2), new NonPreemptivePriorityScheduling.Process(2, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 8, 3)};
41+
NonPreemptivePriorityScheduling.Process[] processes = {
42+
new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority
43+
new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1),
44+
new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)
45+
};
3546
NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes);
3647

3748
// Check that the start time for each process is correctly set
38-
assertEquals(0, executionOrder[0].startTime, "First process should start at time 0.");
39-
assertEquals(5, executionOrder[1].startTime, "Second process should start after the first process.");
40-
assertEquals(15, executionOrder[2].startTime, "Third process should start after the second process.");
49+
assertEquals(0, executionOrder[0].startTime, "First process (P2) should start at time 0."); // Process 2 has the highest priority
50+
assertEquals(5, executionOrder[1].startTime, "Second process (P1) should start at time 5.");
51+
assertEquals(15, executionOrder[2].startTime, "Third process (P3) should start at time 15.");
4152
}
4253
}

0 commit comments

Comments
 (0)