Skip to content

Commit 26e4221

Browse files
authored
Merge branch 'master' into stack_array_improve
2 parents faaf62c + f3c2be2 commit 26e4221

17 files changed

+955
-42
lines changed

DIRECTORY.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,22 +523,29 @@
523523
* Recursion
524524
* [GenerateSubsets](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java)
525525
* scheduling
526+
* [AgingScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/AgingScheduling.java)
526527
* diskscheduling
527528
* [CircularLookScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/diskscheduling/CircularLookScheduling.java)
528529
* [CircularScanScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/diskscheduling/CircularScanScheduling.java)
529530
* [LookScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/diskscheduling/LookScheduling.java)
530531
* [ScanScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/diskscheduling/ScanScheduling.java)
531532
* [SSFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/diskscheduling/SSFScheduling.java)
532533
* [EDFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/EDFScheduling.java)
534+
* [FairShareScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FairShareScheduling.java)
533535
* [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java)
536+
* [GangScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/GangScheduling.java)
534537
* [HighestResponseRatioNextScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java)
535538
* [JobSchedulingWithDeadline](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java)
536539
* [LotteryScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/LotteryScheduling.java)
537540
* [MLFQScheduler](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/MLFQScheduler.java)
541+
* [MultiAgentScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/MultiAgentScheduling.java)
538542
* [NonPreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java)
539543
* [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
544+
* [ProportionalFairScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/ProportionalFairScheduling.java)
540545
* [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java)
546+
* [SelfAdjustingScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SelfAdjustingScheduling.java)
541547
* [SJFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SJFScheduling.java)
548+
* [SlackTimeScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SlackTimeScheduling.java)
542549
* [SRTFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SRTFScheduling.java)
543550
* searches
544551
* [BinarySearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/BinarySearch.java)
@@ -1118,22 +1125,29 @@
11181125
* Recursion
11191126
* [GenerateSubsetsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java)
11201127
* scheduling
1128+
* [AgingSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/AgingSchedulingTest.java)
11211129
* diskscheduling
11221130
* [CircularLookSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/diskscheduling/CircularLookSchedulingTest.java)
11231131
* [CircularScanSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/diskscheduling/CircularScanSchedulingTest.java)
11241132
* [LookSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/diskscheduling/LookSchedulingTest.java)
11251133
* [ScanSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/diskscheduling/ScanSchedulingTest.java)
11261134
* [SSFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/diskscheduling/SSFSchedulingTest.java)
11271135
* [EDFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/EDFSchedulingTest.java)
1136+
* [FairShareSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FairShareSchedulingTest.java)
11281137
* [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java)
1138+
* [GangSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/GangSchedulingTest.java)
11291139
* [HighestResponseRatioNextSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java)
11301140
* [JobSchedulingWithDeadlineTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java)
11311141
* [LotterySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/LotterySchedulingTest.java)
11321142
* [MLFQSchedulerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MLFQSchedulerTest.java)
1143+
* [MultiAgentSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MultiAgentSchedulingTest.java)
11331144
* [NonPreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java)
11341145
* [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
1146+
* [ProportionalFairSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/ProportionalFairSchedulingTest.java)
11351147
* [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
1148+
* [SelfAdjustingSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SelfAdjustingSchedulingTest.java)
11361149
* [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java)
1150+
* [SlackTimeSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SlackTimeSchedulingTest.java)
11371151
* [SRTFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SRTFSchedulingTest.java)
11381152
* searches
11391153
* [BinarySearch2dArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BinarySearch2dArrayTest.java)

src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,42 @@
44
import java.util.List;
55

66
/**
7-
* A SortedLinkedList is a data structure that maintains a sorted list of elements.
8-
* Elements are ordered based on their natural ordering or by a Comparator provided at the time of creation.
9-
* This implementation uses a singly linked list to store the elements.
10-
* Further details can be found on this link
7+
* The SortedLinkedList class represents a singly linked list that maintains its elements in sorted order.
8+
* Elements are ordered based on their natural ordering, with smaller elements at the head and larger elements toward the tail.
9+
* The class provides methods for inserting, deleting, and searching elements, as well as checking if the list is empty.
10+
* <p>
11+
* This implementation utilizes a singly linked list to maintain a dynamically sorted list.
12+
* </p>
13+
* <p>
14+
* Further information can be found here:
1115
* https://runestone.academy/ns/books/published/cppds/LinearLinked/ImplementinganOrderedList.html
16+
* </p>
17+
*
18+
* <b>Usage Example:</b>
19+
* <pre>
20+
* SortedLinkedList list = new SortedLinkedList();
21+
* list.insert(10);
22+
* list.insert(5);
23+
* list.insert(20);
24+
* System.out.println(list); // Outputs: [5, 10, 20]
25+
* </pre>
1226
*/
1327
public class SortedLinkedList {
1428
private Node head;
1529
private Node tail;
1630

31+
/**
32+
* Initializes an empty sorted linked list.
33+
*/
1734
public SortedLinkedList() {
1835
this.head = null;
1936
this.tail = null;
2037
}
2138

2239
/**
23-
* Inserts a new element into the sorted linked list.
24-
* @param value the value to be inserted
40+
* Inserts a new integer into the list, maintaining sorted order.
41+
*
42+
* @param value the integer to insert
2543
*/
2644
public void insert(int value) {
2745
Node newNode = new Node(value);
@@ -48,16 +66,10 @@ public void insert(int value) {
4866
}
4967

5068
/**
51-
* Displays the elements of the sorted linked list.
52-
*/
53-
public void display() {
54-
System.out.println(this.toString());
55-
}
56-
57-
/**
58-
* Deletes the first occurrence of the specified element in the sorted linked list.
59-
* @param value the value to be deleted
60-
* @return true if the element is found and deleted, false otherwise
69+
* Deletes the first occurrence of a specified integer in the list.
70+
*
71+
* @param value the integer to delete
72+
* @return {@code true} if the element was found and deleted; {@code false} otherwise
6173
*/
6274
public boolean delete(int value) {
6375
if (this.head == null) {
@@ -87,9 +99,10 @@ public boolean delete(int value) {
8799
}
88100

89101
/**
90-
* Searches for the specified element in the sorted linked list.
91-
* @param value the value to be searched
92-
* @return true if the element is found, false otherwise
102+
* Searches for a specified integer in the list.
103+
*
104+
* @param value the integer to search for
105+
* @return {@code true} if the value is present in the list; {@code false} otherwise
93106
*/
94107
public boolean search(int value) {
95108
Node temp = this.head;
@@ -103,14 +116,17 @@ public boolean search(int value) {
103116
}
104117

105118
/**
106-
* Checks if the sorted linked list is empty.
107-
* @return true if the list is empty, false otherwise
119+
* Checks if the list is empty.
120+
*
121+
* @return {@code true} if the list is empty; {@code false} otherwise
108122
*/
109123
public boolean isEmpty() {
110124
return head == null;
111125
}
126+
112127
/**
113-
* Returns a string representation of the sorted linked list.
128+
* Returns a string representation of the sorted linked list in the format [element1, element2, ...].
129+
*
114130
* @return a string representation of the sorted linked list
115131
*/
116132
@Override
@@ -123,12 +139,14 @@ public String toString() {
123139
temp = temp.next;
124140
}
125141
return "[" + String.join(", ", elements) + "]";
126-
127142
} else {
128143
return "[]";
129144
}
130145
}
131146

147+
/**
148+
* Node represents an element in the sorted linked list.
149+
*/
132150
public final class Node {
133151
public final int value;
134152
public Node next;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
/**
7+
* AgingScheduling is an algorithm designed to prevent starvation
8+
* by gradually increasing the priority of waiting tasks.
9+
* The longer a process waits, the higher its priority becomes.
10+
*
11+
* Use Case: Useful in systems with mixed workloads to avoid
12+
* lower-priority tasks being starved by higher-priority tasks.
13+
*
14+
* @author Hardvan
15+
*/
16+
public final class AgingScheduling {
17+
18+
static class Task {
19+
String name;
20+
int waitTime;
21+
int priority;
22+
23+
Task(String name, int priority) {
24+
this.name = name;
25+
this.priority = priority;
26+
this.waitTime = 0;
27+
}
28+
}
29+
30+
private final Queue<Task> taskQueue;
31+
32+
public AgingScheduling() {
33+
taskQueue = new LinkedList<>();
34+
}
35+
36+
/**
37+
* Adds a task to the scheduler with a given priority.
38+
*
39+
* @param name name of the task
40+
* @param priority priority of the task
41+
*/
42+
public void addTask(String name, int priority) {
43+
taskQueue.offer(new Task(name, priority));
44+
}
45+
46+
/**
47+
* Schedules the next task based on the priority and wait time.
48+
* The priority of a task increases with the time it spends waiting.
49+
*
50+
* @return name of the next task to be executed
51+
*/
52+
public String scheduleNext() {
53+
if (taskQueue.isEmpty()) {
54+
return null;
55+
}
56+
Task nextTask = taskQueue.poll();
57+
nextTask.waitTime++;
58+
nextTask.priority += nextTask.waitTime;
59+
taskQueue.offer(nextTask);
60+
return nextTask.name;
61+
}
62+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* FairShareScheduling allocates CPU resources equally among users or groups
8+
* instead of individual tasks. Each group gets a proportional share,
9+
* preventing resource hogging by a single user's processes.
10+
*
11+
* Use Case: Multi-user systems where users submit multiple tasks simultaneously,
12+
* such as cloud environments.
13+
*
14+
* @author Hardvan
15+
*/
16+
public final class FairShareScheduling {
17+
18+
static class User {
19+
String name;
20+
int allocatedResources;
21+
int totalWeight;
22+
23+
User(String name) {
24+
this.name = name;
25+
this.allocatedResources = 0;
26+
this.totalWeight = 0;
27+
}
28+
29+
void addWeight(int weight) {
30+
this.totalWeight += weight;
31+
}
32+
}
33+
34+
private final Map<String, User> users; // username -> User
35+
36+
public FairShareScheduling() {
37+
users = new HashMap<>();
38+
}
39+
40+
public void addUser(String userName) {
41+
users.putIfAbsent(userName, new User(userName));
42+
}
43+
44+
public void addTask(String userName, int weight) {
45+
User user = users.get(userName);
46+
if (user != null) {
47+
user.addWeight(weight);
48+
}
49+
}
50+
51+
public void allocateResources(int totalResources) {
52+
int totalWeights = users.values().stream().mapToInt(user -> user.totalWeight).sum();
53+
for (User user : users.values()) {
54+
user.allocatedResources = (int) ((double) user.totalWeight / totalWeights * totalResources);
55+
}
56+
}
57+
58+
public Map<String, Integer> getAllocatedResources() {
59+
Map<String, Integer> allocation = new HashMap<>();
60+
for (User user : users.values()) {
61+
allocation.put(user.name, user.allocatedResources);
62+
}
63+
return allocation;
64+
}
65+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**
9+
* GangScheduling groups related tasks (gangs) to run simultaneously on multiple processors.
10+
* All tasks in a gang are executed together or not at all.
11+
*
12+
* Use Case: Parallel computing environments where multiple threads of a program
13+
* need to run concurrently for optimal performance.
14+
*
15+
* @author Hardvan
16+
*/
17+
public final class GangScheduling {
18+
19+
static class Gang {
20+
String name;
21+
List<String> tasks;
22+
23+
Gang(String name) {
24+
this.name = name;
25+
this.tasks = new ArrayList<>();
26+
}
27+
28+
void addTask(String task) {
29+
tasks.add(task);
30+
}
31+
32+
List<String> getTasks() {
33+
return tasks;
34+
}
35+
}
36+
37+
private final Map<String, Gang> gangs;
38+
39+
public GangScheduling() {
40+
gangs = new HashMap<>();
41+
}
42+
43+
public void addGang(String gangName) {
44+
gangs.putIfAbsent(gangName, new Gang(gangName));
45+
}
46+
47+
public void addTaskToGang(String gangName, String task) {
48+
Gang gang = gangs.get(gangName);
49+
if (gang != null) {
50+
gang.addTask(task);
51+
}
52+
}
53+
54+
public Map<String, List<String>> getGangSchedules() {
55+
Map<String, List<String>> schedules = new HashMap<>();
56+
for (Gang gang : gangs.values()) {
57+
schedules.put(gang.name, gang.getTasks());
58+
}
59+
return schedules;
60+
}
61+
}

0 commit comments

Comments
 (0)