Java Sorting Algorithms
A sorting algorithm is a step-by-step way to arrange data in a chosen order. In Java, sorting often means putting numbers from smallest to largest or strings in alphabetical order.
Sorted data is easier to read, compare, and search. For example, binary search only works correctly when the data is already sorted.
Using Java’s Built-In Sort
For most real programs, use the standard library first. The Arrays.sort() method sorts an array in ascending order and changes the original array.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] scores = {72, 95, 88, 64, 91};
Arrays.sort(scores);
System.out.println(Arrays.toString(scores));
}
}
Output:
[64, 72, 88, 91, 95]
This is the best choice when you simply need sorted values. Java’s built-in sorting methods are tested, optimized, and less error-prone than writing your own sort for everyday code.
Sorting Algorithm Idea
A sorting algorithm usually compares values and moves them until the collection is ordered. Different algorithms use different strategies. Some are easy to understand but slower on large inputs. Others are more complex but much faster.
To learn the basic idea, it helps to write a simple algorithm by hand.
Selection Sort
Selection sort repeatedly finds the smallest remaining value and moves it into the next correct position. It divides the array into two parts: a sorted part at the front and an unsorted part after it.
import java.util.Arrays;
public class Main {
static void selectionSort(int[] values) {
for (int i = 0; i < values.length - 1; i++) {
int smallestIndex = i;
for (int j = i + 1; j < values.length; j++) {
if (values[j] < values[smallestIndex]) {
smallestIndex = j;
}
}
int temp = values[i];
values[i] = values[smallestIndex];
values[smallestIndex] = temp;
}
}
public static void main(String[] args) {
int[] numbers = {5, 2, 9, 1, 3};
selectionSort(numbers);
System.out.println(Arrays.toString(numbers));
}
}
Output:
[1, 2, 3, 5, 9]
How Selection Sort Works
On the first pass, selection sort finds the smallest value in the whole array and swaps it into index 0. On the next pass, it finds the smallest value from index 1 onward and swaps it into index 1. This continues until every value is in order.
The outer loop chooses the position to fill. The inner loop searches the remaining unsorted values for the smallest element. The swap uses a temporary variable so one value is not overwritten before it is moved.
Time Complexity
Selection sort has a time complexity of O(n^2). That means the amount of work grows quickly as the array gets larger, because the algorithm uses a loop inside another loop.
Java’s built-in sorting methods are generally better for production code. Still, selection sort is useful for learning because the comparisons and swaps are easy to follow.
Common Sorting Choices
| Approach | Best use | Note |
|---|---|---|
Arrays.sort() |
Most array sorting tasks | Simple and optimized |
| Selection sort | Learning algorithm basics | Easy to trace but slow for large arrays |
| Merge sort or quicksort | Larger algorithm study | Faster strategies with more complex logic |
Takeaway: use Java’s built-in sort in normal programs, and study simple sorting algorithms to understand how ordered data is built step by step.
