From aae1814981ae5ad1f94058c6ba186df4a8f61562 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 12:54:55 +0530 Subject: [PATCH 1/9] Add `HighestResponseRatioNextScheduling.java` new algorithm with tests --- .../HighestResponseRatioNextScheduling.java | 132 ++++++++++++++++++ ...ighestResponseRatioNextSchedulingTest.java | 47 +++++++ 2 files changed, 179 insertions(+) create mode 100644 src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java create mode 100644 src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java diff --git a/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java b/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java new file mode 100644 index 000000000000..0969837d3810 --- /dev/null +++ b/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java @@ -0,0 +1,132 @@ +package com.thealgorithms.scheduling; + +import java.util.Arrays; + +/** + * The HighestResponseRatioNext class implements the HRRN scheduling algorithm. + * It calculates the Turn Around Time (TAT) and Waiting Time (WT) for a set of processes. + */ +public class HighestResponseRatioNextScheduling { + + /** + * Calculates the Turn Around Time (TAT) for each process. + * + * @param processNames Array of process names. + * @param arrivalTimes Array of arrival times corresponding to each process. + * @param burstTimes Array of burst times for each process. + * @param noOfProcesses The number of processes. + * @return An array of Turn Around Times for each process. + */ + public static int[] calculateTurnAroundTime(String[] processNames, int[] arrivalTimes, int[] burstTimes, int noOfProcesses) { + int currentTime = 0; + int[] turnAroundTime = new int[noOfProcesses]; + boolean[] finishedProcess = new boolean[noOfProcesses]; + int finishedProcessCount = 0; + + // Sort by arrival times using a simple bubble sort for demonstration + int[] sortedIndices = sortIndicesByArrival(arrivalTimes); + arrivalTimes = Arrays.copyOf(arrivalTimes, arrivalTimes.length); + burstTimes = Arrays.copyOf(burstTimes, burstTimes.length); + processNames = Arrays.copyOf(processNames, processNames.length); + + // Reorder the arrays based on sorted indices + int[] tempArrival = new int[noOfProcesses]; + int[] tempBurst = new int[noOfProcesses]; + String[] tempProcess = new String[noOfProcesses]; + + for (int i = 0; i < noOfProcesses; i++) { + tempArrival[i] = arrivalTimes[sortedIndices[i]]; + tempBurst[i] = burstTimes[sortedIndices[i]]; + tempProcess[i] = processNames[sortedIndices[i]]; + } + + arrivalTimes = tempArrival; + burstTimes = tempBurst; + processNames = tempProcess; + + while (finishedProcessCount < noOfProcesses) { + currentTime = Math.max(currentTime, arrivalTimes[findNextProcess(arrivalTimes, finishedProcess, currentTime)]); + int loc = findHighestResponseRatio(processNames, arrivalTimes, burstTimes, finishedProcess, currentTime); + + turnAroundTime[loc] = currentTime + burstTimes[loc] - arrivalTimes[loc]; + currentTime += burstTimes[loc]; + finishedProcess[loc] = true; + finishedProcessCount++; + } + + return turnAroundTime; + } + + /** + * Calculates the Waiting Time (WT) for each process. + * + * @param turnAroundTime The Turn Around Times for each process. + * @param burstTimes The burst times for each process. + * @return An array of Waiting Times for each process. + */ + public static int[] calculateWaitingTime(int[] turnAroundTime, int[] burstTimes) { + int[] waitingTime = new int[turnAroundTime.length]; + for (int i = 0; i < turnAroundTime.length; i++) { + waitingTime[i] = turnAroundTime[i] - burstTimes[i]; + } + return waitingTime; + } + + /** + * Finds the next process to be scheduled based on arrival times and the current time. + * + * @param arrivalTimes Array of arrival times for each process. + * @param finishedProcess Array indicating whether each process has finished. + * @param currentTime The current time in the scheduling process. + * @return The index of the next process to be scheduled. + */ + private static int findNextProcess(int[] arrivalTimes, boolean[] finishedProcess, int currentTime) { + for (int i = 0; i < arrivalTimes.length; i++) { + if (!finishedProcess[i] && arrivalTimes[i] <= currentTime) { + return i; + } + } + return 0; + } + + /** + * Finds the process with the highest response ratio. + * + * @param processNames Array of process names. + * @param arrivalTimes Array of arrival times for each process. + * @param burstTimes Array of burst times for each process. + * @param finishedProcess Array indicating whether each process has finished. + * @param currentTime The current time in the scheduling process. + * @return The index of the process with the highest response ratio. + */ + private static int findHighestResponseRatio(String[] processNames, int[] arrivalTimes, int[] burstTimes, boolean[] finishedProcess, int currentTime) { + double maxResponseRatio = -1; + int loc = -1; + + for (int i = 0; i < processNames.length; i++) { + if (!finishedProcess[i] && arrivalTimes[i] <= currentTime) { + double responseRatio = (double) (burstTimes[i] + (currentTime - arrivalTimes[i])) / burstTimes[i]; + if (responseRatio > maxResponseRatio) { + maxResponseRatio = responseRatio; + loc = i; + } + } + } + return loc; + } + + /** + * Sorts the indices of the arrival times array in ascending order. + * + * @param arrivalTimes Array of arrival times for each process. + * @return An array of indices sorted by the corresponding arrival times. + */ + private static int[] sortIndicesByArrival(int[] arrivalTimes) { + Integer[] indices = new Integer[arrivalTimes.length]; + for (int i = 0; i < arrivalTimes.length; i++) { + indices[i] = i; + } + Arrays.sort(indices, (a, b) -> Integer.compare(arrivalTimes[a], arrivalTimes[b])); + return Arrays.stream(indices).mapToInt(Integer::intValue).toArray(); + } +} diff --git a/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java new file mode 100644 index 000000000000..d2aafa93e6a6 --- /dev/null +++ b/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java @@ -0,0 +1,47 @@ +package com.thealgorithms.scheduling; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import org.junit.jupiter.api.Test; + +public class HighestResponseRatioNextSchedulingTest { + + @Test + public void testCalculateTurnAroundTime() { + String[] processNames = {"A", "B", "C"}; + int[] arrivalTimes = {0, 2, 4}; + int[] burstTimes = {3, 1, 2}; + int noOfProcesses = 3; + + int[] expectedTurnAroundTimes = {3, 2, 2}; + int[] actualTurnAroundTimes = HighestResponseRatioNextScheduling.calculateTurnAroundTime(processNames, arrivalTimes, burstTimes, noOfProcesses); + + assertArrayEquals(expectedTurnAroundTimes, actualTurnAroundTimes, "Turn Around Times do not match"); + } + + @Test + public void testCalculateWaitingTime() { + int[] turnAroundTimes = {3, 1, 5}; + int[] burstTimes = {3, 1, 2}; + + int[] expectedWaitingTimes = {0, 0, 3}; + int[] actualWaitingTimes = HighestResponseRatioNextScheduling.calculateWaitingTime(turnAroundTimes, burstTimes); + + assertArrayEquals(expectedWaitingTimes, actualWaitingTimes, "Waiting Times do not match"); + } + + @Test + public void testCompleteSchedulingScenario() { + String[] processNames = {"A", "B", "C"}; + int[] arrivalTimes = {0, 1, 2}; + int[] burstTimes = {5, 2, 1}; + + int[] expectedTurnAroundTimes = {5, 7, 4}; + int[] turnAroundTimes = HighestResponseRatioNextScheduling.calculateTurnAroundTime(processNames, arrivalTimes, burstTimes, processNames.length); + assertArrayEquals(expectedTurnAroundTimes, turnAroundTimes, "Turn Around Times do not match"); + + int[] expectedWaitingTimes = {0, 5, 3}; + int[] waitingTimes = HighestResponseRatioNextScheduling.calculateWaitingTime(turnAroundTimes, burstTimes); + assertArrayEquals(expectedWaitingTimes, waitingTimes, "Waiting Times do not match"); + } +} From 90d2097fb12a6f3e662ef6a2234b61fde9717f02 Mon Sep 17 00:00:00 2001 From: Hardvan Date: Sun, 6 Oct 2024 07:25:14 +0000 Subject: [PATCH 2/9] Update directory --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 1bad5d3b98a3..5a78118e932d 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -457,6 +457,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) + * [HighestResponseRatioNextScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.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) @@ -911,6 +912,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) + * [HighestResponseRatioNextSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.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 4db4786fbc11aba0ea0ddf2d65a41a8e3fbd30e1 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 13:02:03 +0530 Subject: [PATCH 3/9] Improve class documentation --- .../scheduling/HighestResponseRatioNextScheduling.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java b/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java index 0969837d3810..10cb961544f4 100644 --- a/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java @@ -3,8 +3,13 @@ import java.util.Arrays; /** - * The HighestResponseRatioNext class implements the HRRN scheduling algorithm. - * It calculates the Turn Around Time (TAT) and Waiting Time (WT) for a set of processes. + * The HighestResponseRatioNextScheduling class implements + * the Highest Response Ratio Next (HRRN) scheduling algorithm. + * HRRN is a non-preemptive scheduling algorithm that + * selects the process with the highest response ratio for execution. + * The response ratio is calculated as (waiting time + burst time) / burst time. + * This algorithm aims to reduce the average waiting time + * and improve overall system performance by balancing short and long processes. */ public class HighestResponseRatioNextScheduling { From 93cf0dd7bd4c6782d8a4cb8315bd89f8b036902f Mon Sep 17 00:00:00 2001 From: Hardvan Date: Sun, 6 Oct 2024 07:33:16 +0000 Subject: [PATCH 4/9] Update directory --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 179f537a9768..7e9f5357a825 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -460,6 +460,7 @@ * scheduling * [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java) * [HighestResponseRatioNextScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java) + * [MLFQScheduler](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/MLFQScheduler.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) @@ -917,6 +918,7 @@ * scheduling * [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java) * [HighestResponseRatioNextSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java) + * [MLFQSchedulerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MLFQSchedulerTest.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 82fbe4d2cc17493c4b6e10c1889f5aca65c142d5 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 13:06:30 +0530 Subject: [PATCH 5/9] Fix --- .../scheduling/HighestResponseRatioNextScheduling.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java b/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java index 10cb961544f4..0892d485ef37 100644 --- a/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java @@ -12,6 +12,8 @@ * and improve overall system performance by balancing short and long processes. */ public class HighestResponseRatioNextScheduling { + private HighestResponseRatioNextScheduling() { + } /** * Calculates the Turn Around Time (TAT) for each process. From 28be35c5ba168b7c85f3c16e93f8173ed5999912 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 13:08:40 +0530 Subject: [PATCH 6/9] Fix --- .../scheduling/HighestResponseRatioNextScheduling.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java b/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java index 0892d485ef37..883fa82e1fae 100644 --- a/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java @@ -1,6 +1,7 @@ package com.thealgorithms.scheduling; import java.util.Arrays; +import java.util.Comparator; /** * The HighestResponseRatioNextScheduling class implements @@ -11,7 +12,7 @@ * This algorithm aims to reduce the average waiting time * and improve overall system performance by balancing short and long processes. */ -public class HighestResponseRatioNextScheduling { +public final class HighestResponseRatioNextScheduling { private HighestResponseRatioNextScheduling() { } @@ -133,7 +134,7 @@ private static int[] sortIndicesByArrival(int[] arrivalTimes) { for (int i = 0; i < arrivalTimes.length; i++) { indices[i] = i; } - Arrays.sort(indices, (a, b) -> Integer.compare(arrivalTimes[a], arrivalTimes[b])); + Arrays.sort(indices, Comparator.comparingInt(a -> arrivalTimes[a])); return Arrays.stream(indices).mapToInt(Integer::intValue).toArray(); } } From e4ec6081f8c889818a468328ff6dc2ade31f467a Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 6 Oct 2024 13:12:30 +0530 Subject: [PATCH 7/9] Fix --- .../scheduling/HighestResponseRatioNextScheduling.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java b/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java index 883fa82e1fae..891cde6a04d0 100644 --- a/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java @@ -113,7 +113,7 @@ private static int findHighestResponseRatio(String[] processNames, int[] arrival for (int i = 0; i < processNames.length; i++) { if (!finishedProcess[i] && arrivalTimes[i] <= currentTime) { - double responseRatio = (double) (burstTimes[i] + (currentTime - arrivalTimes[i])) / burstTimes[i]; + double responseRatio = (double) (burstTimes[i] + currentTime - arrivalTimes[i]) / burstTimes[i]; if (responseRatio > maxResponseRatio) { maxResponseRatio = responseRatio; loc = i; From 6b5f794809bdd8c14e9dc5fcb82981124e85d928 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Mon, 7 Oct 2024 12:52:00 +0530 Subject: [PATCH 8/9] Add suggested changes --- .../HighestResponseRatioNextScheduling.java | 166 ++++++++++-------- ...ighestResponseRatioNextSchedulingTest.java | 65 +++++++ 2 files changed, 157 insertions(+), 74 deletions(-) diff --git a/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java b/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java index 891cde6a04d0..8ed689698557 100644 --- a/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java +++ b/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java @@ -4,64 +4,101 @@ import java.util.Comparator; /** - * The HighestResponseRatioNextScheduling class implements - * the Highest Response Ratio Next (HRRN) scheduling algorithm. - * HRRN is a non-preemptive scheduling algorithm that - * selects the process with the highest response ratio for execution. - * The response ratio is calculated as (waiting time + burst time) / burst time. - * This algorithm aims to reduce the average waiting time - * and improve overall system performance by balancing short and long processes. + * The {@code HighestResponseRatioNextScheduling} class implements the + * Highest Response Ratio Next (HRRN) scheduling algorithm. + * HRRN is a non-preemptive scheduling algorithm that selects the process with + * the highest response ratio for execution. + * The response ratio is calculated as: + * + *
+ *     Response Ratio = (waiting time + burst time) / burst time
+ * 
+ * + * HRRN is designed to reduce the average waiting time and improve overall + * system performance by balancing between short and long processes, + * minimizing process starvation. */ public final class HighestResponseRatioNextScheduling { + + private static final int PROCESS_NOT_FOUND = -1; + private static final double INITIAL_MAX_RESPONSE_RATIO = -1.0; + private HighestResponseRatioNextScheduling() { } + /** + * Represents a process in the scheduling algorithm. + */ + private static class Process { + String name; + int arrivalTime; + int burstTime; + int turnAroundTime; + boolean finished; + + Process(String name, int arrivalTime, int burstTime) { + this.name = name; + this.arrivalTime = arrivalTime; + this.burstTime = burstTime; + this.turnAroundTime = 0; + this.finished = false; + } + + /** + * Calculates the response ratio for this process. + * + * @param currentTime The current time in the scheduling process. + * @return The response ratio for this process. + */ + double calculateResponseRatio(int currentTime) { + return (double) (burstTime + currentTime - arrivalTime) / burstTime; + } + } + /** * Calculates the Turn Around Time (TAT) for each process. * + *

Turn Around Time is calculated as the total time a process spends + * in the system from arrival to completion. It is the sum of the burst time + * and the waiting time.

+ * * @param processNames Array of process names. * @param arrivalTimes Array of arrival times corresponding to each process. - * @param burstTimes Array of burst times for each process. + * @param burstTimes Array of burst times for each process. * @param noOfProcesses The number of processes. * @return An array of Turn Around Times for each process. */ - public static int[] calculateTurnAroundTime(String[] processNames, int[] arrivalTimes, int[] burstTimes, int noOfProcesses) { + public static int[] calculateTurnAroundTime(final String[] processNames, final int[] arrivalTimes, final int[] burstTimes, final int noOfProcesses) { int currentTime = 0; int[] turnAroundTime = new int[noOfProcesses]; - boolean[] finishedProcess = new boolean[noOfProcesses]; - int finishedProcessCount = 0; - - // Sort by arrival times using a simple bubble sort for demonstration - int[] sortedIndices = sortIndicesByArrival(arrivalTimes); - arrivalTimes = Arrays.copyOf(arrivalTimes, arrivalTimes.length); - burstTimes = Arrays.copyOf(burstTimes, burstTimes.length); - processNames = Arrays.copyOf(processNames, processNames.length); - - // Reorder the arrays based on sorted indices - int[] tempArrival = new int[noOfProcesses]; - int[] tempBurst = new int[noOfProcesses]; - String[] tempProcess = new String[noOfProcesses]; + Process[] processes = new Process[noOfProcesses]; for (int i = 0; i < noOfProcesses; i++) { - tempArrival[i] = arrivalTimes[sortedIndices[i]]; - tempBurst[i] = burstTimes[sortedIndices[i]]; - tempProcess[i] = processNames[sortedIndices[i]]; + processes[i] = new Process(processNames[i], arrivalTimes[i], burstTimes[i]); } - arrivalTimes = tempArrival; - burstTimes = tempBurst; - processNames = tempProcess; + Arrays.sort(processes, Comparator.comparingInt(p -> p.arrivalTime)); + int finishedProcessCount = 0; while (finishedProcessCount < noOfProcesses) { - currentTime = Math.max(currentTime, arrivalTimes[findNextProcess(arrivalTimes, finishedProcess, currentTime)]); - int loc = findHighestResponseRatio(processNames, arrivalTimes, burstTimes, finishedProcess, currentTime); + int nextProcessIndex = findNextProcess(processes, currentTime); + if (nextProcessIndex == PROCESS_NOT_FOUND) { + currentTime++; + continue; + } - turnAroundTime[loc] = currentTime + burstTimes[loc] - arrivalTimes[loc]; - currentTime += burstTimes[loc]; - finishedProcess[loc] = true; + Process currentProcess = processes[nextProcessIndex]; + currentTime = Math.max(currentTime, currentProcess.arrivalTime); + currentProcess.turnAroundTime = currentTime + currentProcess.burstTime - currentProcess.arrivalTime; + currentTime += currentProcess.burstTime; + currentProcess.finished = true; finishedProcessCount++; } + for (int i = 0; i < noOfProcesses; i++) { + turnAroundTime[i] = processes[i].turnAroundTime; + } + return turnAroundTime; } @@ -69,7 +106,7 @@ public static int[] calculateTurnAroundTime(String[] processNames, int[] arrival * Calculates the Waiting Time (WT) for each process. * * @param turnAroundTime The Turn Around Times for each process. - * @param burstTimes The burst times for each process. + * @param burstTimes The burst times for each process. * @return An array of Waiting Times for each process. */ public static int[] calculateWaitingTime(int[] turnAroundTime, int[] burstTimes) { @@ -83,58 +120,39 @@ public static int[] calculateWaitingTime(int[] turnAroundTime, int[] burstTimes) /** * Finds the next process to be scheduled based on arrival times and the current time. * - * @param arrivalTimes Array of arrival times for each process. - * @param finishedProcess Array indicating whether each process has finished. - * @param currentTime The current time in the scheduling process. - * @return The index of the next process to be scheduled. + * @param processes Array of Process objects. + * @param currentTime The current time in the scheduling process. + * @return The index of the next process to be scheduled, or PROCESS_NOT_FOUND if no process is ready. */ - private static int findNextProcess(int[] arrivalTimes, boolean[] finishedProcess, int currentTime) { - for (int i = 0; i < arrivalTimes.length; i++) { - if (!finishedProcess[i] && arrivalTimes[i] <= currentTime) { - return i; - } - } - return 0; + private static int findNextProcess(Process[] processes, int currentTime) { + return findHighestResponseRatio(processes, currentTime); } /** * Finds the process with the highest response ratio. * - * @param processNames Array of process names. - * @param arrivalTimes Array of arrival times for each process. - * @param burstTimes Array of burst times for each process. - * @param finishedProcess Array indicating whether each process has finished. - * @param currentTime The current time in the scheduling process. - * @return The index of the process with the highest response ratio. + *

The response ratio is calculated as: + * (waiting time + burst time) / burst time + * where waiting time = current time - arrival time

+ * + * @param processes Array of Process objects. + * @param currentTime The current time in the scheduling process. + * @return The index of the process with the highest response ratio, or PROCESS_NOT_FOUND if no process is ready. */ - private static int findHighestResponseRatio(String[] processNames, int[] arrivalTimes, int[] burstTimes, boolean[] finishedProcess, int currentTime) { - double maxResponseRatio = -1; - int loc = -1; + private static int findHighestResponseRatio(Process[] processes, int currentTime) { + double maxResponseRatio = INITIAL_MAX_RESPONSE_RATIO; + int maxIndex = PROCESS_NOT_FOUND; - for (int i = 0; i < processNames.length; i++) { - if (!finishedProcess[i] && arrivalTimes[i] <= currentTime) { - double responseRatio = (double) (burstTimes[i] + currentTime - arrivalTimes[i]) / burstTimes[i]; + for (int i = 0; i < processes.length; i++) { + Process process = processes[i]; + if (!process.finished && process.arrivalTime <= currentTime) { + double responseRatio = process.calculateResponseRatio(currentTime); if (responseRatio > maxResponseRatio) { maxResponseRatio = responseRatio; - loc = i; + maxIndex = i; } } } - return loc; - } - - /** - * Sorts the indices of the arrival times array in ascending order. - * - * @param arrivalTimes Array of arrival times for each process. - * @return An array of indices sorted by the corresponding arrival times. - */ - private static int[] sortIndicesByArrival(int[] arrivalTimes) { - Integer[] indices = new Integer[arrivalTimes.length]; - for (int i = 0; i < arrivalTimes.length; i++) { - indices[i] = i; - } - Arrays.sort(indices, Comparator.comparingInt(a -> arrivalTimes[a])); - return Arrays.stream(indices).mapToInt(Integer::intValue).toArray(); + return maxIndex; } } diff --git a/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java index d2aafa93e6a6..7c8268aa6e11 100644 --- a/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java +++ b/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java @@ -44,4 +44,69 @@ public void testCompleteSchedulingScenario() { int[] waitingTimes = HighestResponseRatioNextScheduling.calculateWaitingTime(turnAroundTimes, burstTimes); assertArrayEquals(expectedWaitingTimes, waitingTimes, "Waiting Times do not match"); } + + @Test + public void testZeroProcesses() { + String[] processNames = {}; + int[] arrivalTimes = {}; + int[] burstTimes = {}; + int noOfProcesses = 0; + + int[] expectedTurnAroundTimes = {}; + int[] actualTurnAroundTimes = HighestResponseRatioNextScheduling.calculateTurnAroundTime(processNames, arrivalTimes, burstTimes, noOfProcesses); + + assertArrayEquals(expectedTurnAroundTimes, actualTurnAroundTimes, "Turn Around Times for zero processes should be an empty array"); + } + + @Test + public void testAllProcessesArriveAtSameTime() { + String[] processNames = {"A", "B", "C", "D"}; + int[] arrivalTimes = {0, 0, 0, 0}; + int[] burstTimes = {4, 3, 1, 2}; + int noOfProcesses = 4; + + int[] expectedTurnAroundTimes = {4, 10, 5, 7}; + int[] actualTurnAroundTimes = HighestResponseRatioNextScheduling.calculateTurnAroundTime(processNames, arrivalTimes, burstTimes, noOfProcesses); + + assertArrayEquals(expectedTurnAroundTimes, actualTurnAroundTimes, "Turn Around Times for processes arriving at the same time do not match"); + } + + @Test + public void testProcessesWithZeroBurstTime() { + String[] processNames = {"A", "B", "C"}; + int[] arrivalTimes = {0, 1, 2}; + int[] burstTimes = {3, 0, 2}; + int noOfProcesses = 3; + + int[] expectedTurnAroundTimes = {3, 2, 3}; + int[] actualTurnAroundTimes = HighestResponseRatioNextScheduling.calculateTurnAroundTime(processNames, arrivalTimes, burstTimes, noOfProcesses); + + assertArrayEquals(expectedTurnAroundTimes, actualTurnAroundTimes, "Turn Around Times for processes with zero burst time do not match"); + } + + @Test + public void testProcessesWithLargeGapsBetweenArrivals() { + String[] processNames = {"A", "B", "C"}; + int[] arrivalTimes = {0, 100, 200}; + int[] burstTimes = {10, 10, 10}; + int noOfProcesses = 3; + + int[] expectedTurnAroundTimes = {10, 10, 10}; + int[] actualTurnAroundTimes = HighestResponseRatioNextScheduling.calculateTurnAroundTime(processNames, arrivalTimes, burstTimes, noOfProcesses); + + assertArrayEquals(expectedTurnAroundTimes, actualTurnAroundTimes, "Turn Around Times for processes with large gaps between arrivals do not match"); + } + + @Test + public void testProcessesWithVeryLargeBurstTimes() { + String[] processNames = {"A", "B"}; + int[] arrivalTimes = {0, 1}; + int[] burstTimes = {Integer.MAX_VALUE/2, Integer.MAX_VALUE/2}; + int noOfProcesses = 2; + + int[] expectedTurnAroundTimes = {Integer.MAX_VALUE/2, Integer.MAX_VALUE-2}; + int[] actualTurnAroundTimes = HighestResponseRatioNextScheduling.calculateTurnAroundTime(processNames, arrivalTimes, burstTimes, noOfProcesses); + + assertArrayEquals(expectedTurnAroundTimes, actualTurnAroundTimes, "Turn Around Times for processes with very large burst times do not match"); + } } From f6cda321548e09cebb7cc332a5a79c017cc45230 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Mon, 7 Oct 2024 12:53:22 +0530 Subject: [PATCH 9/9] Fix clang errors --- .../scheduling/HighestResponseRatioNextSchedulingTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java index 7c8268aa6e11..d225d76b82c9 100644 --- a/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java +++ b/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java @@ -101,10 +101,10 @@ public void testProcessesWithLargeGapsBetweenArrivals() { public void testProcessesWithVeryLargeBurstTimes() { String[] processNames = {"A", "B"}; int[] arrivalTimes = {0, 1}; - int[] burstTimes = {Integer.MAX_VALUE/2, Integer.MAX_VALUE/2}; + int[] burstTimes = {Integer.MAX_VALUE / 2, Integer.MAX_VALUE / 2}; int noOfProcesses = 2; - int[] expectedTurnAroundTimes = {Integer.MAX_VALUE/2, Integer.MAX_VALUE-2}; + int[] expectedTurnAroundTimes = {Integer.MAX_VALUE / 2, Integer.MAX_VALUE - 2}; int[] actualTurnAroundTimes = HighestResponseRatioNextScheduling.calculateTurnAroundTime(processNames, arrivalTimes, burstTimes, noOfProcesses); assertArrayEquals(expectedTurnAroundTimes, actualTurnAroundTimes, "Turn Around Times for processes with very large burst times do not match");