Repository: TheAlgorithms/Java
Pull request #5011 (2024-01-08 16:39:42)

SRTF Algorithm and SRTF Tests
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

mpousmali: Refer to issue #5005 

Patch:
diff --git a/.gitpod.yml b/.gitpod.yml
index 4a3944d0..21d69f6e 100644
--- a/.gitpod.yml
+++ b/.gitpod.yml
@@ -10,3 +10,4 @@ tasks:
 vscode:
   extensions:
     - xaver.clang-format
+
diff --git a/src/main/java/com/thealgorithms/scheduling/SRTFScheduling.java b/src/main/java/com/thealgorithms/scheduling/SRTFScheduling.java
new file mode 100644
index 00000000..ad8aeaba
--- /dev/null
+++ b/src/main/java/com/thealgorithms/scheduling/SRTFScheduling.java
@@ -0,0 +1,69 @@
+package com.thealgorithms.scheduling;
+
+import com.thealgorithms.devutils.entities.ProcessDetails;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Implementation of Shortest Remaining Time First Scheduling Algorithm.
+ * In the SRTF scheduling algorithm, the process with the smallest amount of time remaining until completion is selected to execute.
+ * Example:
+ * Consider the processes p1, p2 and the following table with info about their arrival and burst time:
+ * Process | Burst Time | Arrival Time
+ * P1      | 6 ms        | 0 ms
+ * P2      | 2 ms        | 1 ms
+ * In this example, P1 will be executed at time = 0 until time = 1 when P2 arrives. At time = 2, P2 will be executed until time = 4. At time 4, P2 is done, and P1 is executed again to be done.
+ * That's a simple example of how the algorithm works.
+ * More information you can find here -> https://en.wikipedia.org/wiki/Shortest_remaining_time
+ */
+public class SRTFScheduling {
+    protected List<ProcessDetails> processes;
+    protected List<String> ready;
+
+    /**
+     * Constructor
+     * @param processes ArrayList of ProcessDetails given as input
+     */
+    public SRTFScheduling(ArrayList<ProcessDetails> processes) {
+        this.processes = new ArrayList<>();
+        ready = new ArrayList<>();
+        this.processes = processes;
+    }
+
+    public void evaluateScheduling() {
+        int time = 0, cr = 0; // cr=current running process, time= units of time
+        int n = processes.size();
+        int[] remainingTime = new int[n];
+
+        /* calculating remaining time of every process and total units of time */
+        for (int i = 0; i < n; i++) {
+            remainingTime[i] = processes.get(i).getBurstTime();
+            time += processes.get(i).getBurstTime();
+        }
+
+        /* if the first process doesn't arrive at 0, we have more units of time */
+        if (processes.get(0).getArrivalTime() != 0) {
+            time += processes.get(0).getArrivalTime();
+        }
+
+        /* printing id of the process which is executed at every unit of time */
+        // if the first process doesn't arrive at 0, we print only \n until it arrives
+        if (processes.get(0).getArrivalTime() != 0) {
+            for (int i = 0; i < processes.get(0).getArrivalTime(); i++) {
+                ready.add(null);
+            }
+        }
+
+        for (int i = processes.get(0).getArrivalTime(); i < time; i++) {
+            /* checking if there's a process with remaining time less than current running process.
+               If we find it, then it executes. */
+            for (int j = 0; j < n; j++) {
+                if (processes.get(j).getArrivalTime() <= i && (remainingTime[j] < remainingTime[cr] && remainingTime[j] > 0 || remainingTime[cr] == 0)) {
+                    cr = j;
+                }
+            }
+            ready.add(processes.get(cr).getProcessId());
+            remainingTime[cr]--;
+        }
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/scheduling/SRTFSchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/SRTFSchedulingTest.java
new file mode 100644
index 00000000..0cfe3d34
--- /dev/null
+++ b/src/test/java/com/thealgorithms/scheduling/SRTFSchedulingTest.java
@@ -0,0 +1,61 @@
+package com.thealgorithms.scheduling;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import com.thealgorithms.devutils.entities.ProcessDetails;
+import java.util.ArrayList;
+import org.junit.jupiter.api.Test;
+
+class SRTFSchedulingTest {
+    ArrayList<ProcessDetails> processes;
+
+    public void initialization() {
+        processes = new ArrayList<>();
+        processes.add(new ProcessDetails("4", 0, 3));
+        processes.add(new ProcessDetails("3", 1, 8));
+        processes.add(new ProcessDetails("1", 2, 6));
+        processes.add(new ProcessDetails("5", 4, 4));
+        processes.add(new ProcessDetails("2", 5, 2));
+    }
+
+    @Test
+    public void Constructor() {
+        initialization();
+        SRTFScheduling s = new SRTFScheduling(processes);
+        assertEquals(3, s.processes.get(0).getBurstTime());
+        assertEquals(8, s.processes.get(1).getBurstTime());
+        assertEquals(6, s.processes.get(2).getBurstTime());
+        assertEquals(4, s.processes.get(3).getBurstTime());
+        assertEquals(2, s.processes.get(4).getBurstTime());
+    }
+
+    @Test
+    void evaluateScheduling() {
+        initialization();
+        SRTFScheduling s = new SRTFScheduling(processes);
+        s.evaluateScheduling();
+        assertEquals("4", s.ready.get(0));
+        assertEquals("4", s.ready.get(1));
+        assertEquals("4", s.ready.get(2));
+        assertEquals("1", s.ready.get(3));
+        assertEquals("5", s.ready.get(4));
+        assertEquals("2", s.ready.get(5));
+        assertEquals("2", s.ready.get(6));
+        assertEquals("5", s.ready.get(7));
+        assertEquals("5", s.ready.get(8));
+        assertEquals("5", s.ready.get(9));
+        assertEquals("1", s.ready.get(10));
+        assertEquals("1", s.ready.get(11));
+        assertEquals("1", s.ready.get(12));
+        assertEquals("1", s.ready.get(13));
+        assertEquals("1", s.ready.get(14));
+        assertEquals("3", s.ready.get(15));
+        assertEquals("3", s.ready.get(16));
+        assertEquals("3", s.ready.get(17));
+        assertEquals("3", s.ready.get(18));
+        assertEquals("3", s.ready.get(19));
+        assertEquals("3", s.ready.get(20));
+        assertEquals("3", s.ready.get(21));
+        assertEquals("3", s.ready.get(22));
+    }
+}