01 logo

Java Algorithms — An Overview

A Deep Dive into Binary Search, QuickSort, Depth-First Search, and More!

By Alexander ObregonPublished 10 months ago 4 min read
Like
Source: https://www.oracle.com/java/java-affinity/logos/

Introduction

Algorithm, a term often heard in the realm of computer science, refers to a well-defined sequence of procedures or rules that leads to a solution for a problem. While learning Java, a popular programming language, it’s vital to understand the importance of algorithms. This article will provide a broad overview of algorithms in Java and demonstrate a few examples.

Understanding Algorithms in Java

In Java, algorithms are a set of instructions designed to perform specific tasks. This could be anything from sorting a list of numbers to searching for a particular data point in a database. Algorithms are a fundamental part of Java programming as they provide systematic ways of solving problems and manipulating data.

The efficiency of an algorithm is often measured in terms of time complexity and space complexity, which respectively describe the running time and the amount of computer memory the algorithm requires. Lower complexity usually signifies a more efficient algorithm.

Binary Search Algorithm

A binary search is a quick and efficient algorithm for finding an element in a sorted list of items. It works by repeatedly dividing the search interval in half.

public class BinarySearch {

int binarySearch(int arr[], int x) {

int l = 0, r = arr.length - 1;

while (l <= r) {

int m = l + (r - l) / 2;

// Check if x is present at mid

if (arr[m] == x)

return m;

// If x greater, ignore left half

if (arr[m] < x)

l = m + 1;

// If x is smaller, ignore right half

else

r = m - 1;

}

// If we reach here, then element was not present

return -1;

}

}

In this example, the binary search algorithm starts by comparing the target value (x) to the middle element of the array. If the target value is found, the position is returned. If the target value is not found, the algorithm will continue searching in either the left half or the right half of the array, depending on whether the target value is smaller or larger than the middle element. This process repeats until the value is found or the interval is empty.

Sorting Algorithms

Sorting is another common operation in Java. Various sorting algorithms exist, each with its own trade-offs. Here’s an example of a popular one: the QuickSort algorithm.

QuickSort Algorithm

QuickSort is a divide and conquer algorithm, which works by selecting a ‘pivot’ element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. The sub-arrays are then recursively sorted.

public class QuickSort {

void quickSort(int arr[], int low, int high) {

if (low < high) {

int pi = partition(arr, low, high);

// Recursively sort elements before

// partition and after partition

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

}

}

int partition(int arr[], int low, int high) {

int pivot = arr[high];

int i = (low - 1);

for (int j = low; j <= high - 1; j++) {

if (arr[j] < pivot) {

i++;

// swap arr[i] and arr[j]

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

// swap arr[i+1] and arr[high] (or pivot)

int temp = arr[i + 1];

arr[i + 1] = arr[high];

arr[high] = temp;

return i + 1;

}

}

In this example, the partition() method takes the last element as pivot, places it at its correct position in the sorted array, and places all smaller (smaller than pivot) to the left of the pivot and all greater elements to the right of the pivot.

Graph Algorithms

Graphs are a common data structure in Java that consist of nodes and the edges that connect them. Algorithms that operate on graphs can be used to solve problems such as finding the shortest path between two nodes or determining whether a path exists between two nodes.

Depth-First Search (DFS)

DFS is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root and explores as far as possible along each branch before backtracking. Here’s a simple implementation of DFS using adjacency lists:

import java.util.*;

class Graph {

private int numVertices;

private LinkedList<Integer> adjLists[];

Graph(int vertices) {

numVertices = vertices;

adjLists = new LinkedList[vertices];

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

adjLists[i] = new LinkedList();

}

void addEdge(int src, int dest) {

adjLists[src].add(dest);

}

void DFS(int vertex, boolean visited[]) {

visited[vertex] = true;

System.out.print(vertex + " ");

Iterator<Integer> it = adjLists[vertex].listIterator();

while (it.hasNext()) {

int adj = it.next();

if (!visited[adj])

DFS(adj, visited);

}

}

}

Conclusion

Algorithms are the heart of any computational process, and Java provides an excellent platform for understanding and implementing them. While we’ve discussed several key examples of algorithms here — binary search, QuickSort, and depth-first search — there are many more to explore. Understanding these fundamental algorithms and knowing when to apply them appropriately will significantly improve your problem-solving skills in Java.

Remember that the most suitable algorithm depends on the specific requirements of your software, including factors such as the size and structure of your data, and the trade-offs you’re willing to make between time and space complexity.

With a good grasp of algorithms, you’re well on your way to becoming a proficient Java programmer!

1. Java Collections Framework

2. Algorithm Visualizer

Originally Posted on Medium

tech newshow to
Like

About the Creator

Alexander Obregon

Software Engineer, fervent coder & writer. Devoted to learning & assisting others. Connect on LinkedIn and on Medium.

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.