style: format code (#4212)

close #4204
This commit is contained in:
acbin
2023-06-09 18:52:05 +08:00
committed by GitHub
parent ad03086f54
commit 00282efd8b
521 changed files with 5233 additions and 7309 deletions

View File

@ -1,11 +1,11 @@
package com.thealgorithms.scheduling;
import com.thealgorithms.devutils.entities.ProcessDetails;
import java.util.List;
/**
* Non-pre-emptive First Come First Serve scheduling. This can be understood here - https://www.scaler.com/topics/first-come-first-serve/
* Non-pre-emptive First Come First Serve scheduling. This can be understood here -
* https://www.scaler.com/topics/first-come-first-serve/
*/
public class FCFSScheduling {
@ -23,25 +23,27 @@ public class FCFSScheduling {
private void evaluateWaitingTime() {
int processesNumber = processes.size();
if(processesNumber == 0) {
if (processesNumber == 0) {
return;
}
int waitingTime = 0;
int burstTime = processes.get(0).getBurstTime();
processes.get(0).setWaitingTime(waitingTime); // for the first process, waiting time will be 0.
processes.get(0).setWaitingTime(
waitingTime); // for the first process, waiting time will be 0.
for(int i=1; i<processesNumber; i++) {
processes.get(i).setWaitingTime(waitingTime + burstTime);
waitingTime = processes.get(i).getWaitingTime();
burstTime = processes.get(i).getBurstTime();
for (int i = 1; i < processesNumber; i++) {
processes.get(i).setWaitingTime(waitingTime + burstTime);
waitingTime = processes.get(i).getWaitingTime();
burstTime = processes.get(i).getBurstTime();
}
}
private void evaluateTurnAroundTime() {
for(int i=0; i<processes.size(); i++) {
processes.get(i).setTurnAroundTimeTime(processes.get(i).getBurstTime() + processes.get(i).getWaitingTime());
for (int i = 0; i < processes.size(); i++) {
processes.get(i).setTurnAroundTimeTime(
processes.get(i).getBurstTime() + processes.get(i).getWaitingTime());
}
}
}

View File

@ -5,15 +5,15 @@
package com.thealgorithms.scheduling;
import com.thealgorithms.devutils.entities.ProcessDetails;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* The Round-robin scheduling algorithm is a kind of preemptive First come, First Serve CPU Scheduling algorithm.
* This can be understood here - https://www.scaler.com/topics/round-robin-scheduling-in-os/
* The Round-robin scheduling algorithm is a kind of preemptive First come, First Serve CPU
* Scheduling algorithm. This can be understood here -
* https://www.scaler.com/topics/round-robin-scheduling-in-os/
*/
public class RRScheduling {
@ -33,7 +33,7 @@ public class RRScheduling {
private void evaluateTurnAroundTime() {
int processesNumber = processes.size();
if(processesNumber == 0) {
if (processesNumber == 0) {
return;
}
@ -51,38 +51,42 @@ public class RRScheduling {
remainingBurstTime[i] = processes.get(i).getBurstTime();
}
while (completed != processesNumber){
while (completed != processesNumber) {
int index = queue.poll();
if(remainingBurstTime[index] == processes.get(index).getBurstTime()){
if (remainingBurstTime[index] == processes.get(index).getBurstTime()) {
currentTime = Math.max(currentTime, processes.get(index).getArrivalTime());
}
if(remainingBurstTime[index] - quantumTime > 0){
if (remainingBurstTime[index] - quantumTime > 0) {
remainingBurstTime[index] -= quantumTime;
currentTime += quantumTime;
} else {
currentTime += remainingBurstTime[index];
processes.get(index).setTurnAroundTimeTime(currentTime - processes.get(index).getArrivalTime());
processes.get(index).setTurnAroundTimeTime(
currentTime - processes.get(index).getArrivalTime());
completed++;
remainingBurstTime[index]=0;
remainingBurstTime[index] = 0;
}
// If some process has arrived when this process was executing, insert them into the queue.
for (int i=1; i < processesNumber; i++){
if(remainingBurstTime[i] > 0 && processes.get(i).getArrivalTime() <= currentTime && mark[i] == 0){
mark[i]=1;
// If some process has arrived when this process was executing, insert them into the
// queue.
for (int i = 1; i < processesNumber; i++) {
if (remainingBurstTime[i] > 0 && processes.get(i).getArrivalTime() <= currentTime
&& mark[i] == 0) {
mark[i] = 1;
queue.add(i);
}
}
// If the current process has burst time remaining, push the process into the queue again.
if(remainingBurstTime[index] > 0) queue.add(index);
// If the current process has burst time remaining, push the process into the queue
// again.
if (remainingBurstTime[index] > 0) queue.add(index);
// If the queue is empty, pick the first process from the list that is not completed.
if(queue.isEmpty()){
for (int i=1; i<processesNumber; i++){
if (remainingBurstTime[i] > 0){
if (queue.isEmpty()) {
for (int i = 1; i < processesNumber; i++) {
if (remainingBurstTime[i] > 0) {
mark[i] = 1;
queue.add(i);
break;
@ -94,6 +98,7 @@ public class RRScheduling {
private void evaluateWaitingTime() {
for (int i = 0; i < processes.size(); i++)
processes.get(i).setWaitingTime(processes.get(i).getTurnAroundTimeTime() - processes.get(i).getBurstTime());
processes.get(i).setWaitingTime(
processes.get(i).getTurnAroundTimeTime() - processes.get(i).getBurstTime());
}
}

View File

@ -1,17 +1,17 @@
package com.thealgorithms.scheduling;
import com.thealgorithms.devutils.entities.ProcessDetails;
import java.util.ArrayList;
/**
* Implementation of Shortest Job First Algorithm: The algorithm allows the waiting process with the minimal burst time to be executed first.
* see more here: https://www.guru99.com/shortest-job-first-sjf-scheduling.html
* Implementation of Shortest Job First Algorithm: The algorithm allows the waiting process with the
* minimal burst time to be executed first. see more here:
* https://www.guru99.com/shortest-job-first-sjf-scheduling.html
*/
public class SJFScheduling {
protected ArrayList<ProcessDetails> processes;
protected ArrayList<String>schedule ;
protected ArrayList<String> schedule;
/**
* a simple constructor
@ -20,85 +20,79 @@ public class SJFScheduling {
*/
SJFScheduling(final ArrayList<ProcessDetails> processes) {
this.processes = processes;
schedule=new ArrayList<>();
schedule = new ArrayList<>();
sortByArrivalTime();
}
protected void sortByArrivalTime() {
int size=processes.size(),i,j;
protected void sortByArrivalTime() {
int size = processes.size(), i, j;
ProcessDetails temp;
for(i=0;i<size;i++)
{
for(j=i+1;j<size-1;j++)
{
if(processes.get(j).getArrivalTime()>processes.get(j+1).getArrivalTime())
{
temp=processes.get(j);
processes.set(j,processes.get(j+1));
processes.set(j+1,temp);
for (i = 0; i < size; i++) {
for (j = i + 1; j < size - 1; j++) {
if (processes.get(j).getArrivalTime() > processes.get(j + 1).getArrivalTime()) {
temp = processes.get(j);
processes.set(j, processes.get(j + 1));
processes.set(j + 1, temp);
}
}
}
}
}
/**
* this functions returns the order of the executions
*/
public void scheduleProcesses() {
ArrayList<ProcessDetails> ready=new ArrayList<>();
ArrayList<ProcessDetails> ready = new ArrayList<>();
int size = processes.size(),runtime,time=0;
int executed=0,j,k=0;
int size = processes.size(), runtime, time = 0;
int executed = 0, j, k = 0;
ProcessDetails running;
if (size == 0) {
return;
}
while(executed<size)
{
while(k<size && processes.get(k).getArrivalTime()<=time)//here we find the processes that have arrived.
while (executed < size) {
while (k < size
&& processes.get(k).getArrivalTime()
<= time) // here we find the processes that have arrived.
{
ready.add(processes.get(k));
k++;
}
running=findShortestJob(ready);
if(running==null)
{
time++;
running = findShortestJob(ready);
if (running == null) {
time++;
} else {
runtime = running.getBurstTime();
for (j = 0; j < runtime; j++) {
time++;
}
schedule.add(running.getProcessId());
ready.remove(running);
executed++;
}
else {
runtime = running.getBurstTime();
for (j = 0; j < runtime; j++) {
time++;}
schedule.add(running.getProcessId());
ready.remove(running);
executed++;
}
}
}
/**
* this function evaluates the shortest job of all the ready processes (based on a process burst time)
* this function evaluates the shortest job of all the ready processes (based on a process
* burst time)
* @param ReadyProcesses an array list of ready processes
* @return returns the process' with the shortest burst time OR NULL if there are no ready processes
* @return returns the process' with the shortest burst time OR NULL if there are no ready
* processes
*/
private ProcessDetails findShortestJob(ArrayList<ProcessDetails> ReadyProcesses) {
if (ReadyProcesses.isEmpty()){
if (ReadyProcesses.isEmpty()) {
return null;
}
int i,size = ReadyProcesses.size();
int i, size = ReadyProcesses.size();
int minBurstTime = ReadyProcesses.get(0).getBurstTime(), temp, positionOfShortestJob = 0;
for (i = 1; i < size; i++) {
temp = ReadyProcesses.get(i).getBurstTime();
if (minBurstTime > temp ) {
if (minBurstTime > temp) {
minBurstTime = temp;
positionOfShortestJob = i;
}
@ -106,10 +100,4 @@ protected void sortByArrivalTime() {
return ReadyProcesses.get(positionOfShortestJob);
}
}
}