Skip to main content
Java Basics
CHAPTER 29 Beginner

Data Structures and Algorithms in Java

Updated: May 17, 2026
5 min read

# CHAPTER 29

Data Structures and Algorithms in Java

1. Introduction

Data Structures and Algorithms (DSA) are the foundation of computer science and the core of technical interviews at top companies. Understanding how to store and manipulate data efficiently separates good developers from great ones.

2. Arrays Review

Arrays offer O(1) random access but O(n) insertion/deletion.
java
123
int[] arr = {10, 20, 30, 40, 50};
// Access: arr[2] → O(1)
// Search: Linear scan → O(n)

3. Linked List (Custom Implementation)

java
12345678910111213141516171819202122232425262728293031323334
class Node {
    int data;
    Node next;
    Node(int data) { this.data = data; this.next = null; }
}

class LinkedList {
    Node head;

    void insertAtEnd(int data) {
        Node newNode = new Node(data);
        if (head == null) { head = newNode; return; }
        Node temp = head;
        while (temp.next != null) temp = temp.next;
        temp.next = newNode;
    }

    void display() {
        Node temp = head;
        while (temp != null) {
            System.out.print(temp.data + " → ");
            temp = temp.next;
        }
        System.out.println("null");
    }

    void deleteNode(int key) {
        if (head == null) return;
        if (head.data == key) { head = head.next; return; }
        Node prev = head;
        while (prev.next != null && prev.next.data != key) prev = prev.next;
        if (prev.next != null) prev.next = prev.next.next;
    }
}

4. Stack (LIFO)

java
12345678910111213141516171819
class Stack {
    private int[] arr;
    private int top;

    Stack(int size) { arr = new int[size]; top = -1; }

    void push(int value) {
        if (top == arr.length - 1) { System.out.println("Stack Overflow!"); return; }
        arr[++top] = value;
    }

    int pop() {
        if (top == -1) { System.out.println("Stack Underflow!"); return -1; }
        return arr[top--];
    }

    int peek() { return top == -1 ? -1 : arr[top]; }
    boolean isEmpty() { return top == -1; }
}

5. Queue (FIFO)

java
1234567891011121314151617181920212223
class Queue {
    private int[] arr;
    private int front, rear, size;

    Queue(int capacity) {
        arr = new int[capacity]; front = 0; rear = -1; size = 0;
    }

    void enqueue(int value) {
        if (size == arr.length) { System.out.println("Queue Full!"); return; }
        rear = (rear + 1) % arr.length;
        arr[rear] = value;
        size++;
    }

    int dequeue() {
        if (size == 0) { System.out.println("Queue Empty!"); return -1; }
        int value = arr[front];
        front = (front + 1) % arr.length;
        size--;
        return value;
    }
}

6. Sorting Algorithms

Bubble Sort — O(n²)

java
12345678910
public static void bubbleSort(int[] arr) {
    int n = arr.length;
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp;
            }
        }
    }
}

Selection Sort — O(n²)

java
123456789
public static void selectionSort(int[] arr) {
    for (int i = 0; i < arr.length - 1; i++) {
        int minIdx = i;
        for (int j = i + 1; j < arr.length; j++) {
            if (arr[j] < arr[minIdx]) minIdx = j;
        }
        int temp = arr[minIdx]; arr[minIdx] = arr[i]; arr[i] = temp;
    }
}

Insertion Sort — O(n²)

java
1234567
public static void insertionSort(int[] arr) {
    for (int i = 1; i < arr.length; i++) {
        int key = arr[i], j = i - 1;
        while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; }
        arr[j + 1] = key;
    }
}

7. Searching Algorithms

Linear Search — O(n)

java
123456
public static int linearSearch(int[] arr, int target) {
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] == target) return i;
    }
    return -1;
}

Binary Search — O(log n) (array must be sorted)

java
12345678910
public static int binarySearch(int[] arr, int target) {
    int low = 0, high = arr.length - 1;
    while (low <= high) {
        int mid = low + (high - low) / 2;
        if (arr[mid] == target) return mid;
        else if (arr[mid] < target) low = mid + 1;
        else high = mid - 1;
    }
    return -1;
}

8. Big-O Complexity Comparison

AlgorithmBestAverageWorst
Bubble SortO(n)O(n²)O(n²)
Selection SortO(n²)O(n²)O(n²)
Insertion SortO(n)O(n²)O(n²)
Linear SearchO(1)O(n)O(n)
Binary SearchO(1)O(log n)O(log n)

9. MCQ Quiz with Answers

Question 1

Stack follows:

Question 2

Queue follows:

Question 3

Binary search requires:

Question 4

Bubble sort time complexity (worst)?

Question 5

Binary search time complexity?

Question 6

Stack operations are:

Question 7

Linked list advantage over array?

Question 8

Queue operations are:

Question 9

Which sort is best for nearly sorted arrays?

Question 10

Stack Overflow occurs when:

10. Summary

Data structures (arrays, linked lists, stacks, queues) organize data efficiently. Sorting algorithms (bubble, selection, insertion) arrange data. Searching algorithms (linear, binary) find data. Understanding time complexity helps choose the right tool for the job.

11. Next Chapter Recommendation

In Chapter 30: Final Projects and Real-World Applications, we'll build complete Java applications applying everything we've learned.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·