Selection Sort in Java: Explanation & Practice

Implement selection sort algorithm

Problem summary

Write a program that sorts an array using selection sort algorithm.

Starter code

public class Main {
    public static void main(String[] args) {
        int[] arr = {29, 10, 14, 37, 13}; // Test case 1
        
        // Implement selection sort
        // Print sorted array space-separated
    }
}

Expected output and test cases

  • Sort array
    10 13 14 29 37
  • Sort [3,2,1]
    1 2 3
  • All same elements
    5 5 5

Hints

  1. Find minimum in unsorted portion
  2. Swap with first unsorted position
  3. Move boundary of sorted portion

Related Arrays exercises

Practice all Arrays exercises · Run this idea in the Java compiler