Selection Sort in Java: A Simple Sorting Algorithm

Selection Sort is a fundamental sorting algorithm in Java that arranges elements in ascending or descending order. In this article, we will delve into Selection Sort in Java, understand how it works, and explore a code example to implement this sorting algorithm in Java. By the end, you will have a clear understanding of Selection Sort in Java and its practical application.

Selection Sort in Java

Selection Sort in Java: Explained with Code Example

public class SelectionSort {
    public static void selectionSort(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < n; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;

    public static void main(String[] args) {
        int[] arr = {64, 34, 25, 12, 22, 11, 90};

        System.out.println("Array before sorting:");
        for (int num : arr) {
            System.out.print(num + " ");


        System.out.println("\n\nArray after sorting:");
        for (int num : arr) {
            System.out.print(num + " ");


Array before sorting:
64 34 25 12 22 11 90 

Array after sorting:
11 12 22 25 34 64 90

Selection Sort in Java: Step-by-Step Process

  1. Start with an unsorted array.
  2. Find the minimum element in the remaining unsorted array.
  3. Swap the minimum element with the first element of the unsorted array.
  4. Repeat steps 2-3, considering the remaining unsorted array until the entire array is sorted.

The code example provided demonstrates the Selection Sort algorithm in action. It takes an array of integers, performs the sorting operation, and displays the sorted array. The “selectionSort” method implements the Selection Sort logic by finding the minimum element and swapping it with the appropriate position.

Time Complexity: The time complexity of Selection Sort in Java is O(n^2), where n is the number of elements in the array. This is because for each element, we traverse the remaining unsorted array to find the minimum element.

Space Complexity: The space complexity of Selection Sort in Java is O(1) since the algorithm requires only a constant amount of extra space to perform the sorting operation. The sorting is done in-place, without requiring additional data structures.

Selection Sort in Java: Conclusion

Selection Sort in Java is a straightforward and efficient sorting algorithm, particularly suitable for small data sets. While it may not be as efficient as some advanced sorting algorithms for larger arrays, understanding Selection Sort is crucial for building a solid foundation in sorting techniques.

By implementing Selection Sort in Java using the provided code example and following the step-by-step process, you can grasp the essence of this sorting algorithm. Utilize this knowledge to explore other sorting algorithms and enhance your Java programming skills. Mastery of Selection Sort in Java equips you with the ability to tackle sorting challenges effectively and efficiently.

Related Articles:

Leave a Reply

Your email address will not be published. Required fields are marked *