Skip to content
Open
18 changes: 18 additions & 0 deletions src/main/java/com/thealgorithms/searches/JumpSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@
* Result: Index = 4
*
* <p>
* <b>Example (Simple):</b><br>
* Input: arr = [2, 4, 6, 8, 10], key = 8<br>
* Output: 3
*
* <p>
* <b>Explanation (Easy):</b><br>
* Instead of checking every element one by one, Jump Search skips elements
* by jumping ahead fixed steps (√n). Once it finds a range where the element
* might exist, it performs a linear search in that smaller block.
*
* <p>
* <b>Time Complexity:</b><br>
* - Best-case: O(1) - element found at first position<br>
* - Average: O(√n) - optimal block size reduces jumps<br>
Expand All @@ -36,6 +47,13 @@
* <b>Space Complexity:</b> O(1) - only uses a constant amount of extra space
*
* <p>
* <b>Edge Cases:</b>
* <ul>
* <li>Empty array → returns -1</li>
* <li>Element not present → returns -1</li>
* <li>Single element array</li>
* </ul>
* <p>
* <b>Note:</b> Jump Search requires a sorted array. For unsorted arrays, use Linear Search.
* Compared to Linear Search (O(n)), Jump Search is faster for large arrays.
* Compared to Binary Search (O(log n)), Jump Search is less efficient but may be
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.thealgorithms.stacks;

/**
* A class that implements a Stack using a singly linked list.
* Supports basic operations like push, pop, peek, and isEmpty.
*
* Reference: https://www.geeksforgeeks.org/stack-using-linked-list/
*/
public class StackUsingLinkedList {

/**
* Node class representing each element in the stack
*/
private static class Node {
int data;
Node next;

Node(int data) {
this.data = data;
}
}

private Node top;

/**
* Push an element onto the stack
*
* @param value the value to push
*/
public void push(int value) {
Node newNode = new Node(value);
newNode.next = top;
top = newNode;
}

/**
* Remove and return the top element of the stack
*
* @return top element
*/
public int pop() {
if (top == null) {
throw new RuntimeException("Stack is empty");
}
int value = top.data;
top = top.next;
return value;
}

/**
* Return the top element without removing
*
* @return top element
*/
public int peek() {
if (top == null) {
throw new RuntimeException("Stack is empty");
}
return top.data;
}

/**
* Check if the stack is empty
*
* @return true if empty, false otherwise
*/
public boolean isEmpty() {
return top == null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.thealgorithms.stacks;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

/**
* Test class for StackUsingLinkedList.
*
* This class contains unit tests to verify the correctness
* of stack operations such as push, pop, peek, and isEmpty.
*
* Reference: https://www.geeksforgeeks.org/stack-using-linked-list/
*/
class StackUsingLinkedListTest {

/**
* Test push and pop operations
*/
@Test
void testPushAndPop() {
StackUsingLinkedList stack = new StackUsingLinkedList();
stack.push(10);
stack.push(20);

assertEquals(20, stack.pop());
assertEquals(10, stack.pop());
}

/**
* Test peek operation
*/
@Test
void testPeek() {
StackUsingLinkedList stack = new StackUsingLinkedList();
stack.push(5);

assertEquals(5, stack.peek());
}

/**
* Test isEmpty method
*/
@Test
void testIsEmpty() {
StackUsingLinkedList stack = new StackUsingLinkedList();

assertTrue(stack.isEmpty());
stack.push(1);
assertFalse(stack.isEmpty());
}

/**
* Test pop on empty stack (edge case)
*/
@Test
void testPopOnEmptyStack() {
StackUsingLinkedList stack = new StackUsingLinkedList();

assertThrows(RuntimeException.class, stack::pop);
}

/**
* Test peek on empty stack (edge case)
*/
@Test
void testPeekOnEmptyStack() {
StackUsingLinkedList stack = new StackUsingLinkedList();

assertThrows(RuntimeException.class, stack::peek);
}
}
Loading