Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java (Java)

Code before:
     * @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;
                }
            }
        }

Review comment (suggestion):
Can this value, cause problems if the process with index 0 is not ready yet?

If yes, for such case better instead current approach, to return -1 or another special index to explicitly indicate that the process was not found. In the loop body, it is worth checking that the process actually exists.

Code after:
        int currentTime = 0;
        int[] turnAroundTime = new int[noOfProcesses];
        Process[] processes = new Process[noOfProcesses];

        for (int i = 0; i < noOfProcesses; i++) {
            processes[i] = new Process(processNames[i], arrivalTimes[i], burstTimes[i]);
        }

        Arrays.sort(processes, Comparator.comparingInt(p -> p.arrivalTime));

        int finishedProcessCount = 0;
        while (finishedProcessCount < noOfProcesses) {
            int nextProcessIndex = findNextProcess(processes, currentTime);
            if (nextProcessIndex == PROCESS_NOT_FOUND) {
                currentTime++;
                continue;
            }

            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;
    }

    /**
     * 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.
     *