Skip to content

Commit de50d45

Browse files
committed
Fix
1 parent 34920f7 commit de50d45

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.thealgorithms.scheduling;
22

3+
import java.util.LinkedList;
34
import java.util.PriorityQueue;
5+
import java.util.Queue;
46

57
/**
68
* This class implements the Non-Preemptive Priority Scheduling algorithm.
@@ -65,24 +67,30 @@ public int compareTo(Process other) {
6567
*/
6668
public static Process[] scheduleProcesses(Process[] processes) {
6769
PriorityQueue<Process> pq = new PriorityQueue<>();
70+
Queue<Process> waitingQueue = new LinkedList<>();
6871
int currentTime = 0;
6972
int index = 0;
7073
Process[] executionOrder = new Process[processes.length];
7174

7275
for (Process process : processes) {
73-
pq.add(process);
76+
waitingQueue.add(process);
7477
}
7578

76-
while (!pq.isEmpty()) {
77-
Process currentProcess = pq.poll();
78-
79-
if (currentTime < currentProcess.arrivalTime) {
80-
currentTime = currentProcess.arrivalTime;
79+
while (!waitingQueue.isEmpty() || !pq.isEmpty()) {
80+
// Add processes that have arrived to the priority queue
81+
while (!waitingQueue.isEmpty() && waitingQueue.peek().arrivalTime <= currentTime) {
82+
pq.add(waitingQueue.poll());
8183
}
8284

83-
currentProcess.startTime = currentTime;
84-
executionOrder[index++] = currentProcess;
85-
currentTime += currentProcess.burstTime;
85+
if (!pq.isEmpty()) {
86+
Process currentProcess = pq.poll();
87+
currentProcess.startTime = currentTime;
88+
executionOrder[index++] = currentProcess;
89+
currentTime += currentProcess.burstTime;
90+
} else {
91+
// If no process is ready, move to the next arrival time
92+
currentTime = waitingQueue.peek().arrivalTime;
93+
}
8694
}
8795

8896
return executionOrder;

src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class NonPreemptivePrioritySchedulingTest {
99
@Test
1010
public void testCalculateAverageWaitingTime() {
1111
NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority
12-
new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)};
12+
new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)};
1313
NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes);
1414

1515
double expectedAvgWaitingTime = (0 + 5 + 15) / 3.0; // Waiting times: 0 for P2, 5 for P1, 15 for P3
@@ -21,7 +21,8 @@ public void testCalculateAverageWaitingTime() {
2121
@Test
2222
public void testCalculateAverageTurnaroundTime() {
2323
NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority
24-
new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)};
24+
new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)};
25+
2526
NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes);
2627

2728
double expectedAvgTurnaroundTime = (5 + 15 + 23) / 3.0; // Turnaround times: 5 for P2, 15 for P1, 23 for P3
@@ -32,13 +33,43 @@ public void testCalculateAverageTurnaroundTime() {
3233

3334
@Test
3435
public void testStartTimeIsCorrect() {
35-
NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority
36-
new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)};
36+
NonPreemptivePriorityScheduling.Process[] processes = {
37+
new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority
38+
new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)};
39+
3740
NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes);
3841

3942
// Check that the start time for each process is correctly set
4043
assertEquals(0, executionOrder[0].startTime, "First process (P2) should start at time 0."); // Process 2 has the highest priority
4144
assertEquals(5, executionOrder[1].startTime, "Second process (P1) should start at time 5.");
4245
assertEquals(15, executionOrder[2].startTime, "Third process (P3) should start at time 15.");
4346
}
47+
48+
@Test
49+
public void testWithDelayedArrivalTimes() {
50+
NonPreemptivePriorityScheduling.Process[] processes = {
51+
new NonPreemptivePriorityScheduling.Process(1, 0, 4, 1), // id, arrivalTime, burstTime, priority
52+
new NonPreemptivePriorityScheduling.Process(2, 2, 3, 2), new NonPreemptivePriorityScheduling.Process(3, 4, 2, 3)};
53+
54+
NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes);
55+
56+
// Test the start times considering delayed arrivals
57+
assertEquals(0, executionOrder[0].startTime, "First process (P1) should start at time 0.");
58+
assertEquals(4, executionOrder[1].startTime, "Second process (P2) should start at time 4."); // After P1 finishes
59+
assertEquals(7, executionOrder[2].startTime, "Third process (P3) should start at time 7."); // After P2 finishes
60+
}
61+
62+
@Test
63+
public void testWithGapsInArrivals() {
64+
NonPreemptivePriorityScheduling.Process[] processes = {
65+
new NonPreemptivePriorityScheduling.Process(1, 0, 6, 2), // id, arrivalTime, burstTime, priority
66+
new NonPreemptivePriorityScheduling.Process(2, 8, 4, 1), new NonPreemptivePriorityScheduling.Process(3, 12, 5, 3)};
67+
68+
NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes);
69+
70+
// Test the start times for processes with gaps in arrival times
71+
assertEquals(0, executionOrder[0].startTime, "First process (P1) should start at time 0.");
72+
assertEquals(8, executionOrder[1].startTime, "Second process (P2) should start at time 8."); // After P1 finishes, arrives at 8
73+
assertEquals(12, executionOrder[2].startTime, "Third process (P3) should start at time 12."); // After P2 finishes, arrives at 12
74+
}
4475
}

0 commit comments

Comments
 (0)