Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Selection Sort in Java: Step‑by‑Step Example & Full Code

What Is Selection Sort?

Selection Sort is a straightforward comparison‑based algorithm that repeatedly selects the smallest remaining element and swaps it into place. While not the fastest for large data sets, its simplicity makes it ideal for educational purposes and small arrays.

How the Algorithm Works

Java Implementation

Below is a concise Java program that demonstrates the selection sort algorithm on an integer array. The code prints the array before and after sorting, as well as the intermediate steps to illustrate each pass.

package com.guru99;

public class SelectionSortAlgo {

    public static void main(String[] args) {
        int[] myArray = {860, 8, 200, 9};

        System.out.println("------Before Selection Sort-----");
        printArray(myArray);

        selection(myArray); // Sort the array

        System.out.println("-----After Selection Sort-----");
        printArray(myArray);
    }

    public static void selection(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            System.out.println("Sort Pass Number " + (i + 1));
            int index = i;

            for (int j = i + 1; j < array.length; j++) {
                System.out.println("Comparing " + array[index] + " and " + array[j]);
                if (array[j] < array[index]) {
                    System.out.println(array[index] + " is greater than " + array[j]);
                    index = j;
                }
            }

            int smallerNumber = array[index];
            array[index] = array[i];
            array[i] = smallerNumber;

            System.out.println("Swapping Elements: New Array After Swap");
            printArray(array);
        }
    }

    private static void printArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }
}

Sample Output

------Before Selection Sort-----
860 8 200 9 
Sort Pass Number 1
Comparing 860 and 8
860 is greater than 8
Comparing 8 and 200
Comparing 8 and 9
Swapping Elements: New Array After Swap
8 860 200 9 
Sort Pass Number 2
Comparing 860 and 200
860 is greater than 200
Comparing 200 and 9
200 is greater than 9
Swapping Elements: New Array After Swap
8 9 200 860 
Sort Pass Number 3
Comparing 200 and 860
Swapping Elements: New Array After Swap
8 9 200 860 
-----After Selection Sort-----
8 9 200 860 

Performance Notes

Selection Sort performs O(n²) comparisons regardless of input and uses at most O(1) additional memory. Its deterministic behavior makes it suitable for environments where memory is constrained.

Java

  1. Java Hello World: Step‑by‑Step Guide to Writing Your First Java Program
  2. Encapsulation in Java: A Comprehensive Guide with Practical Example
  3. Understanding Java String.charAt(): Syntax, Return Type, Exceptions, and a Practical Example
  4. Mastering Java’s String.endsWith(): How to Check String Suffixes with Examples
  5. Java HashMap: A Comprehensive Guide
  6. Command‑Line Arguments in Java: A Practical Guide with Code Examples
  7. Java Abstraction: Mastering Abstract Classes, Methods, and Practical Examples
  8. Understanding Java's throws Keyword: Examples & Best Practices
  9. Master Bubble Sort in Java: Clear Example & Step‑by‑Step Walkthrough
  10. Insertion Sort Algorithm in Java: A Clear Example & Step‑by‑Step Guide