Skip to content

Commit 2b34778

Browse files
authored
Merge branch 'master' into reverse_stack_improve
2 parents a958e5e + 1e50706 commit 2b34778

19 files changed

+1072
-74
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)
@@ -1119,22 +1126,29 @@
11191126
* Recursion
11201127
* [GenerateSubsetsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java)
11211128
* scheduling
1129+
* [AgingSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/AgingSchedulingTest.java)
11221130
* diskscheduling
11231131
* [CircularLookSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/diskscheduling/CircularLookSchedulingTest.java)
11241132
* [CircularScanSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/diskscheduling/CircularScanSchedulingTest.java)
11251133
* [LookSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/diskscheduling/LookSchedulingTest.java)
11261134
* [ScanSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/diskscheduling/ScanSchedulingTest.java)
11271135
* [SSFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/diskscheduling/SSFSchedulingTest.java)
11281136
* [EDFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/EDFSchedulingTest.java)
1137+
* [FairShareSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FairShareSchedulingTest.java)
11291138
* [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java)
1139+
* [GangSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/GangSchedulingTest.java)
11301140
* [HighestResponseRatioNextSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java)
11311141
* [JobSchedulingWithDeadlineTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java)
11321142
* [LotterySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/LotterySchedulingTest.java)
11331143
* [MLFQSchedulerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MLFQSchedulerTest.java)
1144+
* [MultiAgentSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MultiAgentSchedulingTest.java)
11341145
* [NonPreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java)
11351146
* [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
1147+
* [ProportionalFairSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/ProportionalFairSchedulingTest.java)
11361148
* [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
1149+
* [SelfAdjustingSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SelfAdjustingSchedulingTest.java)
11371150
* [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java)
1151+
* [SlackTimeSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SlackTimeSchedulingTest.java)
11381152
* [SRTFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SRTFSchedulingTest.java)
11391153
* searches
11401154
* [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;

src/main/java/com/thealgorithms/datastructures/stacks/StackArray.java

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package com.thealgorithms.datastructures.stacks;
22

33
/**
4-
* This class implements a Stack using a regular array.
4+
* Implements a generic stack using an array.
5+
*
6+
* <p>This stack automatically resizes when necessary, growing to accommodate additional elements and
7+
* shrinking to conserve memory when its size significantly decreases.
8+
*
9+
* <p>Elements are pushed and popped in LIFO (last-in, first-out) order, where the last element added
10+
* is the first to be removed.
511
*
612
* @param <T> the type of elements in this stack
713
*/
@@ -13,11 +19,20 @@ public class StackArray<T> implements Stack<T> {
1319
private T[] stackArray;
1420
private int top;
1521

22+
/**
23+
* Creates a stack with a default capacity.
24+
*/
1625
@SuppressWarnings("unchecked")
1726
public StackArray() {
1827
this(DEFAULT_CAPACITY);
1928
}
2029

30+
/**
31+
* Creates a stack with a specified initial capacity.
32+
*
33+
* @param size the initial capacity of the stack, must be greater than 0
34+
* @throws IllegalArgumentException if size is less than or equal to 0
35+
*/
2136
@SuppressWarnings("unchecked")
2237
public StackArray(int size) {
2338
if (size <= 0) {
@@ -28,6 +43,11 @@ public StackArray(int size) {
2843
this.top = -1;
2944
}
3045

46+
/**
47+
* Pushes an element onto the top of the stack. Resizes the stack if it is full.
48+
*
49+
* @param value the element to push
50+
*/
3151
@Override
3252
public void push(T value) {
3353
if (isFull()) {
@@ -36,6 +56,13 @@ public void push(T value) {
3656
stackArray[++top] = value;
3757
}
3858

59+
/**
60+
* Removes and returns the element from the top of the stack. Shrinks the stack if
61+
* its size is below a quarter of its capacity, but not below the default capacity.
62+
*
63+
* @return the element removed from the top of the stack
64+
* @throws IllegalStateException if the stack is empty
65+
*/
3966
@Override
4067
public T pop() {
4168
if (isEmpty()) {
@@ -48,6 +75,12 @@ public T pop() {
4875
return value;
4976
}
5077

78+
/**
79+
* Returns the element at the top of the stack without removing it.
80+
*
81+
* @return the top element of the stack
82+
* @throws IllegalStateException if the stack is empty
83+
*/
5184
@Override
5285
public T peek() {
5386
if (isEmpty()) {
@@ -56,28 +89,72 @@ public T peek() {
5689
return stackArray[top];
5790
}
5891

92+
/**
93+
* Resizes the internal array to a new capacity.
94+
*
95+
* @param newSize the new size of the stack array
96+
*/
5997
private void resize(int newSize) {
6098
@SuppressWarnings("unchecked") T[] newArray = (T[]) new Object[newSize];
6199
System.arraycopy(stackArray, 0, newArray, 0, top + 1);
62100
stackArray = newArray;
63101
maxSize = newSize;
64102
}
65103

104+
/**
105+
* Checks if the stack is full.
106+
*
107+
* @return true if the stack is full, false otherwise
108+
*/
66109
public boolean isFull() {
67110
return top + 1 == maxSize;
68111
}
69112

113+
/**
114+
* Checks if the stack is empty.
115+
*
116+
* @return true if the stack is empty, false otherwise
117+
*/
70118
@Override
71119
public boolean isEmpty() {
72120
return top == -1;
73121
}
74122

75-
@Override public void makeEmpty() { // Doesn't delete elements in the array but if you call
76-
top = -1; // push method after calling makeEmpty it will overwrite previous values
123+
/**
124+
* Empties the stack, marking it as empty without deleting elements. Elements are
125+
* overwritten on subsequent pushes.
126+
*/
127+
@Override
128+
public void makeEmpty() {
129+
top = -1;
77130
}
78131

132+
/**
133+
* Returns the number of elements currently in the stack.
134+
*
135+
* @return the size of the stack
136+
*/
79137
@Override
80138
public int size() {
81139
return top + 1;
82140
}
141+
142+
/**
143+
* Returns a string representation of the stack.
144+
*
145+
* @return a string representation of the stack
146+
*/
147+
@Override
148+
public String toString() {
149+
StringBuilder sb = new StringBuilder();
150+
sb.append("StackArray [");
151+
for (int i = 0; i <= top; i++) {
152+
sb.append(stackArray[i]);
153+
if (i < top) {
154+
sb.append(", ");
155+
}
156+
}
157+
sb.append("]");
158+
return sb.toString();
159+
}
83160
}
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+
}

0 commit comments

Comments
 (0)