NBKRIST Java Hub - Data Structures & Algorithms with Java

Beginner-Friendly Guide with Practical Java Examples

← Back to Home

Industry Expert Insight

As an IT professional, I have worked on multiple software projects where understanding **Data Structures and Algorithms (DSA)** was crucial. Using Java, I built efficient stacks, queues, and implemented sorting and searching algorithms to solve real-world problems such as inventory management, scheduling systems, and data analysis tools.

Learning DSA with Java not only teaches you programming logic, but also prepares you for coding interviews, competitive programming, and building scalable software.

💡 Tip: Practice DSA with Java arrays first—they give a clear understanding of how data is stored and manipulated in memory.

Build Stack using Java Arrays

A stack is a LIFO (Last In First Out) data structure. Here’s a simple Java example using arrays:

class StackExample {
    int[] stack = new int[5];
    int top = -1;

    void push(int value) {
        if(top < stack.length - 1) {
            stack[++top] = value;
            System.out.println(value + " pushed to stack");
        } else {
            System.out.println("Stack Overflow");
        }
    }

    void pop() {
        if(top >= 0) {
            System.out.println(stack[top--] + " popped from stack");
        } else {
            System.out.println("Stack Underflow");
        }
    }

    public static void main(String[] args) {
        StackExample s = new StackExample();
        s.push(10);
        s.push(20);
        s.pop();
    }
}

Build Queue using Java Arrays

A queue is a FIFO (First In First Out) data structure. Here’s a simple Java example using arrays:

class QueueExample {
    int[] queue = new int[5];
    int front = 0, rear = -1;

    void enqueue(int value) {
        if(rear < queue.length - 1) {
            queue[++rear] = value;
            System.out.println(value + " added to queue");
        } else {
            System.out.println("Queue Full");
        }
    }

    void dequeue() {
        if(front <= rear) {
            System.out.println(queue[front++] + " removed from queue");
        } else {
            System.out.println("Queue Empty");
        }
    }

    public static void main(String[] args) {
        QueueExample q = new QueueExample();
        q.enqueue(5);
        q.enqueue(10);
        q.dequeue();
    }
}

Sorting of Arrays using Simple Algorithm

Sorting is essential to organize data. Here’s an example of **bubble sort** in Java:

class BubbleSortExample {
    public static void main(String[] args) {
        int[] arr = {5, 2, 9, 1, 3};
        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;
                }
            }
        }
        System.out.println("Sorted Array:");
        for(int num : arr)
            System.out.print(num + " ");
    }
}

Searching using Binary Search Algorithm

Binary search efficiently finds an element in a **sorted array**. Here’s a Java example:

class BinarySearchExample {
    public static void main(String[] args) {
        int[] arr = {1, 3, 5, 7, 9};
        int target = 5;
        int left = 0, right = arr.length - 1;
        boolean found = false;

        while(left <= right) {
            int mid = left + (right - left)/2;
            if(arr[mid] == target) {
                found = true;
                System.out.println(target + " found at index " + mid);
                break;
            } else if(arr[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }

        if(!found)
            System.out.println(target + " not found in array");
    }
}
← Back to Home