Skip to content
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
* [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)
Expand Down Expand Up @@ -916,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)
* [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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package com.thealgorithms.scheduling;

import java.util.Arrays;
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.
*/
public final class HighestResponseRatioNextScheduling {
private 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, Comparator.comparingInt(a -> arrivalTimes[a]));
return Arrays.stream(indices).mapToInt(Integer::intValue).toArray();
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}