How to Sort an Array in Java: A Beginner’s Guide
Sorting is a crucial concept in computer programming, especially when working with large sets of data. As a Java developer, understanding how to sort arrays effectively can drastically improve your application’s performance. Whether you’re sorting numbers, strings, or custom objects, the ability to efficiently manage and organize data is essential. In this guide, we will walk you through different ways to sort arrays in Java, covering everything from basic built-in methods to advanced sorting algorithms. Let’s dive in!
What Is an Array in Java?
Definition: An array is a data structure that holds a fixed number of elements of the same type.
Array Types:
- Primitive Arrays (e.g.,
int[]
,char[]
) - Object Arrays (e.g.,
String[]
,Integer[]
)
Why Sorting Matters: Sorting improves search performance, data organization, and is foundational in many algorithms.
How to Sort an Array in Java Using Built-In Methods
Using Arrays.sort()
import java.util.Arrays; public class Main { public static void main(String[] args) { int[] arr = {5, 2, 8, 1, 3}; Arrays.sort(arr); System.out.println(Arrays.toString(arr)); // [1, 2, 3, 5, 8] } }
Sorting in Descending Order
import java.util.Arrays; import java.util.Collections; public class Main { public static void main(String[] args) { Integer[] arr = {5, 2, 8, 1, 3}; Arrays.sort(arr, Collections.reverseOrder()); System.out.println(Arrays.toString(arr)); // [8, 5, 3, 2, 1] } }
Understanding Sorting Algorithms in Java
1. Bubble Sort
Repeatedly swaps adjacent elements if they are in the wrong order.

image source: wikimedia.org
public class BubbleSort { public static void main(String[] args) { int[] arr = {5, 2, 8, 1, 3}; bubbleSort(arr); System.out.println(Arrays.toString(arr)); } public static void bubbleSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } }
2. Selection Sort
Finds the smallest element and places it at the beginning.
public class SelectionSort { public static void main(String[] args) { int[] arr = {5, 2, 8, 1, 3}; selectionSort(arr); System.out.println(Arrays.toString(arr)); } public static void selectionSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { int min = i; for (int j = i + 1; j < arr.length; j++) { if (arr[j] < arr[min]) { min = j; } } int temp = arr[min]; arr[min] = arr[i]; arr[i] = temp; } } }
3. Insertion Sort
Builds the final sorted array one item at a time.

image source: wikimedia.org
public class InsertionSort { public static void main(String[] args) { int[] arr = {5, 2, 8, 1, 3}; insertionSort(arr); System.out.println(Arrays.toString(arr)); } public static void insertionSort(int[] arr) { for (int i = 1; i < arr.length; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } } }
Advanced Sorting Methods in Java
Merge Sort
A divide-and-conquer algorithm that splits and merges arrays.
public class MergeSort { public static void main(String[] args) { int[] arr = {5, 2, 8, 1, 3}; mergeSort(arr, 0, arr.length - 1); System.out.println(Arrays.toString(arr)); } public static void mergeSort(int[] arr, int left, int right) { if (left < right) { int mid = (left + right) / 2; mergeSort(arr, left, mid); mergeSort(arr, mid + 1, right); merge(arr, left, mid, right); } } public static void merge(int[] arr, int left, int mid, int right) { int n1 = mid - left + 1; int n2 = right - mid; int[] leftArr = new int[n1]; int[] rightArr = new int[n2]; System.arraycopy(arr, left, leftArr, 0, n1); System.arraycopy(arr, mid + 1, rightArr, 0, n2); int i = 0, j = 0, k = left; while (i < n1 && j < n2) { if (leftArr[i] <= rightArr[j]) { arr[k++] = leftArr[i++]; } else { arr[k++] = rightArr[j++]; } } while (i < n1) arr[k++] = leftArr[i++]; while (j < n2) arr[k++] = rightArr[j++]; } }
Quick Sort
Uses a pivot to divide and conquer the array for fast sorting.
public class QuickSort { public static void main(String[] args) { int[] arr = {5, 2, 8, 1, 3}; quickSort(arr, 0, arr.length - 1); System.out.println(Arrays.toString(arr)); } public static void quickSort(int[] arr, int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } public static int partition(int[] arr, int low, int high) { int pivot = arr[high]; int i = low - 1; for (int j = low; j < high; j++) { if (arr[j] <= pivot) { i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return i + 1; } }
Conclusion
Sorting is a core concept in Java that every developer should master. Here’s a quick recap:
- For beginners: Use
Arrays.sort()
for simplicity. - For learning: Try Bubble, Selection, and Insertion Sort to understand logic and flow.
- For performance: Use Merge Sort or Quick Sort for large datasets.
Play with different methods, understand how they work, and pick the one that best suits your data needs. Happy coding!