An algorithm is a fixed sequence of steps that solves a problem, such as sorting an array or finding a value in it, and this page indexes the Java algorithm tutorials on HowToDoInJava. The sorting, searching and recursion tutorials explain each algorithm with a diagram, give runnable Java code, and compare it with the methods the JDK already provides.
Knowing the common algorithms helps us pick the right tool for our data and understand what Arrays.sort() or a HashMap does for us, and it is also the core of most coding interviews. The sections follow a learning order, from measuring cost and recursion to sorting, searching, string and number problems, and data structures.
The following example shows the JDK methods that run these algorithms for us every day.
int[] scores = {6, 4, 1, 8, 9, 2, 5};
Arrays.sort(scores); // Dual-Pivot Quicksort
int[] sorted = scores; // [1, 2, 4, 5, 6, 8, 9]
int index = Arrays.binarySearch(scores, 8); // 5
List<String> names = new ArrayList<>(List.of("Rita", "Amit", "Lokesh"));
Collections.sort(names); // TimSort, stable
List<String> sortedNames = names; // [Amit, Lokesh, Rita]
Notice that the comments name the algorithm behind each call. The tutorials in each section show how these algorithms work inside and when our own implementation makes sense.
1. Algorithm Basics
Before comparing algorithms, we need a way to measure their cost and a working knowledge of recursion, because many algorithms are defined recursively.
- Big O notation explains how running time and memory grow with the input size, which every complexity table in these tutorials uses.
- Recursion in Java covers base cases, the call stack, memoization and StackOverflowError.
- Tail recursion vs non-tail recursion shows why Java does not optimize tail calls and how loops and trampolines replace them.
2. Sorting Algorithms in Java
Sorting puts elements in order, and each algorithm makes a different trade-off between speed, memory and stability. Stability means that equal elements keep their original order.
| Algorithm | Average time | Worst time | Extra memory | Stable | Use it for |
|---|---|---|---|---|---|
| Quicksort | O(n log n) | O(n2) | O(log n) | No | Large primitive arrays |
| Merge sort | O(n log n) | O(n log n) | O(n) | Yes | Objects, linked lists, external sorting |
| Insertion sort | O(n2) | O(n2) | O(1) | Yes | Small or nearly sorted arrays |
| Selection sort | O(n2) | O(n2) | O(1) | No | Few writes, such as flash memory |
| Bubble sort | O(n2) | O(n2) | O(1) | Yes | Learning how sorting works |
In application code, Arrays.sort() and List.sort() are the right default, because the JDK combines these algorithms. Primitive arrays use Dual-Pivot Quicksort with insertion sort for small ranges, and objects use TimSort, a stable merge sort.
3. Searching Algorithms in Java
Searching finds the position of a value, and the best algorithm depends on whether the data is sorted and how often we search it.
| Algorithm | Needs sorted data | Time | Use it for |
|---|---|---|---|
| Linear search | No | O(n) | Small or unsorted data, conditions |
| Jump search | Yes | O(√n) | Sorted data where moving back is costly |
| Binary search (Arrays.binarySearch()) | Yes | O(log n) | Large sorted arrays in memory |
When the same data is searched many times, a HashSet or HashMap with O(1) average lookups beats every search in the table.
The decision tree combines the sorting and searching choices for everyday Java code.

4. String Algorithms
String problems are frequent coding interview questions, and most of them need one pass over the characters with a map or two indexes.
- Isomorphic strings checks whether one string can be turned into another with a one-to-one character mapping.
- Palindrome string check compares a string with its reverse using two indexes.
- Soundex encodes names by sound, so a search finds Robert when we type Rupert.
- Reverse a string and reverse the words in a string show in-place and StringBuilder approaches.
5. Number Algorithms
Number problems teach loops, integer overflow and the cost of checking divisors.
- Check if a number is prime tests divisors only up to the square root of n.
- Prime numbers from 2 to N uses the sieve of Eratosthenes.
- Factorial in Java compares loops, recursion and BigInteger for large results.
- Find the missing number in a series uses the sum formula instead of sorting.
6. Data Structure Algorithms
The data structure tutorials build or explain data structures, because many algorithms run inside them in practice.
- Stack implementation using an array covers push, pop and resizing.
- Custom list implementation shows how a growable array works.
- Detect a loop in a linked list uses two pointers that move at different speeds.
- How HashMap works explains hashing, buckets and tree bins.
- PriorityQueue is a binary heap that always returns the smallest element first.
7. Concurrency and Security Algorithms
Some algorithms solve problems of shared memory and data protection rather than ordering data.
- Compare and swap (CAS) updates shared values without locks, as the atomic classes do.
- Password hashing with PBKDF2, bcrypt and scrypt explains why fast hashes such as MD5 are not safe for passwords.
- File checksums with SHA-256 verify that a download was not changed.
- AES-256 encryption encrypts and decrypts data with a secret key.
8. Java Algorithms FAQs
Developers who start with algorithms in Java ask these questions most often.
8.1. Which sorting algorithm does Java use?
Arrays.sort() uses Dual-Pivot Quicksort for primitive arrays and TimSort for object arrays. Collections.sort() and List.sort() also use TimSort, which is stable.
8.2. Which algorithms should we learn first for interviews?
We start with Big O, recursion, binary search and two-pointer string problems, and after that learn merge sort and quicksort. Hash maps solve a large share of interview questions, so knowing how HashMap works pays off early.
8.3. Should we write our own sorting algorithm in Java?
Rarely. The JDK sorts are faster and well tested. Writing our own makes sense to learn, for special data such as nearly sorted streams, or when we must control memory or stability in a way the JDK does not offer.
9. Conclusion
The Java algorithms index groups the tutorials by topic, from complexity and recursion to sorting, searching and data structures. The tables help pick an algorithm by its time, memory and stability, and each linked tutorial has runnable code and measurements on Java 25.
10. References
- Arrays (Java SE 25 API)
- Collections (Java SE 25 API)
- Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne
Happy Learning !!
which algorithm is used for java qr code implementation program