Add SortStack algorithm (#5624)

This commit is contained in:
Hardik Pawar
2024-10-08 21:47:56 +05:30
committed by GitHub
parent 0d68b655d2
commit 136e0e23a4
3 changed files with 141 additions and 0 deletions

View File

@ -0,0 +1,60 @@
package com.thealgorithms.stacks;
import java.util.Stack;
/**
* A utility class that provides a method to sort a stack using recursion.
* The elements are sorted in ascending order, with the largest element at the top.
* This algorithm is implemented using only recursion and the original stack,
* without utilizing any additional data structures apart from the stack itself.
*/
public final class SortStack {
private SortStack() {
}
/**
* Sorts the given stack in ascending order using recursion.
* The sorting is performed such that the largest element ends up on top of the stack.
* This method modifies the original stack and does not return a new stack.
*
* The algorithm works as follows:
* 1. Remove the top element.
* 2. Recursively sort the remaining stack.
* 3. Insert the removed element back into the sorted stack at the correct position.
*
* @param stack The stack to be sorted, containing Integer elements.
* @throws IllegalArgumentException if the stack contains `null` elements.
*/
public static void sortStack(Stack<Integer> stack) {
if (stack.isEmpty()) {
return;
}
int top = stack.pop();
sortStack(stack);
insertInSortedOrder(stack, top);
}
/**
* Helper method to insert an element into the correct position in a sorted stack.
* This method is called recursively to place the given element into the stack
* such that the stack remains sorted in ascending order.
*
* The element is inserted in such a way that all elements below it are smaller
* (if the stack is non-empty), and elements above it are larger, maintaining
* the ascending order.
*
* @param stack The stack in which the element needs to be inserted.
* @param element The element to be inserted into the stack in sorted order.
*/
private static void insertInSortedOrder(Stack<Integer> stack, int element) {
if (stack.isEmpty() || element > stack.peek()) {
stack.push(element);
return;
}
int top = stack.pop();
insertInSortedOrder(stack, element);
stack.push(top);
}
}

View File

@ -0,0 +1,77 @@
package com.thealgorithms.stacks;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Stack;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class SortStackTest {
private Stack<Integer> stack;
@BeforeEach
public void setUp() {
stack = new Stack<>();
}
@Test
public void testSortEmptyStack() {
SortStack.sortStack(stack);
assertTrue(stack.isEmpty()); // An empty stack should remain empty
}
@Test
public void testSortSingleElementStack() {
stack.push(10);
SortStack.sortStack(stack);
assertEquals(1, stack.size());
assertEquals(10, (int) stack.peek()); // Single element should remain unchanged
}
@Test
public void testSortAlreadySortedStack() {
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
SortStack.sortStack(stack);
assertEquals(4, stack.size());
assertEquals(4, (int) stack.pop());
assertEquals(3, (int) stack.pop());
assertEquals(2, (int) stack.pop());
assertEquals(1, (int) stack.pop());
}
@Test
public void testSortUnsortedStack() {
stack.push(3);
stack.push(1);
stack.push(4);
stack.push(2);
SortStack.sortStack(stack);
assertEquals(4, stack.size());
assertEquals(4, (int) stack.pop());
assertEquals(3, (int) stack.pop());
assertEquals(2, (int) stack.pop());
assertEquals(1, (int) stack.pop());
}
@Test
public void testSortWithDuplicateElements() {
stack.push(3);
stack.push(1);
stack.push(3);
stack.push(2);
SortStack.sortStack(stack);
assertEquals(4, stack.size());
assertEquals(3, (int) stack.pop());
assertEquals(3, (int) stack.pop());
assertEquals(2, (int) stack.pop());
assertEquals(1, (int) stack.pop());
}
}