Introduction to Algorithms and Data Structures in Java
Learning algorithms and data structures is the foundation on which any programmer's career is built. For Java developers, understanding how basic data structures and algorithms work is critically important: it directly affects performance, code readability, and the ability to solve complex problems. In this article, we will break down key concepts in simple language, with clear code examples in Java.
Whether you are preparing for an interview or simply want to write more efficient code, this article will be your starting point.
What are Data Structures?
A data structure is a way of organizing and storing data in computer memory. Choosing the right data structure can speed up a program tenfold. In Java, there are two types of data structures: built-in (arrays) and collections (classes from the Java Collections Framework).
Array
An array is the simplest and fastest data structure. It stores elements of the same type in a contiguous memory area. Accessing an element by index is instantaneous (O(1)), but adding or removing elements requires recreating the array.
public class ArrayExample { public static void main(String[] args) { // Declaring and initializing an array int[] numbers = new int[5]; numbers[0] = 10; numbers[1] = 20; numbers[2] = 30; // Alternative way String[] fruits = {"Apple", "Banana", "Orange"}; // Traversing the array for (int i = 0; i < fruits.length; i++) { System.out.println(fruits[i]); } }}ArrayList — Dynamic Array
ArrayList solves the main problem of a regular array — fixed size. It automatically expands when new elements are added. This is one of the most popular data structures in Java.
import java.util.ArrayList;
public class ArrayListExample { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); // Adding elements list.add("Java"); list.add("Python"); list.add("JavaScript"); // Getting an element by index System.out.println("First element: " + list.get(0)); // Removing an element list.remove("Python"); // List size System.out.println("List size: " + list.size()); }}Basic Algorithms for Beginners
An algorithm is a sequence of steps to solve a problem. We will look at three basic algorithms that every Java developer should know.
Linear Search
The simplest search algorithm: we sequentially check each element of the array until we find the desired one.
public class LinearSearch { public static int findElement(int[] arr, int target) { for (int i = 0; i < arr.length; i++) { if (arr[i] == target) { return i; // Return the index of the found element } } return -1; // Element not found } public static void main(String[] args) { int[] data = {5, 3, 8, 1, 9, 2}; int index = findElement(data, 8); System.out.println("Index of element 8: " + index); // Will output: 2 }}Binary Search
Binary search works significantly faster than linear search, but requires the array to be sorted. The algorithm divides the array in half at each step, discarding half of the elements.
import java.util.Arrays;
public class BinarySearch { public static int binarySearch(int[] arr, int target) { int left = 0; int right = arr.length - 1; while (left <= right) { int mid = l