From 4ce406f2ddd00df9d3678e833d1e88c9d224d93b Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Thu, 3 Oct 2024 10:42:32 +0530 Subject: [PATCH 01/23] Add NonPreemptivePriorityScheduling new algorithm with unit tests --- .../NonPreemptivePriorityScheduling.java | 113 ++++++++++++++++++ .../NonPreemptivePrioritySchedulingTest.java | 65 ++++++++++ 2 files changed, 178 insertions(+) create mode 100644 src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java create mode 100644 src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java diff --git a/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java b/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java new file mode 100644 index 000000000000..1b2283bde378 --- /dev/null +++ b/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java @@ -0,0 +1,113 @@ +package com.thealgorithms.scheduling; + +import java.util.PriorityQueue; + +/** + * This class implements the Non-Preemptive Priority Scheduling algorithm. + * Processes are executed in order of their priority. The process with the + * highest priority (lower priority number) is executed first, and once a process starts + * executing, + * it cannot be preempted. + */ +public class NonPreemptivePriorityScheduling { + + static class Process implements Comparable { + int id; // Process ID + int burstTime; // Time required by the process for execution + int priority; // Priority of the process (lower value indicates higher priority) + + public Process(int id, int burstTime, int priority) { + this.id = id; + this.burstTime = burstTime; + this.priority = priority; + } + + /** + * Compare based on priority for scheduling. The process with the lowest + * priority is selected first. + */ + @Override + public int compareTo(Process other) { + return Integer.compare(this.priority, other.priority); + } + } + + /** + * Schedules processes based on their priority in a non-preemptive manner. + * + * @param processes Array of processes to be scheduled. + * @return Array of processes in the order they are executed. + */ + public static Process[] scheduleProcesses(Process[] processes) { + PriorityQueue pq = new PriorityQueue<>(); + for (Process process : processes) { + pq.add(process); + } + + Process[] executionOrder = new Process[processes.length]; + int index = 0; + + // Execute processes based on their priority + while (!pq.isEmpty()) { + executionOrder[index++] = pq.poll(); // Poll the process with the highest priority + } + + return executionOrder; + } + + /** + * Calculates the average waiting time of the processes. + * + * @param processes Array of processes. + * @param executionOrder Array of processes in execution order. + * @return Average waiting time. + */ + public static double calculateAverageWaitingTime(Process[] processes, Process[] executionOrder) { + int totalWaitingTime = 0; + int currentTime = 0; + + for (Process process : executionOrder) { + totalWaitingTime += currentTime; + currentTime += process.burstTime; + } + + return (double) totalWaitingTime / processes.length; + } + + /** + * Calculates the average turn-around time of the processes. + * + * @param processes Array of processes. + * @param executionOrder Array of processes in execution order. + * @return Average turn-around time. + */ + public static double calculateAverageTurnaroundTime(Process[] processes, Process[] executionOrder) { + int totalTurnaroundTime = 0; + int currentTime = 0; + + for (Process process : executionOrder) { + currentTime += process.burstTime; + totalTurnaroundTime += currentTime; + } + + return (double) totalTurnaroundTime / processes.length; + } + + public static void main(String[] args) { + Process[] processes = { + new Process(1, 10, 2), + new Process(2, 5, 1), + new Process(3, 8, 3) + }; + + Process[] executionOrder = scheduleProcesses(processes); + + System.out.println("Process ID | Burst Time | Priority"); + for (Process process : executionOrder) { + System.out.printf("%d %d %d%n", process.id, process.burstTime, process.priority); + } + + System.out.printf("Average Waiting Time: %.2f%n", calculateAverageWaitingTime(processes, executionOrder)); + System.out.printf("Average Turnaround Time: %.2f%n", calculateAverageTurnaroundTime(processes, executionOrder)); + } +} diff --git a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java new file mode 100644 index 000000000000..5aa6afb1a0ab --- /dev/null +++ b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java @@ -0,0 +1,65 @@ +package com.thealgorithms.scheduling; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class NonPreemptivePrioritySchedulingTest { + + @Test + public void testScheduleProcesses() { + NonPreemptivePriorityScheduling.Process[] processes = { + new NonPreemptivePriorityScheduling.Process(1, 10, 2), + new NonPreemptivePriorityScheduling.Process(2, 5, 1), + new NonPreemptivePriorityScheduling.Process(3, 8, 3) + }; + + NonPreemptivePriorityScheduling.Process[] expectedOrder = { + new NonPreemptivePriorityScheduling.Process(2, 5, 1), + new NonPreemptivePriorityScheduling.Process(1, 10, 2), + new NonPreemptivePriorityScheduling.Process(3, 8, 3) + }; + + NonPreemptivePriorityScheduling.Process[] actualOrder = NonPreemptivePriorityScheduling + .scheduleProcesses(processes); + + assertArrayEquals(expectedOrder, actualOrder, "Processes should be scheduled by priority."); + } + + @Test + public void testCalculateAverageWaitingTime() { + NonPreemptivePriorityScheduling.Process[] processes = { + new NonPreemptivePriorityScheduling.Process(1, 10, 2), + new NonPreemptivePriorityScheduling.Process(2, 5, 1), + new NonPreemptivePriorityScheduling.Process(3, 8, 3) + }; + + NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling + .scheduleProcesses(processes); + + double expectedAvgWaitingTime = (0 + 10 + 15) / 3.0; // (0 + 10 + (10 + 8)) / 3 + double actualAvgWaitingTime = NonPreemptivePriorityScheduling.calculateAverageWaitingTime(processes, + executionOrder); + + assertEquals(expectedAvgWaitingTime, actualAvgWaitingTime, 0.01, + "Average waiting time should be calculated correctly."); + } + + @Test + public void testCalculateAverageTurnaroundTime() { + NonPreemptivePriorityScheduling.Process[] processes = { + new NonPreemptivePriorityScheduling.Process(1, 10, 2), + new NonPreemptivePriorityScheduling.Process(2, 5, 1), + new NonPreemptivePriorityScheduling.Process(3, 8, 3) + }; + + NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling + .scheduleProcesses(processes); + + double expectedAvgTurnaroundTime = (5 + 15 + 23) / 3.0; // (5 + (5 + 10) + (5 + 10 + 8)) / 3 + double actualAvgTurnaroundTime = NonPreemptivePriorityScheduling.calculateAverageTurnaroundTime(processes, + executionOrder); + + assertEquals(expectedAvgTurnaroundTime, actualAvgTurnaroundTime, 0.01, + "Average turnaround time should be calculated correctly."); + } +} From 819e8a96fbf932886604c8a48b27c56792055e2b Mon Sep 17 00:00:00 2001 From: Hardvan Date: Thu, 3 Oct 2024 05:12:47 +0000 Subject: [PATCH 02/23] Update directory --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index ee09790ed64d..195afa9d9d1a 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -452,6 +452,7 @@ * [Verhoeff](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/Verhoeff.java) * scheduling * [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java) + * [NonPreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java) * [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java) * [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java) * [SJFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SJFScheduling.java) @@ -893,6 +894,7 @@ * [WorstFitCPUTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/WorstFitCPUTest.java) * scheduling * [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java) + * [NonPreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java) * [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java) * [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java) * [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java) From 7d6e7776a28bed806894930346f2ff65b4137237 Mon Sep 17 00:00:00 2001 From: Hardvan Date: Thu, 3 Oct 2024 05:13:09 +0000 Subject: [PATCH 03/23] Update directory --- DIRECTORY.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index b20bff009c2d..db6ac6c16f35 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -451,6 +451,8 @@ * [TowerOfHanoi](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/TowerOfHanoi.java) * [TwoPointers](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/TwoPointers.java) * [Verhoeff](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/Verhoeff.java) + * Recursion + * [GenerateSubsets](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java) * scheduling * [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java) * [NonPreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java) @@ -894,6 +896,8 @@ * [TestPrintMatrixInSpiralOrder](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java) * [TwoPointersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TwoPointersTest.java) * [WorstFitCPUTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/WorstFitCPUTest.java) + * Recursion + * [GenerateSubsetsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java) * scheduling * [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java) * [NonPreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java) From 75417431f853ecf9f80fdbdb3974bda1e0403d1a Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Thu, 3 Oct 2024 10:47:22 +0530 Subject: [PATCH 04/23] Fix clang errors --- .../NonPreemptivePrioritySchedulingTest.java | 48 +++++-------------- 1 file changed, 13 insertions(+), 35 deletions(-) diff --git a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java index 5aa6afb1a0ab..897f5fa91310 100644 --- a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java +++ b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java @@ -1,65 +1,43 @@ package com.thealgorithms.scheduling; -import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + public class NonPreemptivePrioritySchedulingTest { @Test public void testScheduleProcesses() { - NonPreemptivePriorityScheduling.Process[] processes = { - new NonPreemptivePriorityScheduling.Process(1, 10, 2), - new NonPreemptivePriorityScheduling.Process(2, 5, 1), - new NonPreemptivePriorityScheduling.Process(3, 8, 3) - }; + NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 10, 2), new NonPreemptivePriorityScheduling.Process(2, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 8, 3)}; - NonPreemptivePriorityScheduling.Process[] expectedOrder = { - new NonPreemptivePriorityScheduling.Process(2, 5, 1), - new NonPreemptivePriorityScheduling.Process(1, 10, 2), - new NonPreemptivePriorityScheduling.Process(3, 8, 3) - }; + NonPreemptivePriorityScheduling.Process[] expectedOrder = {new NonPreemptivePriorityScheduling.Process(2, 5, 1), new NonPreemptivePriorityScheduling.Process(1, 10, 2), new NonPreemptivePriorityScheduling.Process(3, 8, 3)}; - NonPreemptivePriorityScheduling.Process[] actualOrder = NonPreemptivePriorityScheduling - .scheduleProcesses(processes); + NonPreemptivePriorityScheduling.Process[] actualOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); assertArrayEquals(expectedOrder, actualOrder, "Processes should be scheduled by priority."); } @Test public void testCalculateAverageWaitingTime() { - NonPreemptivePriorityScheduling.Process[] processes = { - new NonPreemptivePriorityScheduling.Process(1, 10, 2), - new NonPreemptivePriorityScheduling.Process(2, 5, 1), - new NonPreemptivePriorityScheduling.Process(3, 8, 3) - }; + NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 10, 2), new NonPreemptivePriorityScheduling.Process(2, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 8, 3)}; - NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling - .scheduleProcesses(processes); + NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); double expectedAvgWaitingTime = (0 + 10 + 15) / 3.0; // (0 + 10 + (10 + 8)) / 3 - double actualAvgWaitingTime = NonPreemptivePriorityScheduling.calculateAverageWaitingTime(processes, - executionOrder); + double actualAvgWaitingTime = NonPreemptivePriorityScheduling.calculateAverageWaitingTime(processes, executionOrder); - assertEquals(expectedAvgWaitingTime, actualAvgWaitingTime, 0.01, - "Average waiting time should be calculated correctly."); + assertEquals(expectedAvgWaitingTime, actualAvgWaitingTime, 0.01, "Average waiting time should be calculated correctly."); } @Test public void testCalculateAverageTurnaroundTime() { - NonPreemptivePriorityScheduling.Process[] processes = { - new NonPreemptivePriorityScheduling.Process(1, 10, 2), - new NonPreemptivePriorityScheduling.Process(2, 5, 1), - new NonPreemptivePriorityScheduling.Process(3, 8, 3) - }; + NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 10, 2), new NonPreemptivePriorityScheduling.Process(2, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 8, 3)}; - NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling - .scheduleProcesses(processes); + NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); double expectedAvgTurnaroundTime = (5 + 15 + 23) / 3.0; // (5 + (5 + 10) + (5 + 10 + 8)) / 3 - double actualAvgTurnaroundTime = NonPreemptivePriorityScheduling.calculateAverageTurnaroundTime(processes, - executionOrder); + double actualAvgTurnaroundTime = NonPreemptivePriorityScheduling.calculateAverageTurnaroundTime(processes, executionOrder); - assertEquals(expectedAvgTurnaroundTime, actualAvgTurnaroundTime, 0.01, - "Average turnaround time should be calculated correctly."); + assertEquals(expectedAvgTurnaroundTime, actualAvgTurnaroundTime, 0.01, "Average turnaround time should be calculated correctly."); } } From 27e7dbb13befe0403f6c8ae26b15d8a8254a1d3c Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Thu, 3 Oct 2024 10:48:34 +0530 Subject: [PATCH 05/23] Fix trailing whitespace --- .../scheduling/NonPreemptivePriorityScheduling.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java b/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java index 1b2283bde378..ad4956ac3d6e 100644 --- a/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java @@ -34,7 +34,7 @@ public int compareTo(Process other) { /** * Schedules processes based on their priority in a non-preemptive manner. - * + * * @param processes Array of processes to be scheduled. * @return Array of processes in the order they are executed. */ @@ -57,7 +57,7 @@ public static Process[] scheduleProcesses(Process[] processes) { /** * Calculates the average waiting time of the processes. - * + * * @param processes Array of processes. * @param executionOrder Array of processes in execution order. * @return Average waiting time. @@ -76,7 +76,7 @@ public static double calculateAverageWaitingTime(Process[] processes, Process[] /** * Calculates the average turn-around time of the processes. - * + * * @param processes Array of processes. * @param executionOrder Array of processes in execution order. * @return Average turn-around time. @@ -94,11 +94,7 @@ public static double calculateAverageTurnaroundTime(Process[] processes, Process } public static void main(String[] args) { - Process[] processes = { - new Process(1, 10, 2), - new Process(2, 5, 1), - new Process(3, 8, 3) - }; + Process[] processes = {new Process(1, 10, 2), new Process(2, 5, 1), new Process(3, 8, 3)}; Process[] executionOrder = scheduleProcesses(processes); From b4c1d7eebe3744c702692bae89886420495b317f Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Thu, 3 Oct 2024 10:51:33 +0530 Subject: [PATCH 06/23] Fix --- .../scheduling/NonPreemptivePrioritySchedulingTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java index 897f5fa91310..f3bdacf8ada4 100644 --- a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java +++ b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java @@ -23,7 +23,7 @@ public void testCalculateAverageWaitingTime() { NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); - double expectedAvgWaitingTime = (0 + 10 + 15) / 3.0; // (0 + 10 + (10 + 8)) / 3 + double expectedAvgWaitingTime = 6.666666666666667 double actualAvgWaitingTime = NonPreemptivePriorityScheduling.calculateAverageWaitingTime(processes, executionOrder); assertEquals(expectedAvgWaitingTime, actualAvgWaitingTime, 0.01, "Average waiting time should be calculated correctly."); From f22f2f68dd8da86771a1d49759d5f4facdd7787a Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Thu, 3 Oct 2024 10:53:08 +0530 Subject: [PATCH 07/23] Fix semicolon --- .../scheduling/NonPreemptivePrioritySchedulingTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java index f3bdacf8ada4..386d0a96bf94 100644 --- a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java +++ b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java @@ -23,7 +23,7 @@ public void testCalculateAverageWaitingTime() { NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); - double expectedAvgWaitingTime = 6.666666666666667 + double expectedAvgWaitingTime = 6.666666666666667; double actualAvgWaitingTime = NonPreemptivePriorityScheduling.calculateAverageWaitingTime(processes, executionOrder); assertEquals(expectedAvgWaitingTime, actualAvgWaitingTime, 0.01, "Average waiting time should be calculated correctly."); From b841d57ee4a4a023ddae5a2f262c3c6636639fe0 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Thu, 3 Oct 2024 10:57:07 +0530 Subject: [PATCH 08/23] Fine tune test --- .../NonPreemptivePrioritySchedulingTest.java | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java index 386d0a96bf94..b14553bf17ec 100644 --- a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java +++ b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java @@ -6,17 +6,6 @@ public class NonPreemptivePrioritySchedulingTest { - @Test - public void testScheduleProcesses() { - NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 10, 2), new NonPreemptivePriorityScheduling.Process(2, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 8, 3)}; - - NonPreemptivePriorityScheduling.Process[] expectedOrder = {new NonPreemptivePriorityScheduling.Process(2, 5, 1), new NonPreemptivePriorityScheduling.Process(1, 10, 2), new NonPreemptivePriorityScheduling.Process(3, 8, 3)}; - - NonPreemptivePriorityScheduling.Process[] actualOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); - - assertArrayEquals(expectedOrder, actualOrder, "Processes should be scheduled by priority."); - } - @Test public void testCalculateAverageWaitingTime() { NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 10, 2), new NonPreemptivePriorityScheduling.Process(2, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 8, 3)}; From 30b141c08bafb87454fd72a1f008af923397dea7 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Thu, 3 Oct 2024 11:01:40 +0530 Subject: [PATCH 09/23] Fix build errors --- .../scheduling/NonPreemptivePriorityScheduling.java | 5 ++++- .../scheduling/NonPreemptivePrioritySchedulingTest.java | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java b/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java index ad4956ac3d6e..b7505c5a9496 100644 --- a/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java @@ -11,12 +11,15 @@ */ public class NonPreemptivePriorityScheduling { + private NonPreemptivePriorityScheduling() { + } + static class Process implements Comparable { int id; // Process ID int burstTime; // Time required by the process for execution int priority; // Priority of the process (lower value indicates higher priority) - public Process(int id, int burstTime, int priority) { + Process(int id, int burstTime, int priority) { this.id = id; this.burstTime = burstTime; this.priority = priority; diff --git a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java index b14553bf17ec..ae3006c4f2f3 100644 --- a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java +++ b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java @@ -1,6 +1,6 @@ package com.thealgorithms.scheduling; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; From 1383847d636f968325b0d5250d32c4188118a1e1 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Thu, 3 Oct 2024 11:03:58 +0530 Subject: [PATCH 10/23] Declare class as final --- .../scheduling/NonPreemptivePriorityScheduling.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java b/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java index b7505c5a9496..dbcd7e266051 100644 --- a/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java @@ -9,7 +9,7 @@ * executing, * it cannot be preempted. */ -public class NonPreemptivePriorityScheduling { +public final class NonPreemptivePriorityScheduling { private NonPreemptivePriorityScheduling() { } From 5f687045298c7da8f4dc22d2089759537d423bd3 Mon Sep 17 00:00:00 2001 From: Hardvan Date: Fri, 4 Oct 2024 14:58:45 +0000 Subject: [PATCH 11/23] Update directory --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 0fe135a65810..b08a382a947f 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -238,6 +238,7 @@ * [KnapsackMemoization](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackMemoization.java) * [LevenshteinDistance](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LevenshteinDistance.java) * [LongestAlternatingSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java) + * [LongestArithmeticSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java) * [LongestCommonSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestCommonSubsequence.java) * [LongestIncreasingSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequence.java) * [LongestPalindromicSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubsequence.java) @@ -734,6 +735,7 @@ * [KnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KnapsackTest.java) * [LevenshteinDistanceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LevenshteinDistanceTests.java) * [LongestAlternatingSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequenceTest.java) + * [LongestArithmeticSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java) * [LongestIncreasingSubsequenceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequenceTests.java) * [LongestPalindromicSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstringTest.java) * [LongestValidParenthesesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestValidParenthesesTest.java) From 0e2e34b7a90abd1cb0734ea94faecee00246acde Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Fri, 4 Oct 2024 20:32:55 +0530 Subject: [PATCH 12/23] Fix comments --- .../scheduling/NonPreemptivePriorityScheduling.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java b/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java index dbcd7e266051..f4749c95f845 100644 --- a/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java @@ -5,9 +5,8 @@ /** * This class implements the Non-Preemptive Priority Scheduling algorithm. * Processes are executed in order of their priority. The process with the - * highest priority (lower priority number) is executed first, and once a process starts - * executing, - * it cannot be preempted. + * highest priority (lower priority number) is executed first, + * and once a process starts executing, it cannot be preempted. */ public final class NonPreemptivePriorityScheduling { From 80d3e0df2929d6eab028bae8027c0be83f62adc3 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 13:15:07 +0530 Subject: [PATCH 13/23] Add startTime --- .../NonPreemptivePriorityScheduling.java | 43 +++++++++++-------- .../NonPreemptivePrioritySchedulingTest.java | 31 +++++++++++-- 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java b/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java index f4749c95f845..d74303f52ee4 100644 --- a/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java @@ -13,13 +13,25 @@ public final class NonPreemptivePriorityScheduling { private NonPreemptivePriorityScheduling() { } + /** + * Represents a process with an ID, burst time, priority, and start time. + */ static class Process implements Comparable { - int id; // Process ID - int burstTime; // Time required by the process for execution - int priority; // Priority of the process (lower value indicates higher priority) + int id; + int startTime; + int burstTime; + int priority; + /** + * Constructs a Process instance with the specified parameters. + * + * @param id Unique identifier for the process + * @param burstTime Time required for the process execution + * @param priority Priority of the process + */ Process(int id, int burstTime, int priority) { this.id = id; + this.startTime = -1; this.burstTime = burstTime; this.priority = priority; } @@ -27,6 +39,10 @@ static class Process implements Comparable { /** * Compare based on priority for scheduling. The process with the lowest * priority is selected first. + * + * @param other The other process to compare against + * @return A negative integer, zero, or a positive integer as this process + * is less than, equal to, or greater than the specified process. */ @Override public int compareTo(Process other) { @@ -48,10 +64,13 @@ public static Process[] scheduleProcesses(Process[] processes) { Process[] executionOrder = new Process[processes.length]; int index = 0; + int currentTime = 0; - // Execute processes based on their priority while (!pq.isEmpty()) { - executionOrder[index++] = pq.poll(); // Poll the process with the highest priority + Process currentProcess = pq.poll(); + currentProcess.startTime = currentTime; + executionOrder[index++] = currentProcess; + currentTime += currentProcess.burstTime; } return executionOrder; @@ -94,18 +113,4 @@ public static double calculateAverageTurnaroundTime(Process[] processes, Process return (double) totalTurnaroundTime / processes.length; } - - public static void main(String[] args) { - Process[] processes = {new Process(1, 10, 2), new Process(2, 5, 1), new Process(3, 8, 3)}; - - Process[] executionOrder = scheduleProcesses(processes); - - System.out.println("Process ID | Burst Time | Priority"); - for (Process process : executionOrder) { - System.out.printf("%d %d %d%n", process.id, process.burstTime, process.priority); - } - - System.out.printf("Average Waiting Time: %.2f%n", calculateAverageWaitingTime(processes, executionOrder)); - System.out.printf("Average Turnaround Time: %.2f%n", calculateAverageTurnaroundTime(processes, executionOrder)); - } } diff --git a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java index ae3006c4f2f3..90b9271d3712 100644 --- a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java +++ b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.scheduling; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import org.junit.jupiter.api.Test; @@ -8,11 +9,15 @@ public class NonPreemptivePrioritySchedulingTest { @Test public void testCalculateAverageWaitingTime() { - NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 10, 2), new NonPreemptivePriorityScheduling.Process(2, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 8, 3)}; + NonPreemptivePriorityScheduling.Process[] processes = { + new NonPreemptivePriorityScheduling.Process(1, 10, 2), + new NonPreemptivePriorityScheduling.Process(2, 5, 1), + new NonPreemptivePriorityScheduling.Process(3, 8, 3) + }; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); - double expectedAvgWaitingTime = 6.666666666666667; + double expectedAvgWaitingTime = 6.666666666666667; // (0 + 10 + 5) / 3 double actualAvgWaitingTime = NonPreemptivePriorityScheduling.calculateAverageWaitingTime(processes, executionOrder); assertEquals(expectedAvgWaitingTime, actualAvgWaitingTime, 0.01, "Average waiting time should be calculated correctly."); @@ -20,7 +25,11 @@ public void testCalculateAverageWaitingTime() { @Test public void testCalculateAverageTurnaroundTime() { - NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 10, 2), new NonPreemptivePriorityScheduling.Process(2, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 8, 3)}; + NonPreemptivePriorityScheduling.Process[] processes = { + new NonPreemptivePriorityScheduling.Process(1, 10, 2), + new NonPreemptivePriorityScheduling.Process(2, 5, 1), + new NonPreemptivePriorityScheduling.Process(3, 8, 3) + }; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); @@ -29,4 +38,20 @@ public void testCalculateAverageTurnaroundTime() { assertEquals(expectedAvgTurnaroundTime, actualAvgTurnaroundTime, 0.01, "Average turnaround time should be calculated correctly."); } + + @Test + public void testStartTimeIsCorrect() { + NonPreemptivePriorityScheduling.Process[] processes = { + new NonPreemptivePriorityScheduling.Process(1, 10, 2), + new NonPreemptivePriorityScheduling.Process(2, 5, 1), + new NonPreemptivePriorityScheduling.Process(3, 8, 3) + }; + + NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); + + // Check that the start time for each process is correctly set + assertEquals(0, executionOrder[0].startTime, "First process should start at time 0."); + assertEquals(5, executionOrder[1].startTime, "Second process should start after the first process."); + assertEquals(15, executionOrder[2].startTime, "Third process should start after the second process."); + } } From f90053a1fe88430e407c54ad302e02b04234228a Mon Sep 17 00:00:00 2001 From: Hardvan Date: Sun, 6 Oct 2024 07:47:01 +0000 Subject: [PATCH 14/23] Update directory --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 0e57804f3aa7..deccbc9e47e0 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -459,6 +459,7 @@ * [GenerateSubsets](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java) * scheduling * [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java) + * [MLFQScheduler](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/MLFQScheduler.java) * [NonPreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java) * [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java) * [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java) @@ -752,6 +753,7 @@ * [LevenshteinDistanceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LevenshteinDistanceTests.java) * [LongestAlternatingSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequenceTest.java) * [LongestArithmeticSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java) + * [LongestCommonSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestCommonSubsequenceTest.java) * [LongestIncreasingSubsequenceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequenceTests.java) * [LongestPalindromicSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstringTest.java) * [LongestValidParenthesesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestValidParenthesesTest.java) @@ -915,6 +917,7 @@ * [GenerateSubsetsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java) * scheduling * [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java) + * [MLFQSchedulerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MLFQSchedulerTest.java) * [NonPreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java) * [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java) * [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java) From 6b77dce60170f7687bdc2876ee640f439313bf10 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 13:18:54 +0530 Subject: [PATCH 15/23] Fix --- .../NonPreemptivePrioritySchedulingTest.java | 21 +++---------------- 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java index 90b9271d3712..438025d84470 100644 --- a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java +++ b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java @@ -9,12 +9,7 @@ public class NonPreemptivePrioritySchedulingTest { @Test public void testCalculateAverageWaitingTime() { - NonPreemptivePriorityScheduling.Process[] processes = { - new NonPreemptivePriorityScheduling.Process(1, 10, 2), - new NonPreemptivePriorityScheduling.Process(2, 5, 1), - new NonPreemptivePriorityScheduling.Process(3, 8, 3) - }; - + NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 10, 2), new NonPreemptivePriorityScheduling.Process(2, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 8, 3)}; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); double expectedAvgWaitingTime = 6.666666666666667; // (0 + 10 + 5) / 3 @@ -25,12 +20,7 @@ public void testCalculateAverageWaitingTime() { @Test public void testCalculateAverageTurnaroundTime() { - NonPreemptivePriorityScheduling.Process[] processes = { - new NonPreemptivePriorityScheduling.Process(1, 10, 2), - new NonPreemptivePriorityScheduling.Process(2, 5, 1), - new NonPreemptivePriorityScheduling.Process(3, 8, 3) - }; - + NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 10, 2), new NonPreemptivePriorityScheduling.Process(2, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 8, 3)}; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); double expectedAvgTurnaroundTime = (5 + 15 + 23) / 3.0; // (5 + (5 + 10) + (5 + 10 + 8)) / 3 @@ -41,12 +31,7 @@ public void testCalculateAverageTurnaroundTime() { @Test public void testStartTimeIsCorrect() { - NonPreemptivePriorityScheduling.Process[] processes = { - new NonPreemptivePriorityScheduling.Process(1, 10, 2), - new NonPreemptivePriorityScheduling.Process(2, 5, 1), - new NonPreemptivePriorityScheduling.Process(3, 8, 3) - }; - + NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 10, 2), new NonPreemptivePriorityScheduling.Process(2, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 8, 3)}; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); // Check that the start time for each process is correctly set From def6fae46cbb17000db990adb89be68104b09920 Mon Sep 17 00:00:00 2001 From: Hardik Pawar <97388607+Hardvan@users.noreply.github.com> Date: Sun, 6 Oct 2024 13:21:39 +0530 Subject: [PATCH 16/23] Fix --- .../scheduling/NonPreemptivePrioritySchedulingTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java index 438025d84470..177e8be4d25f 100644 --- a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java +++ b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java @@ -1,7 +1,6 @@ package com.thealgorithms.scheduling; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; import org.junit.jupiter.api.Test; From 69a503ee0aad239da8a69698d83aeb2ba51ded4c Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Tue, 8 Oct 2024 15:15:46 +0530 Subject: [PATCH 17/23] Fix --- .../NonPreemptivePriorityScheduling.java | 42 ++++++++++++------- .../NonPreemptivePrioritySchedulingTest.java | 29 +++++++++---- 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java b/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java index d74303f52ee4..6aa759cfaa66 100644 --- a/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java @@ -14,10 +14,11 @@ private NonPreemptivePriorityScheduling() { } /** - * Represents a process with an ID, burst time, priority, and start time. + * Represents a process with an ID, burst time, priority, arrival time, and start time. */ static class Process implements Comparable { int id; + int arrivalTime; int startTime; int burstTime; int priority; @@ -25,12 +26,14 @@ static class Process implements Comparable { /** * Constructs a Process instance with the specified parameters. * - * @param id Unique identifier for the process - * @param burstTime Time required for the process execution - * @param priority Priority of the process + * @param id Unique identifier for the process + * @param arrivalTime Time when the process arrives in the system + * @param burstTime Time required for the process execution + * @param priority Priority of the process */ - Process(int id, int burstTime, int priority) { + Process(int id, int arrivalTime, int burstTime, int priority) { this.id = id; + this.arrivalTime = arrivalTime; this.startTime = -1; this.burstTime = burstTime; this.priority = priority; @@ -39,6 +42,7 @@ static class Process implements Comparable { /** * Compare based on priority for scheduling. The process with the lowest * priority is selected first. + * If two processes have the same priority, the one that arrives earlier is selected. * * @param other The other process to compare against * @return A negative integer, zero, or a positive integer as this process @@ -46,28 +50,36 @@ static class Process implements Comparable { */ @Override public int compareTo(Process other) { + if (this.priority == other.priority) { + return Integer.compare(this.arrivalTime, other.arrivalTime); + } return Integer.compare(this.priority, other.priority); } } /** - * Schedules processes based on their priority in a non-preemptive manner. + * Schedules processes based on their priority in a non-preemptive manner, considering their arrival times. * * @param processes Array of processes to be scheduled. * @return Array of processes in the order they are executed. */ public static Process[] scheduleProcesses(Process[] processes) { PriorityQueue pq = new PriorityQueue<>(); + int currentTime = 0; + int index = 0; + Process[] executionOrder = new Process[processes.length]; + for (Process process : processes) { pq.add(process); } - Process[] executionOrder = new Process[processes.length]; - int index = 0; - int currentTime = 0; - while (!pq.isEmpty()) { Process currentProcess = pq.poll(); + + if (currentTime < currentProcess.arrivalTime) { + currentTime = currentProcess.arrivalTime; + } + currentProcess.startTime = currentTime; executionOrder[index++] = currentProcess; currentTime += currentProcess.burstTime; @@ -85,11 +97,10 @@ public static Process[] scheduleProcesses(Process[] processes) { */ public static double calculateAverageWaitingTime(Process[] processes, Process[] executionOrder) { int totalWaitingTime = 0; - int currentTime = 0; for (Process process : executionOrder) { - totalWaitingTime += currentTime; - currentTime += process.burstTime; + int waitingTime = process.startTime - process.arrivalTime; + totalWaitingTime += waitingTime; } return (double) totalWaitingTime / processes.length; @@ -104,11 +115,10 @@ public static double calculateAverageWaitingTime(Process[] processes, Process[] */ public static double calculateAverageTurnaroundTime(Process[] processes, Process[] executionOrder) { int totalTurnaroundTime = 0; - int currentTime = 0; for (Process process : executionOrder) { - currentTime += process.burstTime; - totalTurnaroundTime += currentTime; + int turnaroundTime = (process.startTime + process.burstTime) - process.arrivalTime; + totalTurnaroundTime += turnaroundTime; } return (double) totalTurnaroundTime / processes.length; diff --git a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java index 438025d84470..88c4ec566d7d 100644 --- a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java +++ b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java @@ -1,7 +1,6 @@ package com.thealgorithms.scheduling; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; import org.junit.jupiter.api.Test; @@ -9,10 +8,14 @@ public class NonPreemptivePrioritySchedulingTest { @Test public void testCalculateAverageWaitingTime() { - NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 10, 2), new NonPreemptivePriorityScheduling.Process(2, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 8, 3)}; + NonPreemptivePriorityScheduling.Process[] processes = { + new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority + new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), + new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3) + }; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); - double expectedAvgWaitingTime = 6.666666666666667; // (0 + 10 + 5) / 3 + double expectedAvgWaitingTime = (0 + 5 + 15) / 3.0; // Waiting times: 0 for P2, 5 for P1, 15 for P3 double actualAvgWaitingTime = NonPreemptivePriorityScheduling.calculateAverageWaitingTime(processes, executionOrder); assertEquals(expectedAvgWaitingTime, actualAvgWaitingTime, 0.01, "Average waiting time should be calculated correctly."); @@ -20,10 +23,14 @@ public void testCalculateAverageWaitingTime() { @Test public void testCalculateAverageTurnaroundTime() { - NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 10, 2), new NonPreemptivePriorityScheduling.Process(2, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 8, 3)}; + NonPreemptivePriorityScheduling.Process[] processes = { + new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority + new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), + new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3) + }; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); - double expectedAvgTurnaroundTime = (5 + 15 + 23) / 3.0; // (5 + (5 + 10) + (5 + 10 + 8)) / 3 + double expectedAvgTurnaroundTime = (5 + 15 + 23) / 3.0; // Turnaround times: 5 for P2, 15 for P1, 23 for P3 double actualAvgTurnaroundTime = NonPreemptivePriorityScheduling.calculateAverageTurnaroundTime(processes, executionOrder); assertEquals(expectedAvgTurnaroundTime, actualAvgTurnaroundTime, 0.01, "Average turnaround time should be calculated correctly."); @@ -31,12 +38,16 @@ public void testCalculateAverageTurnaroundTime() { @Test public void testStartTimeIsCorrect() { - NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 10, 2), new NonPreemptivePriorityScheduling.Process(2, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 8, 3)}; + NonPreemptivePriorityScheduling.Process[] processes = { + new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority + new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), + new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3) + }; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); // Check that the start time for each process is correctly set - assertEquals(0, executionOrder[0].startTime, "First process should start at time 0."); - assertEquals(5, executionOrder[1].startTime, "Second process should start after the first process."); - assertEquals(15, executionOrder[2].startTime, "Third process should start after the second process."); + assertEquals(0, executionOrder[0].startTime, "First process (P2) should start at time 0."); // Process 2 has the highest priority + assertEquals(5, executionOrder[1].startTime, "Second process (P1) should start at time 5."); + assertEquals(15, executionOrder[2].startTime, "Third process (P3) should start at time 15."); } } From 0449f1064b289446628abcd4af4716877be9f4e1 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Tue, 8 Oct 2024 15:17:42 +0530 Subject: [PATCH 18/23] Fix --- .../scheduling/NonPreemptivePrioritySchedulingTest.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java index 88c4ec566d7d..8823d6b32359 100644 --- a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java +++ b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java @@ -10,8 +10,7 @@ public class NonPreemptivePrioritySchedulingTest { public void testCalculateAverageWaitingTime() { NonPreemptivePriorityScheduling.Process[] processes = { new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority - new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), - new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3) + new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3) }; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); @@ -25,8 +24,7 @@ public void testCalculateAverageWaitingTime() { public void testCalculateAverageTurnaroundTime() { NonPreemptivePriorityScheduling.Process[] processes = { new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority - new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), - new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3) + new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3) }; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); @@ -40,8 +38,7 @@ public void testCalculateAverageTurnaroundTime() { public void testStartTimeIsCorrect() { NonPreemptivePriorityScheduling.Process[] processes = { new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority - new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), - new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3) + new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3) }; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); From 03f10fab89a40f8639fbbde284be654a686b3b50 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Tue, 8 Oct 2024 15:19:33 +0530 Subject: [PATCH 19/23] Fix --- .../scheduling/NonPreemptivePrioritySchedulingTest.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java index 8823d6b32359..c0a99b59b78b 100644 --- a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java +++ b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java @@ -8,8 +8,7 @@ public class NonPreemptivePrioritySchedulingTest { @Test public void testCalculateAverageWaitingTime() { - NonPreemptivePriorityScheduling.Process[] processes = { - new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority + NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3) }; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); @@ -22,8 +21,7 @@ public void testCalculateAverageWaitingTime() { @Test public void testCalculateAverageTurnaroundTime() { - NonPreemptivePriorityScheduling.Process[] processes = { - new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority + NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3) }; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); @@ -36,8 +34,7 @@ public void testCalculateAverageTurnaroundTime() { @Test public void testStartTimeIsCorrect() { - NonPreemptivePriorityScheduling.Process[] processes = { - new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority + NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3) }; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); From d28bc8fb75df678bb260d57cb9a0feb375ba1678 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Tue, 8 Oct 2024 15:23:15 +0530 Subject: [PATCH 20/23] Fix --- .../scheduling/NonPreemptivePrioritySchedulingTest.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java index c0a99b59b78b..c583470a85e5 100644 --- a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java +++ b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java @@ -9,8 +9,7 @@ public class NonPreemptivePrioritySchedulingTest { @Test public void testCalculateAverageWaitingTime() { NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority - new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3) - }; + new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)}; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); double expectedAvgWaitingTime = (0 + 5 + 15) / 3.0; // Waiting times: 0 for P2, 5 for P1, 15 for P3 @@ -22,8 +21,7 @@ public void testCalculateAverageWaitingTime() { @Test public void testCalculateAverageTurnaroundTime() { NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority - new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3) - }; + new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)}; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); double expectedAvgTurnaroundTime = (5 + 15 + 23) / 3.0; // Turnaround times: 5 for P2, 15 for P1, 23 for P3 @@ -35,8 +33,7 @@ public void testCalculateAverageTurnaroundTime() { @Test public void testStartTimeIsCorrect() { NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority - new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3) - }; + new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)}; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); // Check that the start time for each process is correctly set From 34920f792bd38b5e683e5143e9028cdd17832e05 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Tue, 8 Oct 2024 15:23:43 +0530 Subject: [PATCH 21/23] Fix --- .../scheduling/NonPreemptivePriorityScheduling.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java b/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java index 6aa759cfaa66..a1a684cd0c47 100644 --- a/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java @@ -117,7 +117,7 @@ public static double calculateAverageTurnaroundTime(Process[] processes, Process int totalTurnaroundTime = 0; for (Process process : executionOrder) { - int turnaroundTime = (process.startTime + process.burstTime) - process.arrivalTime; + int turnaroundTime = process.startTime + process.burstTime - process.arrivalTime; totalTurnaroundTime += turnaroundTime; } From de50d4587a6c7961671e22d27729481ebc41acde Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 18:22:01 +0530 Subject: [PATCH 22/23] Fix --- .../NonPreemptivePriorityScheduling.java | 26 ++++++++----- .../NonPreemptivePrioritySchedulingTest.java | 39 +++++++++++++++++-- 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java b/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java index a1a684cd0c47..1d8e2c5160ff 100644 --- a/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java @@ -1,6 +1,8 @@ package com.thealgorithms.scheduling; +import java.util.LinkedList; import java.util.PriorityQueue; +import java.util.Queue; /** * This class implements the Non-Preemptive Priority Scheduling algorithm. @@ -65,24 +67,30 @@ public int compareTo(Process other) { */ public static Process[] scheduleProcesses(Process[] processes) { PriorityQueue pq = new PriorityQueue<>(); + Queue waitingQueue = new LinkedList<>(); int currentTime = 0; int index = 0; Process[] executionOrder = new Process[processes.length]; for (Process process : processes) { - pq.add(process); + waitingQueue.add(process); } - while (!pq.isEmpty()) { - Process currentProcess = pq.poll(); - - if (currentTime < currentProcess.arrivalTime) { - currentTime = currentProcess.arrivalTime; + while (!waitingQueue.isEmpty() || !pq.isEmpty()) { + // Add processes that have arrived to the priority queue + while (!waitingQueue.isEmpty() && waitingQueue.peek().arrivalTime <= currentTime) { + pq.add(waitingQueue.poll()); } - currentProcess.startTime = currentTime; - executionOrder[index++] = currentProcess; - currentTime += currentProcess.burstTime; + if (!pq.isEmpty()) { + Process currentProcess = pq.poll(); + currentProcess.startTime = currentTime; + executionOrder[index++] = currentProcess; + currentTime += currentProcess.burstTime; + } else { + // If no process is ready, move to the next arrival time + currentTime = waitingQueue.peek().arrivalTime; + } } return executionOrder; diff --git a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java index c583470a85e5..9b4893de08ae 100644 --- a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java +++ b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java @@ -9,7 +9,7 @@ public class NonPreemptivePrioritySchedulingTest { @Test public void testCalculateAverageWaitingTime() { NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority - new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)}; + new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)}; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); 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() { @Test public void testCalculateAverageTurnaroundTime() { NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority - new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)}; + new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)}; + NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); double expectedAvgTurnaroundTime = (5 + 15 + 23) / 3.0; // Turnaround times: 5 for P2, 15 for P1, 23 for P3 @@ -32,8 +33,10 @@ public void testCalculateAverageTurnaroundTime() { @Test public void testStartTimeIsCorrect() { - NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority - new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)}; + NonPreemptivePriorityScheduling.Process[] processes = { + new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority + new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)}; + NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); // Check that the start time for each process is correctly set @@ -41,4 +44,32 @@ public void testStartTimeIsCorrect() { assertEquals(5, executionOrder[1].startTime, "Second process (P1) should start at time 5."); assertEquals(15, executionOrder[2].startTime, "Third process (P3) should start at time 15."); } + + @Test + public void testWithDelayedArrivalTimes() { + NonPreemptivePriorityScheduling.Process[] processes = { + new NonPreemptivePriorityScheduling.Process(1, 0, 4, 1), // id, arrivalTime, burstTime, priority + new NonPreemptivePriorityScheduling.Process(2, 2, 3, 2), new NonPreemptivePriorityScheduling.Process(3, 4, 2, 3)}; + + NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); + + // Test the start times considering delayed arrivals + assertEquals(0, executionOrder[0].startTime, "First process (P1) should start at time 0."); + assertEquals(4, executionOrder[1].startTime, "Second process (P2) should start at time 4."); // After P1 finishes + assertEquals(7, executionOrder[2].startTime, "Third process (P3) should start at time 7."); // After P2 finishes + } + + @Test + public void testWithGapsInArrivals() { + NonPreemptivePriorityScheduling.Process[] processes = { + new NonPreemptivePriorityScheduling.Process(1, 0, 6, 2), // id, arrivalTime, burstTime, priority + new NonPreemptivePriorityScheduling.Process(2, 8, 4, 1), new NonPreemptivePriorityScheduling.Process(3, 12, 5, 3)}; + + NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); + + // Test the start times for processes with gaps in arrival times + assertEquals(0, executionOrder[0].startTime, "First process (P1) should start at time 0."); + assertEquals(8, executionOrder[1].startTime, "Second process (P2) should start at time 8."); // After P1 finishes, arrives at 8 + assertEquals(12, executionOrder[2].startTime, "Third process (P3) should start at time 12."); // After P2 finishes, arrives at 12 + } } From f3ac28fa2c95c3be50aa8d0422008fd75dd5f0f3 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 18:23:49 +0530 Subject: [PATCH 23/23] Fix --- .../NonPreemptivePrioritySchedulingTest.java | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java index 9b4893de08ae..d28dcfeaaea3 100644 --- a/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java +++ b/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java @@ -9,7 +9,7 @@ public class NonPreemptivePrioritySchedulingTest { @Test public void testCalculateAverageWaitingTime() { NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority - new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)}; + new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)}; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); double expectedAvgWaitingTime = (0 + 5 + 15) / 3.0; // Waiting times: 0 for P2, 5 for P1, 15 for P3 @@ -21,7 +21,7 @@ public void testCalculateAverageWaitingTime() { @Test public void testCalculateAverageTurnaroundTime() { NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority - new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)}; + new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)}; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); @@ -33,10 +33,8 @@ public void testCalculateAverageTurnaroundTime() { @Test public void testStartTimeIsCorrect() { - NonPreemptivePriorityScheduling.Process[] processes = { - new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority - new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)}; - + NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority + new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)}; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); // Check that the start time for each process is correctly set @@ -47,10 +45,8 @@ public void testStartTimeIsCorrect() { @Test public void testWithDelayedArrivalTimes() { - NonPreemptivePriorityScheduling.Process[] processes = { - new NonPreemptivePriorityScheduling.Process(1, 0, 4, 1), // id, arrivalTime, burstTime, priority - new NonPreemptivePriorityScheduling.Process(2, 2, 3, 2), new NonPreemptivePriorityScheduling.Process(3, 4, 2, 3)}; - + NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 4, 1), // id, arrivalTime, burstTime, priority + new NonPreemptivePriorityScheduling.Process(2, 2, 3, 2), new NonPreemptivePriorityScheduling.Process(3, 4, 2, 3)}; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); // Test the start times considering delayed arrivals @@ -61,9 +57,8 @@ public void testWithDelayedArrivalTimes() { @Test public void testWithGapsInArrivals() { - NonPreemptivePriorityScheduling.Process[] processes = { - new NonPreemptivePriorityScheduling.Process(1, 0, 6, 2), // id, arrivalTime, burstTime, priority - new NonPreemptivePriorityScheduling.Process(2, 8, 4, 1), new NonPreemptivePriorityScheduling.Process(3, 12, 5, 3)}; + NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 6, 2), // id, arrivalTime, burstTime, priority + new NonPreemptivePriorityScheduling.Process(2, 8, 4, 1), new NonPreemptivePriorityScheduling.Process(3, 12, 5, 3)}; NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes);