0% found this document useful (0 votes)
1 views7 pages

Java DSA Complete Examples With Explanation

Uploaded by

sabeershaik009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views7 pages

Java DSA Complete Examples With Explanation

Uploaded by

sabeershaik009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Beginner-Friendly Java DSA Code

Examples with Explanation


1. Arrays
Arrays store multiple values in a single variable. You can access
each item using its index.

public class ArrayExample {


public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40};

// Traversing the array


for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}

2. Strings
Strings are sequences of characters. You can reverse them by
reading characters from the end.

public class StringExample {


public static void main(String[] args) {
String name = "Java";
String reversed = "";

for (int i = name.length() - 1; i >= 0; i--) {


reversed += name.charAt(i);
}

System.out.println("Reversed: " + reversed);


}
}
3. Linked List
A linked list is a sequence of nodes where each node points to
the next. It's useful for dynamic data.

class Node {
int data;
Node next;

Node(int d) {
data = d;
next = null;
}
}

public class LinkedListExample {


public static void main(String[] args) {
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);

Node temp = head;


while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
}
}

4. Stack
A stack follows LIFO (Last In, First Out). You can push and pop
elements like a stack of plates.

import java.util.Stack;

public class StackExample {


public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();

stack.push(10);
stack.push(20);
stack.push(30);
while (!stack.isEmpty()) {
System.out.println(stack.pop());
}
}
}

5. Queue
A queue follows FIFO (First In, First Out). Think of people
standing in line.

import java.util.LinkedList;
import java.util.Queue;

public class QueueExample {


public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();

queue.add(100);
queue.add(200);
queue.add(300);

while (!queue.isEmpty()) {
System.out.println(queue.poll());
}
}
}

6. HashMap
HashMap stores key-value pairs. Useful when you want to search
values by keys quickly.

import java.util.HashMap;

public class HashMapExample {


public static void main(String[] args) {
HashMap<String, Integer> marks = new HashMap<>();
marks.put("Alice", 90);
marks.put("Bob", 80);
System.out.println(marks.get("Alice"));
}
}

7. Recursion
Recursion means a function calling itself. Useful for solving
problems like factorial.

public class RecursionExample {


static int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}

public static void main(String[] args) {


System.out.println(factorial(5)); // Output: 120
}
}

8. Binary Search
Binary Search finds elements in sorted arrays quickly by dividing
the array into halves.

public class BinarySearch {


public static int search(int[] arr, int key) {
int low = 0, high = arr.length - 1;

while (low <= high) {


int mid = (low + high) / 2;

if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1; // not found
}

public static void main(String[] args) {


int[] arr = {10, 20, 30, 40, 50};
System.out.println(search(arr, 30)); // Output: 2
}
}

9. Binary Tree (Inorder Traversal)


A binary tree has nodes with at most 2 children. Inorder
traversal visits left, root, then right.

class TreeNode {
int data;
TreeNode left, right;

TreeNode(int val) {
data = val;
left = right = null;
}
}

public class BinaryTreeExample {


public static void inorder(TreeNode root) {
if (root != null) {
inorder(root.left);
System.out.print(root.data + " ");
inorder(root.right);
}
}

public static void main(String[] args) {


TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);

inorder(root); // Output: 4 2 1 3
}
}
10. Graph (Using Adjacency List)
A graph can be represented using adjacency list. You can store
connected nodes in a list.

import java.util.*;

public class GraphExample {


public static void main(String[] args) {
int V = 5; // Number of vertices
List<List<Integer>> adj = new ArrayList<>();

for (int i = 0; i < V; i++)


adj.add(new ArrayList<>());

// Add edges
adj.get(0).add(1);
adj.get(0).add(2);
adj.get(1).add(3);
adj.get(2).add(4);

// Print adjacency list


for (int i = 0; i < V; i++) {
System.out.print("Node " + i + ": ");
for (int node : adj.get(i)) {
System.out.print(node + " ");
}
System.out.println();
}
}
}

11. Bubble Sort


Bubble sort compares each pair and swaps if needed, moving the
largest to the end step-by-step.

public class BubbleSort {


public static void main(String[] args) {
int[] arr = {5, 3, 8, 4, 2};

for (int i = 0; i < arr.length - 1; i++) {


for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

for (int num : arr) {


System.out.print(num + " ");
}
}
}

12. Insertion Sort


Insertion sort builds the sorted array one item at a time by
comparing and inserting elements.

public class InsertionSort {


public static void main(String[] args) {
int[] arr = {9, 5, 1, 4, 3};

for (int i = 1; i < arr.length; i++) {


int key = arr[i];
int j = i - 1;

while (j >= 0 && arr[j] > key) {


arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}

for (int num : arr) {


System.out.print(num + " ");
}
}
}

You might also like