Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/com/thealgorithms/searches/BinarySearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
* complexity: O(log n) - Element not found or at extreme end - Space complexity: O(1) - Only uses
* a constant amount of extra space
*
* <p><strong>Edge Cases:</strong>
* <ul>
* <li>Empty array: Returns -1</li>
* <li>Null array: Returns -1</li>
* <li>Null key: Returns -1</li>
* <li>Element not found: Returns -1</li>
* <li>Duplicate elements: May return any one valid index of the target</li>
* </ul>
*
* <p>Example Walkthrough: Array: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] Target: 7
*
* <p>Step 1: left=0, right=9, mid=4, array[4]=9 (9 &gt; 7, search left half) Step 2: left=0,
Expand Down
11 changes: 11 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 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