This page provides a collection of code examples related to data structures and algorithms in Java. Whether you are a beginner or an experienced programmer, these examples can help you understand and implement various concepts effectively.

Basic Data Structures

Arrays

Arrays are a fundamental data structure in Java. They provide a way to store a fixed-size sequential collection of elements of the same type.

public class ArrayExample {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        System.out.println(numbers[0]); // Output: 1
    }
}

Lists

Lists are dynamic arrays that allow you to store a collection of elements. In Java, the ArrayList class is commonly used for this purpose.

import java.util.ArrayList;
import java.util.List;

public class ListExample {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        System.out.println(numbers); // Output: [1, 2, 3]
    }
}

Algorithms

Sorting Algorithms

Sorting algorithms are used to arrange elements in a specific order. Some popular sorting algorithms include Bubble Sort, Selection Sort, and Merge Sort.

public class BubbleSortExample {
    public static void main(String[] args) {
        int[] numbers = {5, 2, 8, 12, 1};
        bubbleSort(numbers);
        System.out.println(Arrays.toString(numbers)); // Output: [1, 2, 5, 8, 12]
    }

    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;
                }
            }
        }
    }
}

Search Algorithms

Search algorithms are used to find an element in a collection. One of the most common search algorithms is the Binary Search algorithm.

public class BinarySearchExample {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int target = 5;
        int index = binarySearch(numbers, target);
        if (index != -1) {
            System.out.println("Element found at index: " + index);
        } else {
            System.out.println("Element not found");
        }
    }

    public static int binarySearch(int[] arr, int target) {
        int left = 0;
        int right = arr.length - 1;

        while (left <= right) {
            int mid = left + (right - left) / 2;

            if (arr[mid] == target) {
                return mid;
            } else if (arr[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }

        return -1;
    }
}

For more detailed explanations and additional code examples, you can visit our Data Structures and Algorithms in Java Course.


Sorting Algorithms

Binary Search