Skip to content

Commit 4668a37

Browse files
authored
Merge branch 'master' into improve-power-sum-algorithm
2 parents 07e0482 + 136e0e2 commit 4668a37

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@
567567
* [NextSmallerElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/NextSmallerElement.java)
568568
* [PostfixToInfix](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PostfixToInfix.java)
569569
* [PrefixToInfix](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PrefixToInfix.java)
570+
* [SortStack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/SortStack.java)
570571
* [StackPostfixNotation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java)
571572
* strings
572573
* [AhoCorasick](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/AhoCorasick.java)
@@ -1029,6 +1030,7 @@
10291030
* [NextSmallerElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/NextSmallerElementTest.java)
10301031
* [PostfixToInfixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PostfixToInfixTest.java)
10311032
* [PrefixToInfixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PrefixToInfixTest.java)
1033+
* [SortStackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/SortStackTest.java)
10321034
* [StackPostfixNotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/StackPostfixNotationTest.java)
10331035
* strings
10341036
* [AhoCorasickTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/AhoCorasickTest.java)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.thealgorithms.stacks;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* A utility class that provides a method to sort a stack using recursion.
7+
* The elements are sorted in ascending order, with the largest element at the top.
8+
* This algorithm is implemented using only recursion and the original stack,
9+
* without utilizing any additional data structures apart from the stack itself.
10+
*/
11+
public final class SortStack {
12+
private SortStack() {
13+
}
14+
15+
/**
16+
* Sorts the given stack in ascending order using recursion.
17+
* The sorting is performed such that the largest element ends up on top of the stack.
18+
* This method modifies the original stack and does not return a new stack.
19+
*
20+
* The algorithm works as follows:
21+
* 1. Remove the top element.
22+
* 2. Recursively sort the remaining stack.
23+
* 3. Insert the removed element back into the sorted stack at the correct position.
24+
*
25+
* @param stack The stack to be sorted, containing Integer elements.
26+
* @throws IllegalArgumentException if the stack contains `null` elements.
27+
*/
28+
public static void sortStack(Stack<Integer> stack) {
29+
if (stack.isEmpty()) {
30+
return;
31+
}
32+
33+
int top = stack.pop();
34+
sortStack(stack);
35+
insertInSortedOrder(stack, top);
36+
}
37+
38+
/**
39+
* Helper method to insert an element into the correct position in a sorted stack.
40+
* This method is called recursively to place the given element into the stack
41+
* such that the stack remains sorted in ascending order.
42+
*
43+
* The element is inserted in such a way that all elements below it are smaller
44+
* (if the stack is non-empty), and elements above it are larger, maintaining
45+
* the ascending order.
46+
*
47+
* @param stack The stack in which the element needs to be inserted.
48+
* @param element The element to be inserted into the stack in sorted order.
49+
*/
50+
private static void insertInSortedOrder(Stack<Integer> stack, int element) {
51+
if (stack.isEmpty() || element > stack.peek()) {
52+
stack.push(element);
53+
return;
54+
}
55+
56+
int top = stack.pop();
57+
insertInSortedOrder(stack, element);
58+
stack.push(top);
59+
}
60+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.util.Stack;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class SortStackTest {
11+
12+
private Stack<Integer> stack;
13+
14+
@BeforeEach
15+
public void setUp() {
16+
stack = new Stack<>();
17+
}
18+
19+
@Test
20+
public void testSortEmptyStack() {
21+
SortStack.sortStack(stack);
22+
assertTrue(stack.isEmpty()); // An empty stack should remain empty
23+
}
24+
25+
@Test
26+
public void testSortSingleElementStack() {
27+
stack.push(10);
28+
SortStack.sortStack(stack);
29+
assertEquals(1, stack.size());
30+
assertEquals(10, (int) stack.peek()); // Single element should remain unchanged
31+
}
32+
33+
@Test
34+
public void testSortAlreadySortedStack() {
35+
stack.push(1);
36+
stack.push(2);
37+
stack.push(3);
38+
stack.push(4);
39+
SortStack.sortStack(stack);
40+
41+
assertEquals(4, stack.size());
42+
assertEquals(4, (int) stack.pop());
43+
assertEquals(3, (int) stack.pop());
44+
assertEquals(2, (int) stack.pop());
45+
assertEquals(1, (int) stack.pop());
46+
}
47+
48+
@Test
49+
public void testSortUnsortedStack() {
50+
stack.push(3);
51+
stack.push(1);
52+
stack.push(4);
53+
stack.push(2);
54+
SortStack.sortStack(stack);
55+
56+
assertEquals(4, stack.size());
57+
assertEquals(4, (int) stack.pop());
58+
assertEquals(3, (int) stack.pop());
59+
assertEquals(2, (int) stack.pop());
60+
assertEquals(1, (int) stack.pop());
61+
}
62+
63+
@Test
64+
public void testSortWithDuplicateElements() {
65+
stack.push(3);
66+
stack.push(1);
67+
stack.push(3);
68+
stack.push(2);
69+
SortStack.sortStack(stack);
70+
71+
assertEquals(4, stack.size());
72+
assertEquals(3, (int) stack.pop());
73+
assertEquals(3, (int) stack.pop());
74+
assertEquals(2, (int) stack.pop());
75+
assertEquals(1, (int) stack.pop());
76+
}
77+
}

0 commit comments

Comments
 (0)