Java Algorithms: Sorting, Searching and More

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.

AlgorithmAverage timeWorst timeExtra memoryStableUse it for
QuicksortO(n log n)O(n2)O(log n)NoLarge primitive arrays
Merge sortO(n log n)O(n log n)O(n)YesObjects, linked lists, external sorting
Insertion sortO(n2)O(n2)O(1)YesSmall or nearly sorted arrays
Selection sortO(n2)O(n2)O(1)NoFew writes, such as flash memory
Bubble sortO(n2)O(n2)O(1)YesLearning 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.

AlgorithmNeeds sorted dataTimeUse it for
Linear searchNoO(n)Small or unsorted data, conditions
Jump searchYesO(√n)Sorted data where moving back is costly
Binary search (Arrays.binarySearch())YesO(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.

Decision tree for choosing a Java sorting or searching algorithm
The JDK defaults in green cover most cases, and the yellow cases suit special data

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.

5. Number Algorithms

Number problems teach loops, integer overflow and the cost of checking divisors.

6. Data Structure Algorithms

The data structure tutorials build or explain data structures, because many algorithms run inside them in practice.

7. Concurrency and Security Algorithms

Some algorithms solve problems of shared memory and data protection rather than ordering data.

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

Happy Learning !!

Source Code on Github

Leave a Comment

Comments are closed.

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.